source: src/mlx/mlx.py@ 373:cf2b6b8a3973

Last change on this file since 373:cf2b6b8a3973 was 373:cf2b6b8a3973, checked in by István Váradi <ivaradi@…>, 11 years ago

Added support for a secondary instance (#157)

File size: 2.9 KB
RevLine 
[18]1
[36]2from config import Config
[107]3from i18n import setLanguage
[133]4from sound import initializeSound
[373]5from util import secondaryInstallation
[27]6
[29]7import os
[36]8import sys
[27]9
[36]10#--------------------------------------------------------------------------------------
11
[298]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
[247]21instance = None
22
23#--------------------------------------------------------------------------------------
24
[36]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
[155]37def restart(args = []):
38 """Restart the program with the given arguments."""
[247]39 #print "Restarting with args", args
[155]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
[247]53 instance.close()
[373]54 os.execv(programPath, args)
[155]55
56#--------------------------------------------------------------------------------------
57
[36]58def main():
[29]59 """The main operation of the program."""
[182]60 from singleton import SingleInstance, raiseCallbackWrapper
61
[247]62 global instance
[373]63 instance = SingleInstance("mlx" + ("-secondary" if secondaryInstallation
64 else ""), raiseCallbackWrapper)
[182]65 if not instance: return
66
[36]67 programDirectory = os.path.dirname(sys.argv[0])
68
69 config = Config()
[40]70 config.load()
[107]71
[155]72 if (len(sys.argv)<=1 or sys.argv[1]!="usedeflang") and config.setupLocale():
[373]73 restart(["usedeflang"] + (["secondary"] if secondaryInstallation else []))
[155]74
[276]75 setLanguage(programDirectory, config.getLanguage())
[373]76
[123]77 from .gui.gui import GUI
[36]78 gui = GUI(programDirectory, config)
[373]79
[36]80 sys.stdout = StdIOHandler(gui)
81 sys.stderr = StdIOHandler(gui)
82
[133]83 initializeSound(os.path.join(programDirectory, "sounds"))
84
[38]85 try:
86 gui.build(programDirectory)
[160]87
[38]88 gui.run()
89 finally:
90 gui.flushStdIO()
91 sys.stdout = sys.__stdout__
92 sys.stderr = sys.__stderr__
[27]93
[40]94 config.save()
95
[38]96 if gui.toRestart:
[155]97 restart()
[27]98
[36]99#--------------------------------------------------------------------------------------
100
[20]101if __name__ == "__main__":
[182]102 if instance:
103 main()
Note: See TracBrowser for help on using the repository browser.