-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconvert.py
executable file
·89 lines (65 loc) · 2.28 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python3
# Copyright © LFV
"""OpenAPI Specification to AsciiDoc
"""
import argparse
import json
import os
import sys
from importlib.metadata import version
from typing import TextIO, Union
if __package__ is None or len(__package__) == 0:
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
from openapi_to_asciidoc.objects import OpenApi, OpenApiSchema
else:
from openapi_to_asciidoc.objects import OpenApi, OpenApiSchema
@staticmethod
def create_directory_and_open(file_path: Union[TextIO, str]) -> TextIO:
"""
Create the directory if it doesn't exist and open the specified file.
Parameters:
- file_path (TextIO (sys.stdout) or str (path as argument on command line): The file path.
- mode (str): The mode in which the file should be opened.
Returns:
- TextIO: The opened file.
If the file path is sys.stdout, it is returned as is without attempting to create the directory.
"""
if file_path == sys.stdout:
return file_path
directory = os.path.dirname(os.path.abspath(file_path))
if not os.path.exists(directory):
os.makedirs(directory)
return open(file_path, "w")
def get_arguments():
parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter)
ver: str = "local dev" if __package__ is None else f"{version('openapi-to-asciidoc')}"
parser.add_argument("-V", "--version", action="version", version=ver)
parser.add_argument(
"-j",
"--json",
help="OpenAPI JSON Specification File (default: openapi.json)",
type=argparse.FileType("r"),
default="openapi.json",
)
parser.add_argument(
"-o",
"--output",
nargs="?",
help="Where to output result (default: stdout)",
type=lambda file: create_directory_and_open(file),
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()