Skip to content

Commit

Permalink
chore: schemastore conversion script
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Schreiner <[email protected]>
  • Loading branch information
henryiii committed Oct 13, 2023
1 parent 57fe0d1 commit f49bb92
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions tools/to_schemastore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env python3

import argparse
import json


def convert_tree(tree: dict[str, object]) -> None:
for key, value in list(tree.items()):
match key, value:
case "$$description", list():
tree["description"] = " ".join(value)
del tree["$$description"]
case "$schema", "http://json-schema.org/draft-07/schema":
tree["$schema"] = "http://json-schema.org/draft-07/schema#"
case "$id", str():
del tree["$id"]
case "format", str():
del tree["format"]
case _, dict():
convert_tree(value)
case _, [{"format": _}, {"format": _}]:
tree[key] = [tree[key][0]]
convert_tree(tree[key][0])
case _, list():
for item in value:
if isinstance(item, dict):
convert_tree(item)


if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("schema", help="JSONSchema to convert")
args = parser.parse_args()

with open(args.schema, encoding="utf-8") as f:
schema = json.load(f)

convert_tree(schema)
schema["$id"] = "https://json.schemastore.org/setuptools.json"

print(json.dumps(schema, indent=2))

0 comments on commit f49bb92

Please sign in to comment.