_core.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. """
  2. Load all the JSON Schema specification's official schemas.
  3. """
  4. import json
  5. try:
  6. from importlib.resources import files
  7. except ImportError:
  8. from importlib_resources import ( # type: ignore[import-not-found, no-redef]
  9. files,
  10. )
  11. from referencing import Resource
  12. def _schemas():
  13. """
  14. All schemas we ship.
  15. """
  16. # importlib.resources.abc.Traversal doesn't have nice ways to do this that
  17. # I'm aware of...
  18. #
  19. # It can't recurse arbitrarily, e.g. no ``.glob()``.
  20. #
  21. # So this takes some liberties given the real layout of what we ship
  22. # (only 2 levels of nesting, no directories within the second level).
  23. for version in files(__package__).joinpath("schemas").iterdir():
  24. if version.name.startswith("."):
  25. continue
  26. for child in version.iterdir():
  27. children = [child] if child.is_file() else child.iterdir()
  28. for path in children:
  29. if path.name.startswith("."):
  30. continue
  31. contents = json.loads(path.read_text(encoding="utf-8"))
  32. yield Resource.from_contents(contents)