Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix json schema #11

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions scripts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
venv/
.pyc
Empty file added scripts/__init__.py
Empty file.
56 changes: 56 additions & 0 deletions scripts/annoate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import json
import logging
from collections import defaultdict
from copy import deepcopy
from glob import glob
from os.path import basename, join, realpath
from pprint import pprint
from schema import Schema

import fire


def read(folder, type):
with open(f"{folder}{type[1:]}.json") as ifile:
return json.load(ifile)


def analy(root, folder="../docs/json"):
schema = Schema()
queue = [root]

while queue:
type = queue.pop()
schema.add(type, read(folder, type))

for ref in schema.search("$ref", type):
if schema.has(ref):
continue

assert isinstance(ref, str)
assert ref.startswith("#/")
queue.append(ref)

return schema


def annotate(ifilepath, opath=None, strict=False):
schema = analy("#/animation")

with open(ifilepath) as ifile:
icontent = json.load(ifile)

resolved_schema = schema.validate(icontent, "#/animation", strict=strict)
opath = opath or ifilepath.rstrip(".json") + ".schema.json"

with open(opath, "w") as ofile:
json.dump(resolved_schema, ofile, indent=2)

icontent["$schema"] = "./" + basename(opath)

with open(ifilepath, "w") as ofile:
json.dump(icontent, ofile, indent=2)


if __name__ == "__main__":
fire.Fire(annotate)
4 changes: 4 additions & 0 deletions scripts/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
jsonschema
snapshottest
pytest
fire
Loading