-
Notifications
You must be signed in to change notification settings - Fork 356
/
swagger.py
75 lines (62 loc) · 1.79 KB
/
swagger.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
import json
import os
import sys
def capitalize(s: str) -> str:
if len(s) <= 1:
return s.title()
return s[0].upper() + s[1:]
def clean(fn: str) -> None:
with open(fn, "r") as fp:
spec = json.load(fp)
# Add tag descriptions.
spec["tags"] = [
{
"name": "Authentication",
"description": "Login and logout of the cluster",
},
{
"name": "Users",
"description": "Manage users",
},
{
"name": "Cluster",
"description": "Manage cluster components",
},
{
"name": "Experiments",
"description": "Manage experiments",
},
{
"name": "Templates",
"description": "Manage templates",
},
{
"name": "Models",
"description": "Manage models",
}
]
# Update path names to be consistent.
paths = {}
for key, value in spec["paths"].items():
paths[key.replace(".", "_")] = value
spec["paths"] = paths
del spec["definitions"]["protobufFieldMask"]
for key, value in spec["definitions"].items():
# Remove definitions that should be hidden from the user.
if key == "protobufAny":
value["title"] = "Object"
elif key == "protobufNullValue":
value["title"] = "NullValue"
# Clean up titles.
if "title" not in value:
value["title"] = "".join(capitalize(k) for k in key.split(sep="v1"))
with open(fn, "w") as fp:
json.dump(spec, fp)
def main() -> None:
files = []
for r, d, f in os.walk(sys.argv[1]):
for file in f:
if file.endswith(".json"):
clean(os.path.join(r, file))
if __name__ == '__main__':
main()