/
generate_graph.py
executable file
·59 lines (44 loc) · 1.71 KB
/
generate_graph.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
#!/usr/bin/env python
"""
Simple script to generate graphviz input file from datatype definitions
in OpenAPI (nee Swagger) definition. Needs pystache somewhere and of course
graphviz.
Usage:
./generate_graph.py > my_fancy_graph.gv (input file & template are hardcoded below)
dot -Tpdf -O my_fancy_graph.gv (replace pdf with png, if so desired)
"""
import yaml
import pystache
import json
import pprint
def truncword(content, length, suffix):
return content if len(content) <= length else content[:length-len(suffix)].rsplit(' ', 1)[0] + suffix
with open("resurssienvaraus.swagger.yaml") as stream:
apidoc = yaml.safe_load(stream)
with open("resurssienvaraus-graph.gvt") as stream:
template = stream.read()
defs = apidoc['definitions']
context = []
for definition in defs:
formatted = {}
# Need to replace pystache with something more capable, but too tired
formatted['name'] = definition
keys = ['title', 'description']
for k in keys:
if k in defs[definition]:
formatted[k] = defs[definition][k]
formatted['properties'] = []
for prop in defs[definition]['properties']:
property = defs[definition]['properties'][prop]
if 'title' in property:
desc = property['title']
elif 'description' in property:
desc = property['description']
else:
desc = prop
if not desc:
desc = "not described yet"
formatted['properties'].append({'name': prop, 'description': truncword(desc, 60, "...")})
context.append(formatted)
rendis = pystache.Renderer(string_encoding='utf-8', missing_tags="strict")
print rendis.render(template, {'c': context}).encode('utf-8')