-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdmclient.py
More file actions
executable file
·197 lines (163 loc) · 6.29 KB
/
dmclient.py
File metadata and controls
executable file
·197 lines (163 loc) · 6.29 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/env python3.6
# dmclient.py
# Copyright (C) 2018 Alex Mair. All rights reserved.
# This file is part of dmclient.
#
# dmclient is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 2 of the License.
#
# dmclient is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with dmclient. If not, see <http://www.gnu.org/licenses/>.
#
import argparse
import faulthandler
import itertools
import json
import logging
import logging.handlers
import sys
import traceback
from core import attrs
from core.config import *
_app = None
log = None
def excepthook(type_, value, tb):
# There is much to do in this department.
# For now, we can just prevent program abort if the PyQt
# event loop thread tracebacks due to programmer error.
print("Unhandled exception:", file=sys.stderr, flush=False)
traceback.print_tb(tb)
print("{}: {}".format(type_.__name__, value), file=sys.stderr, flush=True)
class LoggerSpec:
def __init__(self, specstr):
self.name, level = specstr.split('=')
try:
self.level = getattr(logging, level.upper())
except AttributeError:
raise argparse.ArgumentError("invalid logger level `%s'".format(level))
def parse_args(argv):
parser = argparse.ArgumentParser(
prog=APP_NAME,
description=APP_DESCRIPTION,
)
parser.add_argument("--disable-oracle",
action="store_true",
help="Disable the multi-process search indexer.")
parser.add_argument("--logfile",
default=os.path.join(APP_PATH, "dmclient.log"),
help="Override default log file.",
metavar="FILE")
parser.add_argument("--log",
action="append",
help="Specify logger level.",
metavar="<log>=<level>",
dest="loggers",
type=LoggerSpec,
default=[],
nargs='+')
parser.add_argument("campaign",
nargs='?',
help="open CAMPAIGN on startup")
return parser.parse_args(argv)
def init_logging(args):
handler = logging.handlers.RotatingFileHandler(args.logfile,
maxBytes=2 * 1024 * 1024,
backupCount=8,
encoding='utf-8')
stderr_handler = logging.StreamHandler(sys.stderr)
logging.basicConfig(handlers=[handler, stderr_handler],
format="%(asctime)s %(levelname)-7s "
"%(name)-16s %(message)s",
level=logging.DEBUG if __debug__ else logging.INFO)
for logger in itertools.chain.from_iterable(args.loggers):
logging.getLogger(logger.name).setLevel(logger.level)
global log
log = logging.getLogger("dmclient")
def init_appdirs():
for dir_ in [APP_PATH, CONFIG_PATH, TMP_PATH]:
try:
os.mkdir(dir_)
except FileExistsError:
pass
except OSError as e:
raise Exception("Could not create essential foldier %s: %s" % (dir_, e))
def init_config():
try:
decoder = json.JSONDecoder()
with open(os.path.join(CONFIG_PATH, "config.json")) as f:
parsed_config = decoder.decode(f.read())
if not isinstance(parsed_config, dict):
raise ValueError("malformed config file (not a dict?)")
appconfig().update(parsed_config)
except (OSError, ValueError) as e:
log.error("failed to load config file: %s", e)
def save_config(config):
try:
encoder = json.JSONEncoder()
encoded = encoder.encode(attrs(config))
with open(os.path.join(CONFIG_PATH, "config.json"), 'w') as f:
print(encoded, file=f)
except TypeError as e:
log.fatal("*** config object appears to have been corrupted: %s", e)
except OSError as e:
log.error("failed to save config file: %s", e)
def main():
sys.excepthook = excepthook
faulthandler.enable()
delphi, app_controller = None, None
try:
args = parse_args(sys.argv[1:])
init_appdirs()
init_logging(args)
log.debug("hello, world")
init_config()
config = appconfig()
log.debug("initialise oracle zygote")
from oracle import spawn_zygote
oracle_zygote = spawn_zygote(args)
log.debug("initialise Qt")
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication
global _app
_app = QApplication(sys.argv)
_app.setApplicationName(APP_NAME)
_app.setApplicationDisplayName(APP_NAME)
_app.setWindowIcon(QIcon(APP_ICON_PATH))
_app.setOrganizationDomain(APP_URL)
_app.setApplicationVersion(APP_VERSION)
_app.setQuitOnLastWindowClosed(True)
if __debug__:
log.debug("initialise hacks")
from core.hacks import install_hacks
install_hacks()
log.debug("initialise UI")
import ui
log.debug("initialise controllers")
from core.app import AppController
app_controller = AppController(args, _app, oracle_zygote)
if args.campaign:
app_controller.load_campaign(args.campaign)
elif config.open_last_campaign and config.last_campaign_path:
app_controller.load_campaign(config.last_campaign_path)
else:
app_controller.show_new_campaign()
log.debug("Qt app exec")
_app.exec()
except Exception:
# TODO: "dmclient has encountered.." etc. etc.
traceback.print_exc(file=sys.stderr)
finally:
if app_controller:
app_controller.shutdown()
save_config(appconfig())
if log: # None if there was a CLI error
log.debug("goodbye")
logging.shutdown()
if __name__ == '__main__':
main()