-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdocs.py
More file actions
122 lines (99 loc) · 3.79 KB
/
docs.py
File metadata and controls
122 lines (99 loc) · 3.79 KB
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
114
115
116
117
118
119
120
121
122
"""generate openapi docs."""
import json
import argparse
from pkg_resources import get_distribution
from pydantic_openapi_helper.core import get_openapi
from pydantic_openapi_helper.inheritance import class_mapper
from dragonfly_schema.model import Model
parser = argparse.ArgumentParser(description='Generate OpenAPI JSON schemas')
parser.add_argument('--version', help='Set the version of the new OpenAPI Schema')
args = parser.parse_args()
if args.version:
VERSION = args.version.replace('v', '')
else:
VERSION = '.'.join(get_distribution('dragonfly_schema').version.split('.')[:3])
info = {
"description": "",
"version": VERSION,
"title": "",
"contact": {
"name": "Ladybug Tools",
"email": "info@ladybug.tools",
"url": "https://github.com/ladybug-tools/dragonfly-core"
},
"x-logo": {
"url": "https://www.ladybug.tools/assets/img/dragonfly-large.png",
"altText": "Dragonfly logo"
},
"license": {
"name": "MIT",
"url": "https://github.com/ladybug-tools/dragonfly-schema/blob/master/LICENSE"
}
}
modules = [
{'module': [Model], 'name': 'Model'}
]
def _process_name(name):
"""Process module name."""
new_name = '-'.join(n.lower() for n in name.split())
return new_name
for module in modules:
# generate Recipe open api schema
print(f'Generating {module["name"]} documentation...')
external_docs = {
"description": "OpenAPI Specification with Inheritance",
"url": f"./{_process_name(module['name'])}_inheritance.json"
}
openapi = get_openapi(
module['module'],
title=f'Dragonfly {module["name"]} Schema',
description=f'Dragonfly {_process_name(module["name"])} schema.',
version=VERSION, info=info,
external_docs=external_docs
)
# set the version default key in the Model schema
if module['module'] is Model:
openapi['components']['schemas']['Model']['properties']['version']['default'] = \
VERSION
with open(f'./docs/{_process_name(module["name"])}.json', 'w') as out_file:
json.dump(openapi, out_file, indent=2)
# with inheritance
openapi = get_openapi(
module['module'],
title=f'Dragonfly {module["name"]} Schema',
description=f'Documentation for Dragonfly {_process_name(module["name"])} schema',
version=VERSION, info=info,
inheritance=True,
external_docs=external_docs
)
# set the version default key in the Recipe schema
if module['module'] is Model:
openapi['components']['schemas']['Model']['properties']['version']['default'] = \
VERSION
with open(f'./docs/{_process_name(module["name"])}_inheritance.json', 'w') \
as out_file:
json.dump(openapi, out_file, indent=2)
# add the mapper file
with open(f'./docs/{_process_name(module["name"])}_mapper.json', 'w') as out_file:
json.dump(class_mapper(module['module']), out_file, indent=2)
# generate JSONSchema for Dragonfly model
with open('./docs/model_json_schema.json', 'w') as out_file:
json.dump(Model.model_json_schema(), out_file, indent=2)
# generate schema for mode with inheritance but without descriminator
# we will use this file for generating redocly - the full model is too big, and the
# model with inheritance and discriminators is renders incorrectly
external_docs = {
"description": "OpenAPI Specification with Inheritance",
"url": "./model_inheritance.json"
}
openapi = get_openapi(
[Model],
title='Dragonfly Model Schema',
description='Documentation for Dragonfly model schema',
version=VERSION, info=info,
inheritance=True,
external_docs=external_docs,
add_discriminator=False
)
with open('./docs/model_redoc.json', 'w') as out_file:
json.dump(openapi, out_file, indent=2)