Skip to content

Commit ad9d7e8

Browse files
committed
python-jsonschema#398: Added support for 'file://' URL schema on the RefResolver
1 parent 2e082b5 commit ad9d7e8

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

jsonschema/compat.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from urllib.parse import (
2323
unquote, urljoin, urlunsplit, SplitResult, urlsplit as _urlsplit
2424
)
25-
from urllib.request import pathname2url, urlopen
25+
from urllib.request import pathname2url, url2pathname, urlopen
2626
str_types = str,
2727
int_types = int,
2828
iteritems = operator.methodcaller("items")
@@ -32,7 +32,7 @@
3232
from urlparse import (
3333
urljoin, urlunsplit, SplitResult, urlsplit as _urlsplit # noqa
3434
)
35-
from urllib import pathname2url, unquote # noqa
35+
from urllib import pathname2url, url2pathname, unquote # noqa
3636
import urllib2 # noqa
3737
def urlopen(*args, **kwargs):
3838
return contextlib.closing(urllib2.urlopen(*args, **kwargs))

jsonschema/validators.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
urljoin,
2626
urlopen,
2727
urlsplit,
28+
url2pathname,
2829
)
2930

3031
# Sigh. https://gitlab.com/pycqa/flake8/issues/280
@@ -818,6 +819,13 @@ def resolve_remote(self, uri):
818819

819820
if scheme in self.handlers:
820821
result = self.handlers[scheme](uri)
822+
elif scheme in [u"file", u""]:
823+
# Resolve local files
824+
path = uri
825+
if path.startswith('file:'):
826+
path = path[len('file:'):]
827+
with open(url2pathname(path)) as _file:
828+
result = json.load(_file)
821829
elif scheme in [u"http", u"https"] and requests:
822830
# Requests has support for detecting the correct encoding of
823831
# json over http

0 commit comments

Comments
 (0)