[18] | 1 |
|
---|
[36] | 2 | from config import Config
|
---|
[107] | 3 | from i18n import setLanguage
|
---|
[133] | 4 | from sound import initializeSound
|
---|
[373] | 5 | from util import secondaryInstallation
|
---|
[27] | 6 |
|
---|
[29] | 7 | import os
|
---|
[36] | 8 | import 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] | 21 | instance = None
|
---|
| 22 |
|
---|
| 23 | #--------------------------------------------------------------------------------------
|
---|
| 24 |
|
---|
[36] | 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 |
|
---|
[155] | 37 | def 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] | 58 | def 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 |
|
---|
[377] | 72 | secondaryArgs = ["secondary"] if secondaryInstallation else []
|
---|
| 73 | if "usedeflang" not in sys.argv and config.setupLocale():
|
---|
| 74 | restart(["usedeflang"] + secondaryArgs)
|
---|
[155] | 75 |
|
---|
[276] | 76 | setLanguage(programDirectory, config.getLanguage())
|
---|
[373] | 77 |
|
---|
[123] | 78 | from .gui.gui import GUI
|
---|
[36] | 79 | gui = GUI(programDirectory, config)
|
---|
[373] | 80 |
|
---|
[36] | 81 | sys.stdout = StdIOHandler(gui)
|
---|
| 82 | sys.stderr = StdIOHandler(gui)
|
---|
| 83 |
|
---|
[133] | 84 | initializeSound(os.path.join(programDirectory, "sounds"))
|
---|
| 85 |
|
---|
[38] | 86 | try:
|
---|
| 87 | gui.build(programDirectory)
|
---|
[160] | 88 |
|
---|
[38] | 89 | gui.run()
|
---|
| 90 | finally:
|
---|
| 91 | gui.flushStdIO()
|
---|
| 92 | sys.stdout = sys.__stdout__
|
---|
| 93 | sys.stderr = sys.__stderr__
|
---|
[27] | 94 |
|
---|
[40] | 95 | config.save()
|
---|
| 96 |
|
---|
[38] | 97 | if gui.toRestart:
|
---|
[377] | 98 | restart(secondaryArgs)
|
---|
[27] | 99 |
|
---|
[36] | 100 | #--------------------------------------------------------------------------------------
|
---|
| 101 |
|
---|
[20] | 102 | if __name__ == "__main__":
|
---|
[182] | 103 | if instance:
|
---|
| 104 | main()
|
---|