/
compilesubs_gui.py
113 lines (82 loc) · 3.25 KB
/
compilesubs_gui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python
# Do some basic imports, and set up logging to catch ImportError.
import inspect
import locale
import logging
import os
import sys
def create_exception_handler(logger, cleanup_handler):
# The logger is protected from garbage collection by closure magic.
# Local vars referenced by inner funcs live as long as those funcs.
def handle_exception(type, value, tb):
logger.error("Uncaught exception", exc_info=(type, value, tb))
if (cleanup_handler is not None):
cleanup_handler.cleanup()
else:
sys.exit(1)
return handle_exception
self_folder = "." # This var will be used by multiple __main__ blocks.
if __name__ == "__main__":
locale.setlocale(locale.LC_ALL, "")
# Get the un-symlinked, absolute path to this module.
self_folder = os.path.realpath(os.path.abspath(os.path.split(inspect.getfile( inspect.currentframe() ))[0]))
if (self_folder not in sys.path): sys.path.insert(0, self_folder)
# Add ./lib/ to the search path to appease 3rd party libs.
lib_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile( inspect.currentframe() ))[0], "lib")))
if (lib_subfolder not in sys.path): sys.path.insert(0, lib_subfolder)
# Go to this module's dir.
os.chdir(self_folder)
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logstream_handler = logging.StreamHandler()
logstream_formatter = logging.Formatter("%(levelname)s (%(module)s): %(message)s")
logstream_handler.setFormatter(logstream_formatter)
logstream_handler.setLevel(logging.INFO)
logger.addHandler(logstream_handler)
logfile_handler = logging.FileHandler(os.path.join(self_folder, "log.txt"), mode="w")
logfile_formatter = logging.Formatter("%(asctime)s %(levelname)s (%(module)s): %(message)s", "%Y-%m-%d %H:%M:%S")
logfile_handler.setFormatter(logfile_formatter)
logger.addHandler(logfile_handler)
# wx doesn't have a better exception mechanism.
sys.excepthook = create_exception_handler(logger, None)
# __main__ stuff is continued at the end of this file.
# Import everything else (wx may be absent in some environments).
try:
from datetime import datetime, timedelta
import platform
import re
import urllib2
import wx
from lib import cleanup
from lib import common
from lib import global_config
from lib import snarkutils
from lib.gui import csgui
except (Exception) as err:
logging.exception(err)
sys.exit(1)
def main_gui():
config = None
snarks = []
logging.info("Registering ctrl-c handler.")
cleanup_handler = global_config.get_cleanup_handler()
cleanup_handler.register() # Must be called from main thread.
# Warning: If the main thread gets totally blocked, it'll never notice sigint.
sys.excepthook = create_exception_handler(logger, cleanup_handler)
try:
mygui = csgui.GuiApp(redirect=False, clearSigInt=False)
cleanup_handler.add_gui(mygui)
try:
mygui.MainLoop()
finally:
mygui.done = True
except (Exception) as err:
logging.exception(err)
finally:
cleanup_handler.cleanup()
def main():
logging.info("CompileSubs %s (on %s)" % (global_config.VERSION, platform.platform(aliased=True, terse=False)))
main_gui()
if __name__ == "__main__":
global_config._settings_directory = self_folder
main()