import glob | |
import json | |
import os | |
import sys | |
DEFN_KEYWORDS = [ | |
"definitions", | |
"$defs", | |
] | |
REMOVE_KEYS = [ | |
"$comment", | |
"$id", | |
"$ref", | |
"Description", | |
"description", | |
"markdownDescription", | |
"title", | |
] | |
dict_values = type({}.values()) | |
def clean_object(obj): | |
if isinstance(obj, dict): | |
return {k: clean_object(v) for k, v in obj.items() if k not in REMOVE_KEYS} | |
elif isinstance(obj, list): | |
return [clean_object(v) for v in obj] | |
else: | |
return obj | |
def get_definitions(obj): | |
for key in DEFN_KEYWORDS: | |
for name, defn in obj.get(key, {}).items(): | |
if '"$ref"' not in json.dumps(defn): | |
yield name, defn | |
for line in sys.stdin: | |
obj = json.loads(line) | |
schema = obj["name"] | |
obj = json.loads(obj["content"]) | |
for key, defn in get_definitions(obj): | |
json.dump( | |
{ | |
"schema": os.path.basename(schema), | |
"key": key, | |
"definition": json.dumps(clean_object(defn)) | |
}, | |
sys.stdout, | |
) | |
sys.stdout.write("\n") | |