-
Notifications
You must be signed in to change notification settings - Fork 7
/
__main__.py
71 lines (59 loc) · 2.39 KB
/
__main__.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
import argparse
import importlib
import json
import logging
import sys
from ocdskit.exceptions import CommandError
logger = logging.getLogger('ocdskit')
COMMAND_MODULES = (
'ocdskit.cli.commands.combine_record_packages',
'ocdskit.cli.commands.combine_release_packages',
'ocdskit.cli.commands.compile',
'ocdskit.cli.commands.indent',
'ocdskit.cli.commands.mapping_sheet',
'ocdskit.cli.commands.package_releases',
'ocdskit.cli.commands.schema_report',
'ocdskit.cli.commands.schema_strict',
'ocdskit.cli.commands.set_closed_codelist_enums',
'ocdskit.cli.commands.split_record_packages',
'ocdskit.cli.commands.split_release_packages',
'ocdskit.cli.commands.tabulate',
'ocdskit.cli.commands.upgrade',
'ocdskit.cli.commands.validate',
)
def main():
parser = argparse.ArgumentParser(description='Open Contracting Data Standard CLI')
parser.add_argument('--encoding', help='the file encoding')
parser.add_argument('--ascii', help='print escape sequences instead of UTF-8 characters', action='store_true')
parser.add_argument('--pretty', help='pretty print output', action='store_true')
subparsers = parser.add_subparsers(dest='subcommand')
subcommands = {}
for module in COMMAND_MODULES:
try:
command = importlib.import_module(module).Command(subparsers)
subcommands[command.name] = command
except ImportError as e:
logger.error('exception "%s" prevented loading of %s module', e, module)
args = parser.parse_args()
if args.subcommand:
command = subcommands[args.subcommand]
try:
command.args = args
try:
command.handle()
except json.decoder.JSONDecodeError as e:
raise CommandError('JSON error: {}\nIs the JSON data not line-delimited? '
'Try piping it through `jq -crM .`'.format(e))
except UnicodeDecodeError as e:
if args.encoding and args.encoding.lower() == 'iso-8859-1':
suggestion = 'utf-8'
else:
suggestion = 'iso-8859-1'
raise CommandError('encoding error: {}\nTry `--encoding {}`?'.format(e, suggestion))
except CommandError as e:
logger.critical(e)
sys.exit(1)
else:
parser.print_help()
if __name__ == '__main__':
main()