src/eric7/Toolbox/Startup.py

branch
eric7
changeset 10303
ee1aadab1215
parent 9674
43dd357b3bff
child 10431
64157aeb0312
equal deleted inserted replaced
10302:8cb0dabf852f 10303:ee1aadab1215
18 from eric7.EricWidgets.EricApplication import EricApplication 18 from eric7.EricWidgets.EricApplication import EricApplication
19 from eric7.Globals import getConfig 19 from eric7.Globals import getConfig
20 from eric7.SystemUtilities import QtUtilities 20 from eric7.SystemUtilities import QtUtilities
21 21
22 application = None 22 application = None
23
24
25 def usage(appinfo, optlen=12):
26 """
27 Module function to show the usage information.
28
29 @param appinfo dictionary describing the application
30 @param optlen length of the field for the commandline option (integer)
31 """
32 options = [
33 ("--version", "show the program's version number and exit"),
34 ("-h, --help", "show this help message and exit"),
35 ]
36 options.extend(appinfo["options"])
37
38 print(
39 """\n"""
40 """Usage: {bin} [OPTIONS] {arg}\n"""
41 """\n"""
42 """{name} - {description}\n"""
43 """\n"""
44 """Options:""".format(**appinfo)
45 )
46 for opt in options:
47 print(" {0} {1}".format(opt[0].ljust(optlen), opt[1]))
48 sys.exit(0)
49
50
51 def version(appinfo):
52 """
53 Module function to show the version information.
54
55 @param appinfo dictionary describing the application
56 """
57 print(
58 """\n"""
59 """{name} {version}\n"""
60 """\n"""
61 """{description}\n"""
62 """\n"""
63 """Copyright (c) 2002 - 2023 Detlev Offenbach"""
64 """ <detlev@die-offenbachs.de>\n"""
65 """This is free software; see LICENSE.txt for copying"""
66 """ conditions.\n"""
67 """There is NO warranty; not even for MERCHANTABILITY or FITNESS"""
68 """ FOR A\n"""
69 """PARTICULAR PURPOSE.""".format(**appinfo)
70 )
71 sys.exit(0)
72
73
74 def handleArgs(argv, appinfo):
75 """
76 Module function to handle the always present commandline options.
77
78 @param argv list of commandline parameters (list of strings)
79 @param appinfo dictionary describing the application
80 @return index of the '--' option (integer). This is used to tell
81 the application, that all additional options don't belong to
82 the application.
83 """
84 ddindex = 30000 # arbitrarily large number
85 args = {"--version": version, "--help": usage, "-h": usage}
86 if "--" in argv:
87 ddindex = argv.index("--")
88 for a in args:
89 if a in argv and argv.index(a) < ddindex:
90 args[a](appinfo)
91 return ddindex
92
93
94 def loadTranslatorForLocale(dirs, tn):
95 """
96 Module function to find and load a specific translation.
97
98 @param dirs Searchpath for the translations. (list of strings)
99 @param tn The translation to be loaded. (string)
100 @return Tuple of a status flag and the loaded translator
101 (int, QTranslator)
102 """
103 trans = QTranslator(None)
104 for directory in dirs:
105 loaded = trans.load(tn, directory)
106 if loaded:
107 return (trans, True)
108
109 print("Warning: translation file '" + tn + "'could not be loaded.")
110 print("Using default.")
111 return (None, False)
112 23
113 24
114 def initializeResourceSearchPath(application): 25 def initializeResourceSearchPath(application):
115 """ 26 """
116 Module function to initialize the default mime source factory. 27 Module function to initialize the default mime source factory.
180 91
181 def loadTranslators(qtTransDir, app, translationFiles=()): 92 def loadTranslators(qtTransDir, app, translationFiles=()):
182 """ 93 """
183 Module function to load all required translations. 94 Module function to load all required translations.
184 95
185 @param qtTransDir directory of the Qt translations files (string) 96 @param qtTransDir directory of the Qt translations files
186 @param app reference to the application object (QApplication) 97 @type str
98 @param app reference to the application object
99 @type QApplication
187 @param translationFiles tuple of additional translations to 100 @param translationFiles tuple of additional translations to
188 be loaded (tuple of strings) 101 be loaded
189 @return the requested locale (string) 102 @type tuple of str
103 @return the requested locale
104 @rtype str
190 """ 105 """
191 from eric7 import Preferences 106 from eric7 import Preferences
192 107
193 global loaded_translators 108 global loaded_translators
194 109
226 else: 141 else:
227 loc = None 142 loc = None
228 return loc 143 return loc
229 144
230 145
231 def simpleAppStartup( 146 def loadTranslatorForLocale(dirs, tn):
232 argv, 147 """
233 appinfo, 148 Module function to find and load a specific translation.
149
150 @param dirs searchpath for the translations
151 @type list of str
152 @param tn translation to be loaded
153 @type str
154 @return tuple containing a status flag and the loaded translator
155 @rtype tuple of (int, QTranslator)
156 """
157 trans = QTranslator(None)
158 for directory in dirs:
159 loaded = trans.load(tn, directory)
160 if loaded:
161 return (trans, True)
162
163 print("Warning: translation file '" + tn + "'could not be loaded.")
164 print("Using default.")
165 return (None, False)
166
167
168 def appStartup(
169 args,
234 mwFactory, 170 mwFactory,
235 quitOnLastWindowClosed=True, 171 quitOnLastWindowClosed=True,
236 app=None, 172 app=None,
237 raiseIt=True, 173 raiseIt=True,
238 installErrorHandler=False, 174 installErrorHandler=False,
241 Module function to start up an application that doesn't need a specialized 177 Module function to start up an application that doesn't need a specialized
242 start up. 178 start up.
243 179
244 This function is used by all of eric's helper programs. 180 This function is used by all of eric's helper programs.
245 181
246 @param argv list of commandline parameters (list of strings) 182 @param args namespace object created by ArgumentParser.parse_args() containing
247 @param appinfo dictionary describing the application 183 the parsed command line arguments
184 @type argparse.Namespace
248 @param mwFactory factory function generating the main widget. This 185 @param mwFactory factory function generating the main widget. This
249 function must accept the following parameter. 186 function must accept the following parameter.
250 <dl> 187 <dl>
251 <dt>argv</dt> 188 <dt>args</dt>
252 <dd>list of commandline parameters (list of strings)</dd> 189 <dd>parsed command line arguments (argparse.Namespace)</dd>
253 </dl> 190 </dl>
254 @param quitOnLastWindowClosed flag indicating to quit the application, 191 @param quitOnLastWindowClosed flag indicating to quit the application,
255 if the last window was closed (boolean) 192 if the last window was closed
256 @param app reference to the application object (QApplication or None) 193 @type bool
194 @param app reference to the application object
195 @type QApplication or None
257 @param raiseIt flag indicating to raise the generated application 196 @param raiseIt flag indicating to raise the generated application
258 window (boolean) 197 window
198 @type bool
259 @param installErrorHandler flag indicating to install an error 199 @param installErrorHandler flag indicating to install an error
260 handler dialog (boolean) 200 handler dialog
261 @return exit result (integer) 201 @type bool
202 @return exit result
203 @rtype int
262 """ 204 """
263 global application 205 global application
264 206
265 if "__PYVENV_LAUNCHER__" in os.environ: 207 if "__PYVENV_LAUNCHER__" in os.environ:
266 del os.environ["__PYVENV_LAUNCHER__"] 208 del os.environ["__PYVENV_LAUNCHER__"]
267 209
268 handleArgs(argv, appinfo)
269 if app is None: 210 if app is None:
270 # set the library paths for plugins 211 # set the library paths for plugins
271 setLibraryPaths() 212 setLibraryPaths()
272 app = EricApplication(argv) 213 app = EricApplication(sys.argv)
273 application = app 214 application = app
274 app.setQuitOnLastWindowClosed(quitOnLastWindowClosed) 215 app.setQuitOnLastWindowClosed(quitOnLastWindowClosed)
275 216
276 # the following code depends upon a valid application object 217 # the following code depends upon a valid application object
277 from eric7 import Preferences # __IGNORE_WARNING_I101__ 218 from eric7 import Preferences # __IGNORE_WARNING_I101__
286 if not qtTransDir: 227 if not qtTransDir:
287 qtTransDir = QLibraryInfo.path(QLibraryInfo.LibraryPath.TranslationsPath) 228 qtTransDir = QLibraryInfo.path(QLibraryInfo.LibraryPath.TranslationsPath)
288 loadTranslators(qtTransDir, app, ("qscintilla",)) 229 loadTranslators(qtTransDir, app, ("qscintilla",))
289 # qscintilla needed for web browser 230 # qscintilla needed for web browser
290 231
291 w = mwFactory(argv) 232 w = mwFactory(args)
292 if w is None: 233 if w is None:
293 return 100 234 return 100
294 235
295 if quitOnLastWindowClosed: 236 if quitOnLastWindowClosed:
296 app.lastWindowClosed.connect(app.quit) 237 app.lastWindowClosed.connect(app.quit)

eric ide

mercurial