-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
388 lines (303 loc) · 12.2 KB
/
app.py
File metadata and controls
388 lines (303 loc) · 12.2 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# -*- coding: utf-8 -*-
"""
Created on Thu May 12 16:07:25 2016
@author: Zahari Kassabov
Functions to drive reportengine, and report errors properly.
"""
import sys
import logging
import contextlib
import argparse
import inspect
import pathlib
import tempfile
import traceback
import os
import importlib
from reportengine.resourcebuilder import ResourceBuilder, ResourceError
from reportengine.configparser import ConfigError, Config
from reportengine.environment import Environment, EnvironmentError_
from reportengine.baseexceptions import ErrorWithAlternatives
from reportengine.utils import get_providers, import_path
from reportengine import colors
from reportengine import helputils
log = logging.getLogger(__name__)
root_log = logging.getLogger()
class ArgumentHelpAction(argparse.Action):
def __init__(self,
option_strings,
dest=argparse.SUPPRESS,
default=argparse.SUPPRESS,
app = None,
help=None):
self.app = app
super().__init__(
option_strings=option_strings,
dest=dest,
default=default,
nargs='?',
help=help)
def __call__(self, parser, namespace, values, option_string=None):
if not values:
parser.print_help()
parser.exit()
return
#Need to initialize the matplotlib babkend before printing
#help to avoid pulling QT and such when pyplot is imported anyewhere.
self.app.init_style({})
if values=='config':
print(helputils.format_config(self.app.config_class))
print("\n")
print(helputils.format_environment(self.app.environment_class))
elif values in self.app.default_provider_names:
module = importlib.import_module(values)
print(helputils.format_providermodule(module))
else:
#TODO: This is ugly as hell
providermods = self.app.load_providers()
rb = ResourceBuilder(self.app.config_class({}),
providermods,
[])
try:
providertree = rb.explain_provider(values)
except AttributeError:
alternatives = ['config', *self.app.default_provider_names]
alternatives += [f for mod in providermods
for f in get_providers(mod).keys()
]
msg = "No help available for %s" % values
print(ErrorWithAlternatives(msg, values, alternatives),
file=sys.stderr)
else:
print(helputils.print_providertree(providertree,
environ_class=self.app.environment_class))
parser.exit()
class ArgparserWithProviderDescription(argparse.ArgumentParser):
def __init__(self, providers, *args, **kwargs):
self.__providers = providers
self.__text_description = ''
super().__init__(*args, **kwargs)
@property
def description(self):
modules = '\n'.join(' - %s' % provider for provider in self.__providers)
provider_description = (
"""The installed provider modules are:
{modules}
Use {t.bold}{prog} --help{t.normal} {t.blue}<provider module>{t.normal} to get specific information about actions
in the module.
Use {t.bold}{prog} --help{t.normal} {t.blue}<action>{t.normal} to get specific information about the action.
Use {t.bold}{prog} --help config{t.normal} to get information on the parseable resources in the config file.
"""
).format(modules=modules, t=colors.t, prog=self.prog)
return self.__text_description + provider_description
@description.setter
def description(self, txt):
if txt:
self.__text_description = txt
else:
self.__text_description = ''
class App:
"""Class that processes the config file and drives the entire application.
It contains various hooks for concrete implementations to use."""
environment_class = Environment
config_class = Config
default_style = None
critical_message = "A critical error oucurred. It has been logged in %s"
def __init__(self, name, default_providers):
self.name = name
self.default_providers = default_providers
@property
def default_provider_names(self):
return [getattr(p,__name__,p) for p in self.default_providers]
@property
def argparser(self):
parser = ArgparserWithProviderDescription(self.default_providers,
epilog="A reportengine application",
formatter_class=argparse.RawDescriptionHelpFormatter,
add_help=False
)
parser.add_argument('config_yml',
help = "path to the configuration file")
parser.add_argument('-o','--output', help="output folder where to "
"store resulting plots and tables",
default='output')
loglevel = parser.add_mutually_exclusive_group()
loglevel.add_argument('-q','--quiet', help="supress INFO messages",
action='store_true')
loglevel.add_argument('-d', '--debug', help = "show debug info",
action='store_true')
parser.add_argument('--style',
help='matplotlib style file to override the built-in one.',
default=None)
parser.add_argument('--formats', nargs='+', help="formats of the output figures",
default=('png', 'pdf',))
parser.add_argument('-x', '--extra-providers', nargs='+',
help="Python files to load additional providers from")
parallel = parser.add_mutually_exclusive_group()
parallel.add_argument('--parallel', action='store_true',
help="execute actions in parallel")
parallel.add_argument('--no-parrallel', dest='parallel',
action='store_false')
parser.add_argument('-h', '--help', action=ArgumentHelpAction,
app=self)
return parser
def init_providers(self, args):
extra_provider_names = args['extra_providers']
try:
extra_providers = [import_path(p) for p in extra_provider_names]
except FileNotFoundError as e:
log.error(f"Could not import extra provider: No such file '{e}'.")
sys.exit(1)
except BaseException as e:
log.error("Error importing extra provider")
print(colors.color_exception(type(e), e, e.__traceback__), file=sys.stderr)
sys.exit(1)
maybe_names = reversed(self.default_providers + extra_providers)
providers = self.load_providers(maybe_names)
self.providers = providers
def load_providers(self, maybe_names=None):
if maybe_names is None:
maybe_names = self.default_providers
providers = []
for mod in maybe_names:
if isinstance(mod, str):
try:
mod = importlib.import_module(mod)
except ImportError as e:
log.error("Could not import module %s", mod)
#This *should* be a critical error.
raise
providers.append(mod)
return providers
def get_commandline_arguments(self):
args = vars(self.argparser.parse_args())
if args.get('quiet', False):
level = logging.WARN
elif args.get('debug', False):
level = logging.DEBUG
else:
level = logging.INFO
args['loglevel'] = level
args['this_folder'] = self.this_folder()
return args
@classmethod
def this_folder(cls):
try:
p = inspect.getfile(cls)
except TypeError: #__main__ module
return pathlib.Path('.')
return pathlib.Path(p).parent
def excepthook(self, etype, evalue, tb):
print("\n----\n")
print(colors.color_exception(etype, evalue, tb), file=sys.stderr)
print("----\n")
fd,name = tempfile.mkstemp(prefix=self.name + '-crash-', text=True)
with os.fdopen(fd, 'w') as f:
traceback.print_exception(etype, evalue, tb, file=f)
root_log.critical(self.critical_message, colors.t.blue(name))
def init_logging(self, args):
root_log.setLevel(args['loglevel'])
root_log.addHandler(colors.ColorHandler())
def init_style(self, args):
#Delay expensive imports
import matplotlib
#This avoids interacting with QT which we don't need here.
#DO NOT remove this unless you know Qt to work properly with LHAPDF.
matplotlib.use('Agg')
import matplotlib.pyplot as plt
if args.get('style', False):
try:
plt.style.use(args['style'])
except Exception as e:
log.error("There was a problem reading the supplied style: %s" %e,
)
sys.exit(1)
elif self.default_style:
plt.style.use(self.default_style)
def init(self):
import faulthandler
faulthandler.enable()
args = self.get_commandline_arguments()
self.init_logging(args)
sys.excepthook = self.excepthook
try:
self.environment = self.make_environment(args)
except EnvironmentError_ as e:
traceback_if_debug(e)
log.error(e)
sys.exit(1)
self.init_style(args)
self.init_providers(args)
self.args = args
def run(self):
args = self.args
environment = self.environment
parallel = args['parallel']
config_file = args['config_yml']
try:
with open(config_file) as f:
try:
c = self.config_class.from_yaml(f, environment=environment)
except ConfigError as e:
format_rich_error(e)
sys.exit(1)
except OSError as e:
log.error("Could not open configuration file: %s" % e)
sys.exit(1)
try:
self.environment.init_output()
except EnvironmentError_ as e:
log.error(f"Could not initialize output folder: {e}")
sys.exit(1)
try:
actions = c.parse_actions_(c['actions_'])
except ConfigError as e:
format_rich_error(e)
sys.exit(1)
except KeyError as e:
log.error("A key 'actions_' is needed in the top level of the config file.")
sys.exit(1)
providers = self.providers
rb = ResourceBuilder(c, providers, actions, environment=self.environment)
rb.rootns.update(self.environment.ns_dump())
try:
rb.resolve_fuzzytargets()
except ConfigError as e:
format_rich_error(e)
sys.exit(1)
except ResourceError as e:
with contextlib.redirect_stdout(sys.stderr):
log.error("Cannot process a resource:")
print(e)
sys.exit(1)
log.info("All requirements processed and checked successfully. "
"Executing actions.")
if parallel:
rb.execute_parallel()
else:
rb.execute_sequential()
def make_environment(self, args):
env = self.environment_class(**args)
return env
def main(self):
try:
self.init()
self.run()
except KeyboardInterrupt as e:
print(colors.t.bold_red("\nInterrupted by user. Exiting."), file=sys.stderr)
traceback_if_debug(e)
exit(1)
def traceback_if_debug(e):
if log.isEnabledFor(logging.DEBUG):
log.debug("The traceback of the exception below is:\n%s",
colors.color_exception(type(e), e, e.__traceback__))
def format_rich_error(e):
with contextlib.redirect_stdout(sys.stderr):
log.error("Bad configuration encountered:")
traceback_if_debug(e)
print(e)
def main():
a = App()
a.main()
if __name__ == '__main__':
main()