1 | # The main program
|
---|
2 |
|
---|
3 | from config import Config
|
---|
4 | from i18n import setLanguage
|
---|
5 | from sound import initializeSound
|
---|
6 |
|
---|
7 | import os
|
---|
8 | import sys
|
---|
9 |
|
---|
10 | #--------------------------------------------------------------------------------------
|
---|
11 |
|
---|
12 | instance = None
|
---|
13 |
|
---|
14 | #--------------------------------------------------------------------------------------
|
---|
15 |
|
---|
16 | class 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 |
|
---|
28 | def 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 |
|
---|
49 | def 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 |
|
---|
91 | if __name__ == "__main__":
|
---|
92 | if instance:
|
---|
93 | main()
|
---|