-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
90 lines (72 loc) · 3.18 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
import argparse
import pathlib
import sys
from eventdoc import pz_event_doc
def main():
arg_parser = argparse.ArgumentParser(epilog="If none of --events, --hooks, and --callbacks are set,"
"all are treated as enabled.")
arg_parser.add_argument("input", default="data.json", nargs='?',
help="The path of the JSON file containing the data.")
arg_parser.add_argument("output", default="events.lua", nargs='?',
help="The filepath to write the documented data to.")
arg_parser.add_argument("--events", action="store_true",
help="Enables documenting events.")
arg_parser.add_argument("--hooks", action="store_true",
help="Enables documenting hooks.")
arg_parser.add_argument("--callbacks", action="store_true",
help="Enables documenting callbacks.")
arg_parser.add_argument("--render_deprecated", default="false", nargs="?", const="true",
choices=["false", "true", "only"],
help="Whether to document deprecated objects.")
arg_parser.add_argument("--format",
choices=["lua", "md"], default=None,
help="Which format to document in.")
args = arg_parser.parse_args()
want_events = args.events
want_hooks = args.hooks
want_callbacks = args.callbacks
# default behaviour: if no table flags are set, all types are enabled
if not args.events and not args.hooks and not args.callbacks:
want_events = True
want_hooks = True
want_callbacks = True
want_non_deprecated = True
want_deprecated = False
output = args.output
if args.render_deprecated != "false":
want_deprecated = True
if args.render_deprecated == "only":
want_non_deprecated = False
input_path: pathlib.Path
if args.input is not None:
input_path = pathlib.Path(args.input)
else:
input_path = pathlib.Path(__file__).parent / "data.json"
if not input_path.exists() or not input_path.is_file():
print(f"Could not open input file {input_path}")
sys.exit(1)
file = input_path.open('r')
json = file.read()
file.close()
desired_format: str = args.format
if desired_format is None:
desired_format = args.output.split('.')[-1]
rendered_text = pz_event_doc.document_from_json(
json, desired_format,
want_events=want_events, want_hooks=want_hooks, want_callbacks=want_callbacks,
want_deprecated=want_deprecated, want_non_deprecated=want_non_deprecated)
if rendered_text == "":
print("Rendering failed.")
sys.exit(1)
if desired_format == "lua":
# this path could be user defined
extra_path = pathlib.Path(__file__).parent / "extra.lua"
if extra_path.exists() and extra_path.is_file():
file = extra_path.open('r')
rendered_text = file.read() + "\n" + rendered_text
file.close()
file = open(output, 'w')
file.write(rendered_text)
file.close()
if __name__ == "__main__":
main()