1 |
|
---|
2 | from config import Config
|
---|
3 | from i18n import setLanguage
|
---|
4 | from sound import initializeSound
|
---|
5 | from util import secondaryInstallation
|
---|
6 |
|
---|
7 | import os
|
---|
8 | import 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 |
|
---|
21 | instance = None
|
---|
22 |
|
---|
23 | #--------------------------------------------------------------------------------------
|
---|
24 |
|
---|
25 | class 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 |
|
---|
37 | def 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 |
|
---|
58 | def 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 |
|
---|
102 | if __name__ == "__main__":
|
---|
103 | if instance:
|
---|
104 | main()
|
---|