-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_branches_for_json_schema_draft4.py
51 lines (41 loc) · 1.57 KB
/
get_branches_for_json_schema_draft4.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
import argparse
import json
def get_branches_from_schema_recursively(schema):
branches = []
if type(schema) is not dict and type(schema) is not list:
return branches
for key, value in schema.iteritems():
v = None
if type(value) is dict:
v = value
elif type(value) is list:
v = value[0]
if v is not None:
l = get_branches_from_schema_recursively(v)
for item in l:
if key == 'properties' and schema['type'] == 'object':
branches.append(item)
elif key == 'items' and ('type' in schema.keys() and schema['type'] == 'array'):
branches.append(item)
elif key == 'anyOf' and len(schema.keys()) == 1:
branches.append(item)
else:
branches.append(key+'.'+item)
else:
branches.append(key)
return branches
if __name__=='__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--schema-draft4-file",
help="Input file with json-schema.org/draft-04/schema#", type=argparse.FileType('r'))
args = parser.parse_args()
if args.schema_draft4_file is None:
parser.print_help()
exit(1)
#get list of branches
schema = json.load(args.schema_draft4_file)
schema_branches=get_branches_from_schema_recursively(schema)
#remove exscessive last split from every branch
for item in schema_branches:
item = '.'.join(item.split('.')[:-1])
print item