-
Notifications
You must be signed in to change notification settings - Fork 0
/
schemasplit.py
59 lines (41 loc) · 1.58 KB
/
schemasplit.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
"""Split JSON schema in YAML into JSON files, one per subschema"""
import argparse
import json
import logging
from copy import deepcopy
from pathlib import Path
import yaml
def main():
"""Main function"""
parser = argparse.ArgumentParser(description="Schema splitter")
parser.add_argument("schema", metavar="filename")
args = parser.parse_args()
logging.basicConfig(level=logging.INFO)
with open(args.schema) as fp:
schema = yaml.safe_load(fp)
logging.info("Read %s", args.schema)
subschemas = schema.get("anyOf", [])
subschemas_names = []
for subschema in subschemas:
subschemas_names.append(subschema["$ref"].split("/")[-1])
for subschema in subschemas:
subschema_name = subschema["$ref"].split("/")[-1]
logging.info("Processing %s", subschema_name)
schema2 = deepcopy(schema)
schema2["properties"].update(schema["$defs"][subschema_name]["properties"])
schema2["required"] = list(
set(schema2["required"]) | set(schema["$defs"][subschema_name]["required"])
)
del schema2["anyOf"]
for n in subschemas_names:
del schema2["$defs"][n]
output = Path(args.schema).stem + f"-{subschema_name}.json"
with open(output, "wt") as fp:
json.dump(schema2, fp)
logging.info("Wrote %s", output)
output = Path(args.schema).stem + f"-{subschema_name}.yaml"
with open(output, "wt") as fp:
yaml.dump(schema2, fp)
logging.info("Wrote %s", output)
if __name__ == "__main__":
main()