-
Notifications
You must be signed in to change notification settings - Fork 4
/
build_schemas.py
60 lines (51 loc) · 2.6 KB
/
build_schemas.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
"""
This script builds the schemas and examples modules.
The schemas module contains all the schemas in the SCHEMAS_DIR.
The examples module contains all the examples in the EXAMPLES_DIR.
The script is aware of the include and reference statements and is using the json_include library to resolve them.
The script creates the python modules containing the schemas and examples and avoids
having the dependency on the filesystem during the calls to the ESSE class.
Usage (NOTE: "-e" in pip install is required for the file references to be resolved):
virtualenv .venv
source .venv/bin/activate
pip install -e ."[tests]"
python build_schemas.py
After that, check for the files `src/py/mat3ra/esse/data/schemas.py` and `src/py/mat3ra/esse/data/examples.py`.
"""
import os
import json
import yaml
from pathlib import Path
from mat3ra.esse.utils import parse_include_reference_statements_by_dir
TOP_DIR = os.path.dirname(__file__)
SCHEMAS = parse_include_reference_statements_by_dir(TOP_DIR)
# Wrap examples into {"data": example, "path": schema_id} format to relate them to schemas.
EXAMPLES = parse_include_reference_statements_by_dir(TOP_DIR, True)
with open(os.path.join(TOP_DIR, "manifest/properties.yaml")) as f:
PROPERTIES_MANIFEST = yaml.load(f.read(), Loader=yaml.FullLoader)
RESULTS = [k for k, v in PROPERTIES_MANIFEST.items() if v.get("isResult")]
with open("src/py/mat3ra/esse/data/schemas.py", "w") as f:
f.write("import json\n" + f"SCHEMAS = json.loads(json.dumps({SCHEMAS}))")
with open("src/py/mat3ra/esse/data/examples.py", "w") as f:
f.write("import json\n" + f"EXAMPLES = json.loads(json.dumps({EXAMPLES}))")
with open("src/py/mat3ra/esse/data/properties.py", "w") as f:
content = (
"import json\n"
+ f"PROPERTIES_MANIFEST = json.loads(json.dumps({PROPERTIES_MANIFEST}))\n"
+ f"RESULTS = json.loads(json.dumps({RESULTS}))\n"
)
f.write(content)
if os.environ.get("BUILD_DOCS") != "true":
exit(0)
for schema in SCHEMAS:
id_as_path = schema["$id"].replace("-", "_")
full_path = os.path.join(TOP_DIR, "docs/py/schema", id_as_path + ".json")
Path(full_path).parent.mkdir(parents=True, exist_ok=True)
with open(full_path, "w") as f:
f.write(json.dumps(schema, sort_keys=True, indent=4, separators=(",", ": ")))
for example in EXAMPLES:
id_as_path = example["path"].replace("-", "_")
full_path = os.path.join(TOP_DIR, "docs/py/example", id_as_path + ".json")
Path(full_path).parent.mkdir(parents=True, exist_ok=True)
with open(full_path, "w") as f:
f.write(json.dumps(example, sort_keys=True, indent=4, separators=(",", ": ")))