-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: schemastore conversion script
Signed-off-by: Henry Schreiner <[email protected]>
- Loading branch information
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |