forked from luftfartsverket/openapi-to-asciidoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.py
executable file
·66 lines (49 loc) · 1.39 KB
/
convert.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
#!/usr/bin/env python3
# Copyright © LFV
"""OpenAPI Specification to AsciiDoc
"""
import argparse
import json
import sys
from openapi_to_asciidoc.objects import OpenApi, OpenApiSchema
def get_arguments():
"""
Parses command line arguments
"""
usage = """
Usage:
Use stdin and stdout:
cat openapi.json | openapi_to_asciidoc.py -t openapi.j2
Use specific files for input and output:
openapi_to_asciidoc.py -j openapi.json -t openapi.j2 -o openapi.adoc
"""
parser = argparse.ArgumentParser(epilog=usage, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument(
"-j",
"--json",
help="OpenAPI JSON Specification File (default: stdin)",
type=argparse.FileType("r"),
default=sys.stdin,
)
parser.add_argument(
"-o",
"--output",
nargs="?",
help="Where to output result (default: stdout)",
type=argparse.FileType("w"),
default=sys.stdout,
)
return parser.parse_args()
def main():
args = get_arguments()
js_input = args.json
output = args.output
# read openapi json input
openapi = json.load(js_input)
# render template from schema
openapi_schema = OpenApiSchema()
schema: OpenApi = openapi_schema.load(openapi)
# output rendered result
output.write(schema.result)
if __name__ == "__main__":
main()