eric5.py

changeset 15
f6ccc31d6e72
child 18
3b1f5d872fd7
equal deleted inserted replaced
14:092aa8fafa4e 15:f6ccc31d6e72
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3
4 # Copyright (c) 2002 - 2010 Detlev Offenbach <detlev@die-offenbachs.de>
5 #
6
7 """
8 Eric4 Python IDE
9
10 This is the main Python script that performs the necessary initialization
11 of the IDE and starts the Qt event loop.
12 """
13
14 import sys
15 import os
16 import traceback
17 import io
18 import time
19 import logging
20
21 from PyQt4.QtCore import QTextCodec, SIGNAL, SLOT, qWarning, \
22 QLibraryInfo, QTimer
23 from PyQt4.QtGui import QApplication, QErrorMessage
24
25 # some global variables needed to start the application
26 args = None
27 mainWindow = None
28 splash = None
29
30 # generate list of arguments to be remembered for a restart
31 restartArgsList = ["--nosplash", "--plugin", "--debug", "--config"]
32 restartArgs = [arg for arg in sys.argv[1:] if arg.split("=", 1)[0] in restartArgsList]
33
34 if "--debug" in sys.argv:
35 del sys.argv[sys.argv.index("--debug")]
36 logging.basicConfig(level = logging.DEBUG)
37
38 for arg in sys.argv:
39 if arg.startswith("--config="):
40 import Utilities
41 configDir = arg.replace("--config=", "")
42 Utilities.setConfigDir(configDir)
43 sys.argv.remove(arg)
44 break
45
46 # make ThirdParty package available as a packages repository
47 try:
48 import pygments
49 except ImportError:
50 sys.path.insert(2, os.path.join(os.path.dirname(__file__), "ThirdParty", "Pygments"))
51
52 from E4Gui.E4Application import E4Application
53
54 from UI.Info import Program, Version, BugAddress
55 from UI.SplashScreen import SplashScreen, NoneSplashScreen
56 from E4Gui.E4SingleApplication import E4SingleApplicationClient
57
58 import Utilities
59 from Utilities import Startup
60
61 logging.debug("Importing Preferences")
62 import Preferences
63
64 def handleSingleApplication(ddindex):
65 """
66 Global function to handle the single application mode.
67
68 @param ddindex index of a '--' option in the options list
69 """
70 client = E4SingleApplicationClient()
71 res = client.connect()
72 if res > 0:
73 if "--nosplash" in sys.argv and sys.argv.index("--nosplash") < ddindex:
74 del sys.argv[sys.argv.index("--nosplash")]
75 if "--noopen" in sys.argv and sys.argv.index("--noopen") < ddindex:
76 del sys.argv[sys.argv.index("--noopen")]
77 if "--debug" in sys.argv and sys.argv.index("--debug") < ddindex:
78 del sys.argv[sys.argv.index("--debug")]
79 for arg in sys.argv:
80 if arg.startswith("--config="):
81 sys.argv.remove(arg)
82 break
83 if len(sys.argv) > 1:
84 client.processArgs(sys.argv[1:])
85 sys.exit(0)
86 elif res < 0:
87 print("eric5: %s" % client.errstr())
88 sys.exit(res)
89
90 def excepthook(excType, excValue, tracebackobj):
91 """
92 Global function to catch unhandled exceptions.
93
94 @param excType exception type
95 @param excValue exception value
96 @param tracebackobj traceback object
97 """
98 separator = '-' * 80
99 logFile = os.path.join(Utilities.getConfigDir(), "eric5_error.log")
100 notice = \
101 """An unhandled exception occurred. Please report the problem\n"""\
102 """using the error reporting dialog or via email to <%s>.\n"""\
103 """A log has been written to "%s".\n\nError information:\n""" % \
104 (BugAddress, logFile)
105 timeString = time.strftime("%Y-%m-%d, %H:%M:%S")
106
107 versionInfo = "\n%s\n%s" % (separator, Utilities.generateVersionInfo())
108 pluginVersionInfo = Utilities.generatePluginsVersionInfo()
109 if pluginVersionInfo:
110 versionInfo += "%s\n%s" % (separator, pluginVersionInfo)
111 distroInfo = Utilities.generateDistroInfo()
112 if distroInfo:
113 versionInfo += "%s\n%s" % (separator, distroInfo)
114
115 tbinfofile = io.StringIO()
116 traceback.print_tb(tracebackobj, None, tbinfofile)
117 tbinfofile.seek(0)
118 tbinfo = tbinfofile.read()
119 errmsg = '%s: \n%s' % (str(excType), str(excValue))
120 sections = [separator, timeString, separator, errmsg, separator, tbinfo]
121 msg = '\n'.join(sections)
122 try:
123 f = open(logFile, "w")
124 f.write(msg)
125 f.write(versionInfo)
126 f.close()
127 except IOError:
128 pass
129 qWarning(str(notice) + str(msg) + str(versionInfo))
130
131 def uiStartUp():
132 """
133 Global function to finalize the start up of the main UI.
134
135 Note: It is activated by a zero timeout single-shot timer.
136 """
137 global args, mainWindow, splash
138
139 if splash:
140 splash.finish(mainWindow)
141 del splash
142
143 mainWindow.checkForErrorLog()
144 mainWindow.processArgs(args)
145 mainWindow.performVersionCheck(False)
146 mainWindow.checkConfigurationStatus()
147
148 def main():
149 """
150 Main entry point into the application.
151 """
152 global args, mainWindow, splash, restartArgs
153
154 sys.excepthook = excepthook
155
156 options = [\
157 ("--config=configDir",
158 "use the given directory as the one containing the config files"),
159 ("--debug", "activate debugging output to the console"),
160 ("--nosplash", "don't show the splash screen"),
161 ("--noopen", "don't open anything at startup except that given in command"),
162 ("--plugin=plugin-file", "load the given plugin file (plugin development)"),
163 ("--start-session", "load the global session file"),
164 ("--", "indicate that there are options for the program to be debugged"),
165 ("", "(everything after that is considered arguments for this program)")
166 ]
167 appinfo = Startup.makeAppInfo(sys.argv,
168 "Eric4",
169 "[project | files... [--] [debug-options]]",
170 "A Python IDE",
171 options)
172 ddindex = Startup.handleArgs(sys.argv, appinfo)
173
174 if Preferences.getUI("SingleApplicationMode"):
175 handleSingleApplication(ddindex)
176
177 app = E4Application(sys.argv)
178
179 # set the searchpath for icons
180 Startup.initializeResourceSearchPath()
181
182 # generate and show a splash window, if not suppressed
183 if "--nosplash" in sys.argv and sys.argv.index("--nosplash") < ddindex:
184 del sys.argv[sys.argv.index("--nosplash")]
185 splash = NoneSplashScreen()
186 elif not Preferences.getUI("ShowSplash"):
187 splash = NoneSplashScreen()
188 else:
189 splash = SplashScreen()
190
191 pluginFile = None
192 noopen = False
193 if "--noopen" in sys.argv and sys.argv.index("--noopen") < ddindex:
194 del sys.argv[sys.argv.index("--noopen")]
195 noopen = True
196 for arg in sys.argv:
197 if arg.startswith("--plugin=") and sys.argv.index(arg) < ddindex:
198 # extract the plugin development option
199 pluginFile = arg.replace("--plugin=", "")
200 sys.argv.remove(arg)
201 pluginFile = os.path.expanduser(pluginFile)
202 pluginFile = Utilities.normabspath(pluginFile)
203 break
204
205 # is there a set of filenames or options on the command line,
206 # if so, pass them to the UI
207 if len(sys.argv) > 1:
208 args = sys.argv[1:]
209
210 # Set the applications string encoding
211 try:
212 sys.setappdefaultencoding(Preferences.getSystem("StringEncoding"))
213 except AttributeError:
214 pass
215
216 # get the Qt4 translations directory
217 qt4TransDir = Preferences.getQt4TranslationsDir()
218 if not qt4TransDir:
219 qt4TransDir = QLibraryInfo.location(QLibraryInfo.TranslationsPath)
220
221 # Load translation files and install them
222 loc = Startup.loadTranslators(qt4TransDir, app, ("qscintilla",))
223
224 QTextCodec.setCodecForCStrings(QTextCodec.codecForName(\
225 str(Preferences.getSystem("StringEncoding"))))
226
227 splash.showMessage(QApplication.translate("eric5", "Importing packages..."))
228 # We can only import these after creating the E4Application because they
229 # make Qt calls that need the E4Application to exist.
230 from UI.UserInterface import UserInterface
231
232 splash.showMessage(QApplication.translate("eric5", "Generating Main Window..."))
233 try:
234 mainWindow = UserInterface(loc, splash, pluginFile, noopen, restartArgs)
235 app.connect(app, SIGNAL("lastWindowClosed()"), app, SLOT("quit()"))
236 mainWindow.show()
237
238 QTimer.singleShot(0, uiStartUp)
239
240 # generate a graphical error handler
241 eMsg = QErrorMessage.qtHandler()
242 eMsg.setMinimumSize(600, 400)
243
244 # start the event loop
245 res = app.exec_()
246 logging.debug("Shutting down, result %d" % res)
247 logging.shutdown()
248 sys.exit(res)
249 except Exception as err:
250 raise err
251
252 if __name__ == '__main__':
253 main()

eric ide

mercurial