Skip to content

Commit

Permalink
Merge pull request #873 from sevein/dev/issue-871-importlib-resources
Browse files Browse the repository at this point in the history
Load schemas via importlib.resources
  • Loading branch information
Julian authored Nov 3, 2021
2 parents 272b4f2 + ffb5e06 commit 14b4fd7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
13 changes: 10 additions & 3 deletions jsonschema/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@
from urllib.parse import urlsplit
import itertools
import json
import pkgutil
import re
import sys

# The files() API was added in Python 3.9.
if sys.version_info >= (3, 9): # pragma: no cover
import importlib.resources as resources
else: # pragma: no cover
import importlib_resources as resources


class URIDict(MutableMapping):
Expand Down Expand Up @@ -51,8 +57,9 @@ def load_schema(name):
Load a schema from ./schemas/``name``.json and return it.
"""

data = pkgutil.get_data("jsonschema", "schemas/{0}.json".format(name))
return json.loads(data.decode("utf-8"))
path = resources.files(__package__).joinpath(f"schemas/{name}.json")
data = path.read_text(encoding="utf-8")
return json.loads(data)


def format_as_index(container, indices):
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ python_requires = >=3.7
install_requires =
attrs>=17.4.0
importlib_metadata;python_version<'3.8'
importlib_resources;python_version<'3.9'
pyrsistent>=0.14.0,!=0.17.0,!=0.17.1,!=0.17.2

[options.extras_require]
Expand Down

0 comments on commit 14b4fd7

Please sign in to comment.