Skip to content

Commit 8752f87

Browse files
authored
Make exceptions loading extension schemas non-fatal. (#144)
* Make exceptions loading extension schemas non-fatal. Also cache results of check_exists().
1 parent 8ae7664 commit 8752f87

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

schema_salad/ref_resolver.py

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,18 @@ def urljoin(self, base_url, url): # type: (Text, Text) -> Text
120120

121121
class DefaultFetcher(Fetcher):
122122
def __init__(self,
123-
cache, # type: Dict[Text, Text]
123+
cache, # type: Dict[Text, Union[Text, bool]]
124124
session # type: Optional[requests.sessions.Session]
125125
): # type: (...) -> None
126126
self.cache = cache
127127
self.session = session
128128

129129
def fetch_text(self, url):
130130
# type: (Text) -> Text
131-
if url in self.cache:
132-
return self.cache[url]
131+
if url in self.cache and self.cache[url] is not True:
132+
# treat "True" as a placeholder that indicates something exists but
133+
# not necessarily what its contents is.
134+
return cast(Text, self.cache[url])
133135

134136
split = urllib.parse.urlsplit(url)
135137
scheme, path = split.scheme, split.path
@@ -172,6 +174,7 @@ def check_exists(self, url): # type: (Text) -> bool
172174
resp.raise_for_status()
173175
except Exception as e:
174176
return False
177+
self.cache[url] = True
175178
return True
176179
elif scheme == 'file':
177180
return os.path.exists(urllib.request.url2pathname(str(path)))
@@ -244,7 +247,7 @@ def __init__(self,
244247
idx=None, # type: Dict[Text, Union[CommentedMap, CommentedSeq, Text, None]]
245248
cache=None, # type: Dict[Text, Any]
246249
session=None, # type: requests.sessions.Session
247-
fetcher_constructor=None, # type: Callable[[Dict[Text, Text], requests.sessions.Session], Fetcher]
250+
fetcher_constructor=None, # type: Callable[[Dict[Text, Union[Text, bool]], requests.sessions.Session], Fetcher]
248251
skip_schemas=None # type: bool
249252
):
250253
# type: (...) -> None
@@ -378,22 +381,25 @@ def add_schemas(self, ns, base_url):
378381
if self.skip_schemas:
379382
return
380383
for sch in aslist(ns):
381-
fetchurl = self.fetcher.urljoin(base_url, sch)
382-
if fetchurl not in self.cache:
383-
_logger.debug("Getting external schema %s", fetchurl)
384-
content = self.fetch_text(fetchurl)
385-
self.cache[fetchurl] = rdflib.graph.Graph()
386-
for fmt in ['xml', 'turtle', 'rdfa']:
387-
try:
388-
self.cache[fetchurl].parse(data=content, format=fmt, publicID=str(fetchurl))
389-
self.graph += self.cache[fetchurl]
390-
break
391-
except xml.sax.SAXParseException:
392-
pass
393-
except TypeError:
394-
pass
395-
except BadSyntax:
396-
pass
384+
try:
385+
fetchurl = self.fetcher.urljoin(base_url, sch)
386+
if fetchurl not in self.cache or self.cache[fetchurl] is True:
387+
_logger.debug("Getting external schema %s", fetchurl)
388+
content = self.fetch_text(fetchurl)
389+
self.cache[fetchurl] = rdflib.graph.Graph()
390+
for fmt in ['xml', 'turtle', 'rdfa']:
391+
try:
392+
self.cache[fetchurl].parse(data=content, format=fmt, publicID=str(fetchurl))
393+
self.graph += self.cache[fetchurl]
394+
break
395+
except xml.sax.SAXParseException:
396+
pass
397+
except TypeError:
398+
pass
399+
except BadSyntax:
400+
pass
401+
except Exception as e:
402+
_logger.warn("Could not load extension schema %s: %s", fetchurl, e)
397403

398404
for s, _, _ in self.graph.triples((None, RDF.type, RDF.Property)):
399405
self._add_properties(s)

0 commit comments

Comments
 (0)