Skip to content

Commit

Permalink
Python codegen: reuse the existing check_exists function.
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-c committed Sep 11, 2023
1 parent 61d0cde commit 4036fef
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 52 deletions.
29 changes: 3 additions & 26 deletions schema_salad/metaschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
cast,
)
from urllib.parse import quote, urldefrag, urlparse, urlsplit, urlunsplit
from urllib.request import pathname2url, url2pathname
from urllib.request import pathname2url

from rdflib import Graph
from rdflib.plugins.parsers.notation3 import BadSyntax
Expand Down Expand Up @@ -145,29 +145,6 @@ def __init__(
self.vocab[k] = v
self.rvocab[v] = k

def check_exists(self, url: str) -> bool:
"""Check if a url exists and is valid."""
if url in self.cache:
return True

split = urlsplit(url)
scheme, path = split.scheme, split.path

if scheme in ["http", "https"]:
if self.fetcher is None:
raise ValidationException(f"Can't check {self.fetcher} URL, fetcher is None")
try:
self.fetcher.fetch_text(url)
except Exception:
return False
self.cache[url] = True
return True
if scheme == "file":
return os.path.exists(url2pathname(str(path)))
if scheme == "mailto":
return True
raise ValidationException(f"Unsupported scheme '{scheme}' in url: {url}")

@property
def graph(self) -> Graph:
"""Generate a merged rdflib.Graph from all entries in self.schemas."""
Expand Down Expand Up @@ -489,7 +466,7 @@ def load(self, doc, baseuri, loadingOptions, docRoot=None, lc=None):
if doc[i].get("id") in fields:
errors.append(
ValidationException(
f"Duplicate field '{doc[i].get('id')}'",
f"Duplicate field {doc[i].get('id')!r}",
SourceLine(doc[i], "id", str),
[],
)
Expand Down Expand Up @@ -743,7 +720,7 @@ def load(self, doc, baseuri, loadingOptions, docRoot=None, lc=None):
if isinstance(doc, str):
errors = []
try:
if not loadingOptions.check_exists(doc):
if not loadingOptions.fetcher.check_exists(doc):
errors.append(ValidationException(f"contains undefined reference to `{doc}`"))
except ValidationException:
pass
Expand Down
29 changes: 3 additions & 26 deletions schema_salad/python_codegen_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
cast,
)
from urllib.parse import quote, urldefrag, urlparse, urlsplit, urlunsplit
from urllib.request import pathname2url, url2pathname
from urllib.request import pathname2url

from rdflib import Graph
from rdflib.plugins.parsers.notation3 import BadSyntax
Expand Down Expand Up @@ -142,29 +142,6 @@ def __init__(
self.vocab[k] = v
self.rvocab[v] = k

def check_exists(self, url: str) -> bool:
"""Check if a url exists and is valid."""
if url in self.cache:
return True

split = urlsplit(url)
scheme, path = split.scheme, split.path

if scheme in ["http", "https"]:
if self.fetcher is None:
raise ValidationException(f"Can't check {self.fetcher} URL, fetcher is None")
try:
self.fetcher.fetch_text(url)
except Exception:
return False
self.cache[url] = True
return True
if scheme == "file":
return os.path.exists(url2pathname(str(path)))
if scheme == "mailto":
return True
raise ValidationException(f"Unsupported scheme '{scheme}' in url: {url}")

@property
def graph(self) -> Graph:
"""Generate a merged rdflib.Graph from all entries in self.schemas."""
Expand Down Expand Up @@ -486,7 +463,7 @@ def load(self, doc, baseuri, loadingOptions, docRoot=None, lc=None):
if doc[i].get("id") in fields:
errors.append(
ValidationException(
f"Duplicate field '{doc[i].get('id')}'",
f"Duplicate field {doc[i].get('id')!r}",
SourceLine(doc[i], "id", str),
[],
)
Expand Down Expand Up @@ -740,7 +717,7 @@ def load(self, doc, baseuri, loadingOptions, docRoot=None, lc=None):
if isinstance(doc, str):
errors = []
try:
if not loadingOptions.check_exists(doc):
if not loadingOptions.fetcher.check_exists(doc):
errors.append(ValidationException(f"contains undefined reference to `{doc}`"))
except ValidationException:
pass
Expand Down

0 comments on commit 4036fef

Please sign in to comment.