unicode: use core.encode() and core.decode() throughout git-cola
[git-cola.git] / cola / main.py
blob709989f5b4e191d7944c03c7fb40b064d170bd19
1 # Copyright(C) 2008, David Aguilar <davvid@gmail.com>
3 import optparse
4 import sys
5 import os
7 from cola import utils
8 from cola import version
10 def main():
11 parser = optparse.OptionParser(usage='%prog [options]')
13 parser.add_option('-v', '--version',
14 help='Show cola version',
15 dest='version',
16 default=False,
17 action='store_true')
18 parser.add_option('-s', '--style',
19 help='Applies an alternate stylesheet. '
20 'The allowed values are: "dark" or a file path.',
21 dest='style',
22 metavar='PATH or STYLE',
23 default='')
24 parser.add_option('-r', '--repo',
25 help='Specifies the path to a git repository.',
26 dest='repo',
27 metavar='PATH',
28 default=os.getcwd())
29 opts, args = parser.parse_args()
31 if opts.version or 'version' in args:
32 print "cola version", version.version
33 sys.exit(0)
35 repo = os.path.realpath(opts.repo)
36 if not os.path.isdir(repo):
37 print >> sys.stderr, "fatal: '%s' is not a directory. Consider supplying -r <path>.\n" % repo
38 sys.exit(-1)
39 os.chdir(opts.repo)
41 try:
42 # Defer these imports to allow git cola --version without pyqt installed
43 from PyQt4 import QtCore
44 from PyQt4 import QtGui
45 except ImportError:
46 print >> sys.stderr, 'Sorry, you do not seem to have PyQt4 installed.'
47 print >> sys.stderr, 'Please install it before using cola.'
48 print >> sys.stderr, 'e.g.: sudo apt-get install python-qt4'
49 sys.exit(-1)
50 from cola import qtutils
52 class ColaApplication(QtGui.QApplication):
53 """This makes translation work by throwing out the context."""
54 wrapped = QtGui.QApplication.translate
55 def translate(*args):
56 trtxt = unicode(ColaApplication.wrapped('', *args[2:]))
57 if trtxt[-6:-4] == '@@': # handle @@verb / @@noun
58 trtxt = trtxt[:-6]
59 return trtxt
61 app = ColaApplication(sys.argv)
62 QtGui.QApplication.translate = app.translate
63 app.setWindowIcon(QtGui.QIcon(utils.get_icon('git.png')))
64 locale = str(QtCore.QLocale().system().name())
65 qmfile = utils.get_qm_for_locale(locale)
66 if os.path.exists(qmfile):
67 translator = QtCore.QTranslator(app)
68 translator.load(qmfile)
69 app.installTranslator(translator)
71 if opts.style:
72 # This loads the built-in and user-specified stylesheets.
73 # We allows absolute and relative paths to a stylesheet
74 # by assuming that non-file arguments refer to a built-in style.
75 if os.path.isabs(opts.style) or os.path.isfile(opts.style):
76 filename = opts.style
77 else:
78 filename = utils.get_stylesheet(opts.style)
80 try:
81 # Automatically register each subdirectory in the style dir
82 # as a Qt resource directory.
83 dirname = os.path.dirname(filename)
84 resource_paths = []
85 resource_paths.extend(utils.get_resource_dirs(dirname))
86 for r in resource_paths:
87 basename = os.path.basename(r)
88 QtCore.QDir.setSearchPaths(basename, [r])
89 stylesheet = open(filename, 'r')
90 style = stylesheet.read()
91 stylesheet.close()
92 app.setStyleSheet(style)
93 except:
94 print >> sys.stderr, ("warn: '%s' is not a valid style."
95 % opts.style)
97 from cola.models import Model
98 from cola.views import View
99 from cola.controllers import Controller
101 model = Model()
102 view = View(app.activeWindow())
104 valid = model.use_worktree(repo)
105 while not valid:
106 gitdir = qtutils.opendir_dialog(view, 'Open Git Repository...', os.getcwd())
107 if not gitdir:
108 sys.exit(-1)
109 valid = model.use_worktree(gitdir)
111 os.chdir(model.git.get_work_tree())
112 view.show()
113 ctl = Controller(model, view)
114 sys.exit(app.exec_())