Repository navigation
Expand file tree
/
Copy path229P-Parser.py
More file actions
70 lines (53 loc) · 2.03 KB
/
Copy path229P-Parser.py
File metadata and controls
70 lines (53 loc) · 2.03 KB
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
import json
from pprint import pprint
from tqdm import tqdm
referenced_file_names = [
"ASHRAE229.schema.json",
"Enumerations2019ASHRAE901.schema.json",
"Output2019ASHRAE901.schema.json",
"Enumerations2019T24.schema.json",
]
referenced_jsons_by_file_name = {}
for file_name in referenced_file_names:
with open("./docs229/" + file_name, 'r') as file:
referenced_jsons_by_file_name[file_name] = json.load(file)
def dereference(json_path):
"""Undo the references in the json"""
[file_name, json_path] = json_path.split("#")
referenced_json = referenced_jsons_by_file_name[file_name]
json_path = json_path.split("/")[1:]
def list_get(j, keys):
if len(keys) == 0:
return j
return list_get(j.get(keys[0]), keys[1:])
return list_get(referenced_json, json_path)
def build_definitions(name, schema_dict, definitions={}, tree=""):
# do any dereferencing
if "$ref" in schema_dict:
schema_dict.update(dereference(schema_dict["$ref"]))
# add base definitions
if "enum" in schema_dict:
for enum, description in zip(schema_dict["enum"], schema_dict["descriptions"]):
definitions[tree + "/" + name + "#" + enum]= {
"name": enum,
"description": description,
"tree": tree + "/" + name,
}
else:
definitions[tree + "/" + name]= {
"name": name,
"description": schema_dict.get("description", ""),
"tree": tree + "/" + name,
}
# build children definitions
properties = schema_dict.get("properties", {})
for key, value in properties.items():
if isinstance(value, dict):
build_definitions(key, value, definitions, tree= tree + "/" + name)
return definitions
# do the thing
schema = referenced_jsons_by_file_name["ASHRAE229.schema.json"]
schema_definitions = {}
for key, value in tqdm(schema["definitions"].items()):
schema_definitions.update(build_definitions(key, value))
pprint(schema_definitions)