Utilities/Startup.py

changeset 0
de9c2efb9d02
child 12
1d8dd9706f46
equal deleted inserted replaced
-1:000000000000 0:de9c2efb9d02
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2002 - 2009 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing some startup helper funcions
8 """
9
10 import os
11 import sys
12
13 from PyQt4.QtCore import QTranslator, QTextCodec, QLocale, QDir, SIGNAL, SLOT, \
14 QLibraryInfo
15 from PyQt4.QtGui import QApplication
16
17 from E4Gui.E4Application import E4Application
18
19 import Preferences
20 import Utilities
21 from UI.Info import Version
22
23 import UI.PixmapCache
24
25 from eric4config import getConfig
26
27
28 def makeAppInfo(argv, name, arg, description, options = []):
29 """
30 Module function to generate a dictionary describing the application.
31
32 @param argv list of commandline parameters (list of strings)
33 @param name name of the application (string)
34 @param arg commandline arguments (string)
35 @param description text describing the application (string)
36 @param options list of additional commandline options
37 (list of tuples of two strings (commandline option, option description)).
38 The options --version, --help and -h are always present and must not
39 be repeated in this list.
40 @return dictionary describing the application
41 """
42 return {
43 "bin": argv[0],
44 "arg": arg,
45 "name": name,
46 "description": description,
47 "version": Version,
48 "options" : options
49 }
50
51 def usage(appinfo, optlen = 12):
52 """
53 Module function to show the usage information.
54
55 @param appinfo dictionary describing the application
56 @param optlen length of the field for the commandline option (integer)
57 """
58 options = [\
59 ("--version", "show the program's version number and exit"),
60 ("-h, --help", "show this help message and exit")
61 ]
62 options.extend(appinfo["options"])
63
64 print \
65 """
66 Usage: %(bin)s [OPTIONS] %(arg)s
67
68 %(name)s - %(description)s
69
70 Options:""" % appinfo
71 for opt in options:
72 print " %s %s" % (opt[0].ljust(optlen), opt[1])
73 sys.exit(0)
74
75 def version(appinfo):
76 """
77 Module function to show the version information.
78
79 @param appinfo dictionary describing the application
80 """
81 print \
82 """
83 %(name)s %(version)s
84
85 %(description)s
86
87 Copyright (c) 2002 - 2009 Detlev Offenbach <detlev@die-offenbachs.de>
88 This is free software; see LICENSE.GPL3 for copying conditions.
89 There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
90 PARTICULAR PURPOSE.""" % appinfo
91 sys.exit(0)
92
93 def handleArgs(argv, appinfo):
94 """
95 Module function to handle the always present commandline options.
96
97 @param argv list of commandline parameters (list of strings)
98 @param appinfo dictionary describing the application
99 @return index of the '--' option (integer). This is used to tell
100 the application, that all additional option don't belong to
101 the application.
102 """
103 ddindex = 30000 # arbitrarily large number
104 args = {
105 "--version": version,
106 "--help": usage,
107 "-h": usage
108 }
109 if '--' in argv:
110 ddindex = argv.index("--")
111 for a in args:
112 if a in argv and argv.index(a) < ddindex:
113 args[a](appinfo)
114 return ddindex
115
116 def loadTranslatorForLocale(dirs, tn):
117 """
118 Module function to find and load a specific translation.
119
120 @param dirs Searchpath for the translations. (list of strings)
121 @param tn The translation to be loaded. (string)
122 @return Tuple of a status flag and the loaded translator. (int, QTranslator)
123 """
124 trans = QTranslator(None)
125 for dir in dirs:
126 loaded = trans.load(tn, dir)
127 if loaded:
128 return (trans, True)
129
130 print "Warning: translation file '" + tn + "'could not be loaded."
131 print "Using default."
132 return (None, False)
133
134 def initializeResourceSearchPath():
135 """
136 Module function to initialize the default mime source factory.
137 """
138 defaultIconPath = os.path.join(getConfig('ericIconDir'), "default")
139 iconPaths = Preferences.getIcons("Path")
140 for iconPath in iconPaths:
141 if iconPath:
142 UI.PixmapCache.addSearchPath(iconPath)
143 if not defaultIconPath in iconPaths:
144 UI.PixmapCache.addSearchPath(defaultIconPath)
145
146 # the translator must not be deleted, therefore we save them here
147 loaded_translators = {}
148
149 def loadTranslators(qtTransDir, app, translationFiles = ()):
150 """
151 Module function to load all required translations.
152
153 @param qtTransDir directory of the Qt translations files (string)
154 @param app reference to the application object (QApplication)
155 @param translationFiles tuple of additional translations to
156 be loaded (tuple of strings)
157 @return the requested locale (string)
158 """
159 translations = ("qt", "eric4") + translationFiles
160 loc = Preferences.getUILanguage()
161 if loc is None:
162 return
163
164 if loc == "System":
165 loc = QLocale.system().name()
166 if loc != "C":
167 dirs = [getConfig('ericTranslationsDir'), Utilities.getConfigDir()]
168 if qtTransDir is not None:
169 dirs.append(qtTransDir)
170
171 loca = loc
172 for tf in ["%s_%s" % (tr, loc) for tr in translations]:
173 translator, ok = loadTranslatorForLocale(dirs, tf)
174 loaded_translators[tf] = translator
175 if ok:
176 app.installTranslator(translator)
177 else:
178 if tf.startswith("eric4"):
179 loca = None
180 loc = loca
181 else:
182 loc = None
183 return loc
184
185 def simpleAppStartup(argv, appinfo, mwFactory, quitOnLastWindowClosed = True):
186 """
187 Module function to start up an application that doesn't need a specialized start up.
188
189 This function is used by all of eric4's helper programs.
190
191 @param argv list of commandline parameters (list of strings)
192 @param appinfo dictionary describing the application
193 @param mwFactory factory function generating the main widget. This
194 function must accept the following parameter.
195 <dl>
196 <dt>argv</dt>
197 <dd>list of commandline parameters (list of strings)</dd>
198 </dl>
199 @keyparam quitOnLastWindowClosed flag indicating to quit the application,
200 if the last window was closed (boolean)
201 """
202 ddindex = handleArgs(argv, appinfo)
203 app = E4Application(argv)
204 app.setQuitOnLastWindowClosed(quitOnLastWindowClosed)
205 try:
206 sys.setappdefaultencoding(Preferences.getSystem("StringEncoding"))
207 except AttributeError:
208 pass
209
210 initializeResourceSearchPath()
211 QApplication.setWindowIcon(UI.PixmapCache.getIcon("eric.png"))
212
213 qt4TransDir = Preferences.getQt4TranslationsDir()
214 if not qt4TransDir:
215 qt4TransDir = QLibraryInfo.location(QLibraryInfo.TranslationsPath)
216 loadTranslators(qt4TransDir, app)
217
218 QTextCodec.setCodecForCStrings(\
219 QTextCodec.codecForName(Preferences.getSystem("StringEncoding"))
220 )
221
222 w = mwFactory(argv)
223 app.connect(app, SIGNAL("lastWindowClosed()"), app, SLOT("quit()"))
224 w.show()
225
226 return app.exec_()

eric ide

mercurial