This repository has been archived by the owner on Jan 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate.py
151 lines (117 loc) · 5.13 KB
/
generate.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import re
import csv
import json
import collections
import jsonref
from jsonref import JsonRef
import requests
import sys
import copy
try:
r = requests.get(sys.argv[1])
release = r.json()
print("Fetched schema from URL")
except:
print("Using local release schema")
with open('release-schema.json', 'r') as f:
release = json.loads(f.read(), object_pairs_hook=collections.OrderedDict)
release = JsonRef.replace_refs(release)
print(jsonref.dumps(release, indent=3))
# Based on https://stackoverflow.com/questions/30734682/extracting-url-and-anchor-text-from-markdown-using-python
INLINE_LINK_RE = re.compile(r'\[([^\]]+)\]\(([^)]+)\)')
def find_md_links(md):
links = dict(INLINE_LINK_RE.findall(md))
return links
def remove_links(text, links):
for key, link in links.items():
text = text.replace("[" + key + "](" + link + ")", key)
return text
def display_links(links):
link_list = []
for key, link in links.items():
link_list.append(link)
return ", ".join(link_list)
def display_properties(schema, path='', section='', deprecated=''):
# Create a copy of obj, because there may be references to it from
# elsewhere in the JSON schema, and we don't want to mutate it in
# all those places
obj = copy.deepcopy(schema['properties'])
required_fields = schema['required'] if 'required' in schema else []
rows = []
for field in obj:
row = {'path': path + field, 'deprecated': deprecated}
section = row['path'].split("/")[0] if "/" in row['path'] else ""
row['section'] = section
# If there was a reference here, prefer the values from that
if hasattr(obj[field], '__reference__'):
obj[field].update(obj[field].__reference__)
row['title'] = obj[field]['title'] if 'title' in obj[field] else field + "*"
if 'description' in obj[field]:
links = find_md_links(obj[field]['description'])
row['description'] = remove_links(obj[field]['description'], links)
row['links'] = display_links(links)
# Type
if 'type' in obj[field]:
# ToDo: Add checking of the required array also.
# This checks whether this field is **implicity required**
if 'null' in obj[field]['type']:
obj[field]['type'].remove('null')
required = False
else:
if 'string' in obj[field]['type'] or 'integer' in obj[field]['type']:
required = True
else:
required = False
if type(obj[field]['type']) in (tuple, list):
row['type'] = ", ".join(obj[field]['type'])
else:
row['type'] = obj[field]['type']
else:
row['type'] = "unknown"
# Required field
if field in required_fields:
required = True
maxn = 'n' if row['type'] == "array" else '1'
minn = '1' if required else '0'
row['range'] = minn + ".." + maxn
# Format or restrictions
if 'format' in obj[field]:
row['values'] = obj[field]['format']
elif 'enum' in obj[field]:
if None in obj[field]['enum']:
obj[field]['enum'].remove(None)
row['values'] = "Codelist: " + ", ".join(obj[field]['enum'])
else:
row['values'] = ""
# Check for deprecation
if 'deprecated' in obj[field]:
row['deprecated'] = obj[field]['deprecated'].get('deprecatedVersion', '')
row['deprecationNotes'] = obj[field]['deprecated'].get('description', '')
rows.append(row)
if 'properties' in obj[field]:
rows = rows + display_properties(obj[field], path + field + "/", section, row['deprecated'])
if 'items' in obj[field]:
if 'properties' in obj[field]['items']:
if 'title' in obj[field]['items']:
if 'description' in obj[field]['items']:
rows.append({'section': section, 'path': path + field,
'title': obj[field]['items']['title'],
'description': obj[field]['items']['description'],
'type': obj[field]['items']['type']})
else:
rows.append({'section': section, 'path': path + field,
'title': obj[field]['items']['title'],
'description': "",
'type': obj[field]['items']['type']})
else:
pass
# rows.append({'section':section,'path':path + field,'title':'missing','description':'missing'})
rows = rows + display_properties(obj[field]['items'], path + field + "/", section, row['deprecated'])
return rows
schema = display_properties(release)
f = open('fields.csv', 'wt')
w = csv.DictWriter(f, ['section', 'path', 'title', 'description', 'type',
'range', 'values', 'links', 'deprecated', 'deprecationNotes'])
w.writeheader()
w.writerows(schema)
f.close()