-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcodegen.py
executable file
·59 lines (54 loc) · 1.71 KB
/
codegen.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 python3
import sys
import yaml
import os
import re
import requests
class NoAliasDumper(yaml.Dumper):
def ignore_aliases(self, data):
return True
def remove_oneof(data):
""" Removes oneOf key from a dict and recursively calls this
function on other dict values.
"""
if 'oneOf' in data:
del data['oneOf']
for key in data:
if isinstance(data[key], dict):
remove_oneof(data[key])
return data
def remove_non_json_apis(data):
""" The R openapi generator does not handle non-json responses
"""
blacklist = [
"/rest/GetFrame/{id}",
"/rest/StateGraphic/{id}"
]
paths = data['paths']
for key in blacklist:
del paths[key]
return data
def replace_description_newlines(data):
""" The way R docs and mustache templates interact
does not allow for newlines, it has to be html break tags
"""
if 'description' in data:
if isinstance(data['description'], str):
data['description'] = re.sub("\\n|\n", "\n#' ", data['description'])
for key in data:
if isinstance(data[key], dict):
replace_description_newlines(data[key])
return data
filepath = sys.argv[1]
if not os.path.exists(filepath):
response = requests.get("https://cloud.tator.io/schema")
assert response.status_code == 200
with open(filepath, 'wb') as f:
f.write(response.content)
with open(filepath, 'r') as f:
schema = yaml.load(f, Loader=yaml.FullLoader)
schema = remove_oneof(schema)
schema = remove_non_json_apis(schema)
schema = replace_description_newlines(schema)
with open(filepath, 'w') as f:
yaml.dump(schema, f, Dumper=NoAliasDumper)