Skip to content

Commit b1d7b69

Browse files
author
Peter Amstutz
committed
Support http/https existence check (using HTTP HEAD) in check_file.
1 parent fefc957 commit b1d7b69

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

schema_salad/ref_resolver.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -661,12 +661,21 @@ def fetch(self, url, inject_ids=True): # type: (unicode, bool) -> Any
661661
self.idx[url] = result
662662
return result
663663

664-
def check_file(self, fn): # type: (unicode) -> bool
665-
if fn.startswith("file://"):
666-
u = urlparse.urlsplit(fn)
667-
return os.path.exists(u.path)
664+
def check_file(self, url): # type: (unicode) -> bool
665+
split = urlparse.urlsplit(url)
666+
scheme, path = split.scheme, split.path
667+
668+
if scheme in [u'http', u'https'] and self.session:
669+
try:
670+
resp = self.session.head(url)
671+
resp.raise_for_status()
672+
except Exception as e:
673+
return False
674+
return True
675+
elif scheme == 'file':
676+
return os.path.exists(path)
668677
else:
669-
return False
678+
raise ValueError('Unsupported scheme in url: %s' % url)
670679

671680
FieldType = TypeVar('FieldType', unicode, List[unicode], Dict[unicode, Any])
672681

0 commit comments

Comments
 (0)