-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
113 lines (85 loc) · 2.68 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
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 python3
# PYTHON_ARGCOMPLETE_OK
"""The command line interface to dryml"""
import argparse
import sys
import textwrap
from dryml import dryml_file_to_json, dryml_string_to_json
from dryml.errors import DrymlError
from dryml.version import __version__
def print_version() -> None:
print(__version__)
DRYML_DESCRIPTION = textwrap.dedent(
f"""
Converts a DRYML Document to a JSON Document.
"""
)
class LineWrapRawTextHelpFormatter(argparse.RawDescriptionHelpFormatter):
def _split_lines(self, text, width):
text = self._whitespace_matcher.sub(" ", text).strip()
return textwrap.wrap(text, width)
def run_dryml_command(args: argparse.Namespace):
setup(args)
if args.command:
if args.path:
target_json = dryml_file_to_json(args.dryml_input)
else:
target_json = dryml_string_to_json(args.dryml_input)
print(target_json)
return 0
else:
return 1
def _add_run(subparsers):
p = subparsers.add_parser(
"dryml-to-json",
formatter_class=LineWrapRawTextHelpFormatter,
help=(
"Provide the DRYML Document String or the Path to the DRYML File you want to convert to a JSON Document."
),
)
p.add_argument(
"dryml_input",
metavar="dryml-input",
help="The DRYML Document String or the Path to the DRYML File you want to convert.",
)
p.add_argument(
"--path",
"-p",
help="Indicate that the path to the DRYML File was provided in the dryml-input parameter.",
action="store_true",
)
p.set_defaults(subparser=p)
def get_command_parser():
parser = argparse.ArgumentParser(
prog="dryml",
formatter_class=LineWrapRawTextHelpFormatter,
description=DRYML_DESCRIPTION,
)
subparsers = parser.add_subparsers(
dest="command",
description="Get help for specific command:\n> dryml dryml-to-json --help",
)
_add_run(subparsers)
parser.add_argument("--version", action="store_true", help="Print version and exit")
return parser
def setup(args):
if "version" in args and args.version:
print_version()
sys.exit(0)
def cli() -> int:
"""Entry point from command line"""
try:
parser = get_command_parser()
parsed_dryml_args = parser.parse_args()
setup(parsed_dryml_args)
if not parsed_dryml_args.command:
parser.print_help()
return 1
return run_dryml_command(parsed_dryml_args)
except DrymlError as e:
print(str(e), file=sys.stderr)
return 1
except KeyboardInterrupt:
return 1
if __name__ == "__main__":
sys.exit(cli())