-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathZanataArgParser.py
executable file
·435 lines (382 loc) · 16.1 KB
/
ZanataArgParser.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
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#!/usr/bin/env python
"""ZanataArgParser is an sub-class of ArgumentParser
that handles sub-parser, environments more easily.
This also handles logging with color format.
The color formattting part is borrowed from KurtJacobson's colored_log.py
https://gist.github.com/KurtJacobson/c87425ad8db411c73c6359933e5db9f9"""
from __future__ import (absolute_import, division, print_function)
import inspect
import logging
import os
import re
import sys
from argparse import ArgumentParser, ArgumentError, RawDescriptionHelpFormatter
# Following are for mypy
from argparse import Action # noqa: F401 # pylint: disable=W0611
from argparse import Namespace # noqa: F401 # pylint: disable=W0611
from argparse import _SubParsersAction # noqa: F401 # pylint: disable=W0611
try:
from typing import List, Any # noqa: F401 # pylint: disable=W0611
from typing import Dict # noqa: F401 # pylint: disable=W0611
from typing import Optional # noqa: F401 # pylint: disable=W0611
from typing import Tuple # noqa: F401 # pylint: disable=W0611
except ImportError:
sys.stderr.write("python typing module is not installed" + os.linesep)
class NoSuchMethodError(Exception):
"""Method does not exist
Args:
method_name (str): Name of the method
"""
def __init__(self, method_name):
super(NoSuchMethodError, self).__init__()
self.method_name = method_name
def __str__(self):
return "No such method: %s" % self.method_name
class ColoredFormatter(logging.Formatter):
"""Log colored formated
Inspired from KurtJacobson's colored_log.py"""
# Background ASCII color
bg = os.getenv("LOGGING_BG_COLOR", 40) # Default black background
COLOR_MAPPING = {
'DEBUG': [os.getenv("LOGGING_DEBUG_COLOR", 37), bg], # white
'INFO': [os.getenv("LOGGING_INFO_COLOR", 36), bg], # cyan
'WARNING': [os.getenv("LOGGING_WARNING_COLOR", 33), bg], # yellow
'ERROR': [os.getenv("LOGGING_ERROR_COLOR", 31), bg], # red
'CRITICAL': [37, 41]} # white on red bg
PREFIX = '\033['
SUFFIX = '\033[0m'
def __init__(self, patern):
logging.Formatter.__init__(self, patern)
@staticmethod
def _color(colors, content):
if os.getenv("LOGGING_NO_COLOR", ""):
return ""
return "\033[%d;%dm%s\033[0m" % (colors[0], colors[1], content)
def format(self, record):
if not os.getenv('LOGGING_NO_COLOR', ''):
# Turn of color with env LOGGING_NO_COLOR=1
colors = ColoredFormatter.COLOR_MAPPING.get(
record.levelname, ColoredFormatter.COLOR_MAPPING['DEBUG'])
record.levelname = ColoredFormatter._color(
colors, record.levelname)
record.message = ColoredFormatter._color(
colors, record.getMessage())
if self.usesTime():
record.asctime = ColoredFormatter._color(
colors, self.formatTime(record, self.datefmt))
try:
s = self._fmt % record.__dict__
except UnicodeDecodeError as e:
# Issue 25664. The logger name may be Unicode. Try again ...
try:
record.name = record.name.decode('utf-8')
s = self._fmt % record.__dict__
except UnicodeDecodeError:
raise e
if record.exc_info:
# Cache the traceback text to avoid converting it multiple times
# (it's constant anyway)
if not record.exc_text:
record.exc_text = self.formatException(record.exc_info)
if record.exc_text:
if s[-1:] != "\n":
s = s + "\n"
try:
s = s + record.exc_text
except UnicodeError:
# Sometimes filenames have non-ASCII chars, which can lead
# to errors when s is Unicode and record.exc_text is str
# See issue 8924.
# We also use replace for when there are multiple
# encodings, e.g. UTF-8 for the filesystem and latin-1
# for a script. See issue 13232.
s = s + record.exc_text.decode(sys.getfilesystemencoding(),
'replace')
return s
class ZanataArgParser(ArgumentParser):
"""Zanata Argument Parser that support sub-commands and environment
Examples:
>>> import ZanataArgParser
>>> parser = ZanataArgParser.ZanataArgParser('my-prog')
>>> parser.add_common_argument('-b', '--branch', default='master')
>>> parser.add_sub_command('list', None, None)
>>> args = parser.parse_all(['list', '-b', 'release'])
>>> print(args.sub_command)
list
>>> print(args.branch)
release
>>> args2 = parser.parse_all(['list'])
>>> print(args2.branch)
master
"""
def __init__(self, *args, **kwargs):
# type: (Any, Any) -> None
# Ignore mypy "ArgumentParser" gets multiple values for keyword
# argument "formatter_class"
# See https://github.com/python/mypy/issues/1028
super(ZanataArgParser, self).__init__(
*args, formatter_class=RawDescriptionHelpFormatter, **kwargs)
self.env_def = {} # type: Dict[str, dict]
self.parent_parser = ArgumentParser(add_help=False)
self.add_argument(
'-v', '--verbose', type=str, default='INFO',
metavar='VERBOSE_LEVEL',
help='Valid values: %s'
% 'DEBUG, INFO, WARNING, ERROR, CRITICAL, NONE')
self.sub_parsers = None # type: _SubParsersAction
self.sub_command_obj_dict = {} # type: Dict[str, Any]
def add_common_argument(self, *args, **kwargs):
# type: (Any, Any) -> None
"""Add a common argument that will be used in all sub commands
In other words, common argument will be put in common parser.
Note that add_common_argument must be put in then front of
add_sub_command that uses common arguments."""
self.parent_parser.add_argument(*args, **kwargs)
def add_sub_command(self, name, arguments, obj=None, **kwargs):
# type: (str, List, Any, Any) -> None
"""Add a sub command
Args:
name (str): name of the sub-command
arguments (dict): argments to be passed to argparse.add_argument()
obj (Any, optional): Defaults to None. The sub_command is
a method of the obj.
kwargs (Any, optional): other arguments for add_parser
"""
if not self.sub_parsers:
self.sub_parsers = self.add_subparsers(
title='Command', description='Valid commands',
help='Command help')
if obj:
self.sub_command_obj_dict[name] = obj
if 'parents' in kwargs:
kwargs['parents'] += [self.parent_parser]
else:
kwargs['parents'] = [self.parent_parser]
anonymous_parser = self.sub_parsers.add_parser(
name, **kwargs)
if arguments:
for arg in arguments:
k = arg[0]
v = arg[1]
if v:
anonymous_parser.add_argument(*k.split(), **v)
else:
anonymous_parser.add_argument(*k.split())
anonymous_parser.set_defaults(sub_command=name)
def add_env( # pylint: disable=too-many-arguments
self, env_name,
default=None,
required=False,
value_type=str,
dest=None,
sub_commands=None):
# type: (str, object, bool, type, str, List[str]) -> None
"""Add environment variable
env_name: Environment variable name
default: Default value
value_type: type of value e.g. str
dest: attribute name to be return by parse_*
sub_commands: List of subcommands that use this environment"""
if not dest:
dest = env_name.lower()
if env_name in self.env_def:
raise ArgumentError(
None, "Duplicate environment name %s" % env_name)
self.env_def[env_name] = {
'default': default,
'required': required,
'value_type': value_type,
'dest': dest,
'sub_commands': sub_commands}
def add_methods_as_sub_commands(self, obj, name_pattern='.*'):
# type (Any, str) -> None
"""Add public methods as sub-commands
Args:
cls ([type]): Public methods of obj will be used
name_pattern (str, optional): Defaults to '.*'.
Method name should match the pattern.
"""
method_list = inspect.getmembers(obj)
for m in method_list:
if not re.match(name_pattern, m[0]):
continue
name = m[0]
m_obj = m[1]
if name[0] == '_':
# No private functions (which start with _)
continue
if not inspect.ismethod(m_obj) and not inspect.isfunction(m_obj):
continue
if name == 'init_from_parsed_args':
# init_from_parsed_args initialize from parsed args.
# No need to be in sub-commands
continue
argspec = inspect.getargspec(m_obj)
sub_args = None
try:
start_idx = len(argspec.args) - len(argspec.defaults)
except TypeError:
start_idx = len(argspec.args) + 1
for idx, a in enumerate(argspec.args):
if a == 'self' or a == 'cls':
continue
if argspec.defaults and idx >= start_idx:
arg_def = {
'nargs': '?',
'default': argspec.defaults[idx - start_idx]}
else:
arg_def = None
if sub_args:
sub_args.append(tuple([a, arg_def]))
else:
sub_args = [tuple([a, arg_def])]
self.add_sub_command(
name,
sub_args,
obj,
help=re.sub(
"\n.*$", "", m_obj.__doc__, flags=re.MULTILINE),
description=m_obj.__doc__)
def has_common_argument(self, option_string=None, dest=None):
# type: (str, str) -> bool
"""Has the parser defined this argument as a common argument?
Either specify option_string or dest
option_string: option in command line. e.g. -i
dest: attribute name to be return by parse_*"""
for action in self.parent_parser._actions: # pylint: disable=W0212
if option_string:
if option_string in action.option_strings:
return True
else:
continue
elif dest:
if dest == action.dest:
return True
else:
continue
else:
raise ArgumentError(None, "need either option_string or dest")
return False
def has_env(self, env_name):
# type: (str) -> bool
"""Whether this parser parses this environment"""
return env_name in self.env_def
@staticmethod
def set_logger(verbose):
# type: (str) -> None
"""Handle logger
Inspired from KurtJacobson's colored_log.py"""
# Root logger will be fine
logger = logging.getLogger()
# Add console handler
s_handler = logging.StreamHandler()
s_handler.setLevel(logging.DEBUG)
c_formatter = ColoredFormatter(
'%(asctime)-15s [%(levelname)s] %(message)s')
s_handler.setFormatter(c_formatter)
logger.addHandler(s_handler)
if verbose == 'NONE':
# Not showing any log
logger.setLevel(logging.CRITICAL + 1)
elif hasattr(logging, verbose):
logger.setLevel(getattr(logging, verbose))
else:
ArgumentError(None, "Invalid verbose level: %s" % verbose)
def parse_args(self, args=None, namespace=None):
# type: (Any, Any) -> Namespace
"""Parse arguments"""
result = super(ZanataArgParser, self).parse_args(args, namespace)
ZanataArgParser.set_logger(result.verbose)
# We do not need verbose for the caller
delattr(result, 'verbose')
return result
@staticmethod
def _is_env_valid(env_name, env_value, env_data, args):
# type (str, str, dict, argparse.Namespace) -> bool
"""The invalid env should be skipped or raise error"""
# Skip when the env is NOT in the list of supported sub-commands
if env_data['sub_commands'] and args and hasattr(args, 'sub_command'):
if args.sub_command not in env_data['sub_commands']:
return False
# Check whether the env_value is valid
if not env_value:
if env_data['required']:
# missing required value
raise AssertionError("Missing environment '%s'" % env_name)
elif not env_data['default']:
# no default value
return False
return True
def parse_env(self, args=None):
# type: (Namespace) -> dict
"""Parse environment"""
result = {}
for env_name in self.env_def:
env_data = self.env_def[env_name]
env_value = os.environ.get(env_name)
try:
if not ZanataArgParser._is_env_valid(
env_name, env_value, env_data, args):
continue
except AssertionError as e:
raise e
if not env_value:
if env_data['required']:
raise AssertionError("Missing environment '%s'" % env_name)
elif not env_data['default']:
continue
else:
env_value = env_data['default']
result[env_data['dest']] = env_value
return result
def parse_all(self, args=None, namespace=None):
# type: (List, Namespace) -> Namespace
"""Parse arguments and environment"""
result = self.parse_args(args, namespace)
env_dict = self.parse_env(result)
for k, v in env_dict.iteritems(): # pylint: disable=no-member
setattr(result, k, v)
return result
def run_sub_command(self, args=None):
"""Run the sub ccommand with parsed arguments
Args:
instance ([type]): [description]
args ([type], optional): Defaults to None. Arguments
Raises:
ArgumentError: When sub_command is missing
"""
if not args.sub_command:
raise ArgumentError(args, "Missing sub-command")
if args.sub_command not in self.sub_command_obj_dict:
raise ArgumentError(
args,
"sub-command %s is not associated with any object" %
args.sub_command)
obj = self.sub_command_obj_dict[args.sub_command]
if inspect.isclass(obj):
cls = obj
if not hasattr(cls, 'init_from_parsed_args'):
raise NoSuchMethodError('init_from_parsed_args')
# New an object accordingto args
obj = getattr(cls, 'init_from_parsed_args')(args)
sub_cmd_obj = getattr(obj, args.sub_command)
argspec = inspect.getargspec(sub_cmd_obj)
arg_values = []
for a in argspec.args:
if a == 'self' or a == 'cls':
continue
arg_values.append(getattr(args, a))
return sub_cmd_obj(*arg_values)
if __name__ == '__main__':
if os.getenv("PY_DOCTEST", "0") == "1":
import doctest
test_result = doctest.testmod()
print(doctest.testmod(), file=sys.stderr)
sys.exit(0 if test_result.failed == 0 else 1)
print("Legend of log levels", file=sys.stderr)
ZanataArgParser('parser').parse_args(["-v", "DEBUG"])
logging.debug("debug")
logging.info("info")
logging.warning("warning")
logging.error("error")
logging.critical("critical")