-
Notifications
You must be signed in to change notification settings - Fork 2
/
build_docs.py
45 lines (38 loc) · 1.55 KB
/
build_docs.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
import os
import jinja2
import json
from collections import OrderedDict
def render(tpl_path, context):
path, filename = os.path.split(tpl_path)
return jinja2.Environment(
loader=jinja2.FileSystemLoader(path or './')
).get_template(filename).render(context)
schemas = [
('event', 'event.json'),
('relation', 'relation.json'),
('object', 'object.json'),
]
context = {}
for key, filename in schemas:
with open('jsonschema/' + filename, 'r') as fp:
data = json.load(fp, object_pairs_hook=OrderedDict)
required = data['required']
properties = data['properties']
for k, v in properties.items():
properties[k]['required'] = 'Yes' if k in required else 'No'
if '$ref' in v:
properties[k]['type'] = 'object ([`{0}`](#definition-{0}))'.format(v['$ref'].split('definitions/')[1])
context[key] = properties
with open('jsonschema/definitions.json', 'r') as fp:
data = json.load(fp, object_pairs_hook=OrderedDict)
definitions = data['definitions']
context['definitions'] = {}
for def_k, def_v in definitions.items():
required = def_v.get('required', [])
properties = def_v['properties']
for k, v in properties.items():
properties[k]['required'] = 'Yes' if k in required else 'No'
if '$ref' in v:
properties[k]['type'] = 'object ([`{0}`](#definition-{0}))'.format(v['$ref'].split('definitions/')[1])
context['definitions'][def_k] = properties
print(render('./README.md.template', context))