source: src/mlx/mlx.py@ 275:155f94c971e0

Last change on this file since 275:155f94c971e0 was 247:12a62baf908f, checked in by István Váradi <ivaradi@…>, 12 years ago

Hopefully fixed the problem of not restarting properly

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