source: src/mlx/mlx.py@ 396:1ea8fd301b62

Last change on this file since 396:1ea8fd301b62 was 377:72893c2b24d9, checked in by István Váradi <ivaradi@…>, 12 years ago

Fixed the restarting (#157)

File size: 2.9 KB
Line 
1
2from config import Config
3from i18n import setLanguage
4from sound import initializeSound
5from util import secondaryInstallation
6
7import os
8import sys
9
10#--------------------------------------------------------------------------------------
11
12## @package mlx.mlx
13#
14# The main program.
15#
16# This module contains the main program of the logger as well as the \ref
17# restart "restart" handling.
18
19#--------------------------------------------------------------------------------------
20
21instance = None
22
23#--------------------------------------------------------------------------------------
24
25class StdIOHandler(object):
26 """Handler for the standard I/O messages."""
27 def __init__(self, gui):
28 """Construct the handler."""
29 self._gui = gui
30
31 def write(self, text):
32 """Write the given text into the log."""
33 self._gui.writeStdIO(text)
34
35#--------------------------------------------------------------------------------------
36
37def restart(args = []):
38 """Restart the program with the given arguments."""
39 #print "Restarting with args", args
40 programPath = os.path.join(os.path.dirname(sys.argv[0]),
41 "runmlx.exe" if os.name=="nt" else "runmlx.sh")
42 if os.name=="nt":
43 import win32api
44 try:
45 programPath = win32api.GetShortPathName(programPath)
46 except:
47 programPath = os.path.join(os.path.dirname(sys.argv[0]),
48 "runmlx.bat")
49 programPath = win32api.GetShortPathName(programPath)
50
51 args = [programPath] + args
52
53 instance.close()
54 os.execv(programPath, args)
55
56#--------------------------------------------------------------------------------------
57
58def main():
59 """The main operation of the program."""
60 from singleton import SingleInstance, raiseCallbackWrapper
61
62 global instance
63 instance = SingleInstance("mlx" + ("-secondary" if secondaryInstallation
64 else ""), raiseCallbackWrapper)
65 if not instance: return
66
67 programDirectory = os.path.dirname(sys.argv[0])
68
69 config = Config()
70 config.load()
71
72 secondaryArgs = ["secondary"] if secondaryInstallation else []
73 if "usedeflang" not in sys.argv and config.setupLocale():
74 restart(["usedeflang"] + secondaryArgs)
75
76 setLanguage(programDirectory, config.getLanguage())
77
78 from .gui.gui import GUI
79 gui = GUI(programDirectory, config)
80
81 sys.stdout = StdIOHandler(gui)
82 sys.stderr = StdIOHandler(gui)
83
84 initializeSound(os.path.join(programDirectory, "sounds"))
85
86 try:
87 gui.build(programDirectory)
88
89 gui.run()
90 finally:
91 gui.flushStdIO()
92 sys.stdout = sys.__stdout__
93 sys.stderr = sys.__stderr__
94
95 config.save()
96
97 if gui.toRestart:
98 restart(secondaryArgs)
99
100#--------------------------------------------------------------------------------------
101
102if __name__ == "__main__":
103 if instance:
104 main()
Note: See TracBrowser for help on using the repository browser.