Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/check_jsonschema/cachedownloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class CacheDownloader:
# this will let us do any other caching we might need in the future in the same
# cache dir (adjacent to "downloads")
_CACHEDIR_NAME = os.path.join("check_jsonschema", "downloads")
# Keep list of newly loaded/revalidated schemas in memory to avoid network requests
# Especially useful for schemas making extensive use of refs to remote URLs
_DOWNLOADED_URIS: set[str] = set()

def __init__(
self,
Expand Down Expand Up @@ -113,10 +116,14 @@ def _write(self, dest: str, response: requests.Response) -> None:
shutil.copy(fp.name, dest)
os.remove(fp.name)

def _download(self) -> str:
def _cachefile_path(self) -> str:
assert self._cache_dir
os.makedirs(self._cache_dir, exist_ok=True)
dest = os.path.join(self._cache_dir, self._filename)
return os.path.join(self._cache_dir, self._filename)

def _download(self) -> str:
dest = self._cachefile_path()
CacheDownloader._DOWNLOADED_URIS.add(self._file_url)

response = self._get_request()
# check to see if we have a file which matches the connection
Expand All @@ -130,6 +137,14 @@ def _download(self) -> str:
def open(self) -> t.Iterator[t.IO[bytes]]:
if (not self._cache_dir) or self._disable_cache:
yield io.BytesIO(self._get_request().content)

else:
with open(self._download(), "rb") as fp:
yield fp
cachefile = self._cachefile_path()
if self._file_url in CacheDownloader._DOWNLOADED_URIS and os.path.exists(
cachefile
):
with open(cachefile, "rb") as fp:
yield fp
else:
with open(self._download(), "rb") as fp:
yield fp
10 changes: 5 additions & 5 deletions src/check_jsonschema/schema_loader/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
import urllib.parse

import referencing
import requests
from referencing.jsonschema import DRAFT202012, Schema

from check_jsonschema.cachedownloader import CacheDownloader

from ..parsers import ParserSet
from ..utils import filename2path

Expand Down Expand Up @@ -62,10 +63,9 @@ def retrieve_reference(uri: str) -> referencing.Resource[Schema]:

full_uri_scheme = urllib.parse.urlsplit(full_uri).scheme
if full_uri_scheme in ("http", "https"):
data = requests.get(full_uri, stream=True)
parsed_object = parser_set.parse_data_with_path(
data.content, full_uri, "json"
)
dwl = CacheDownloader(full_uri)
with dwl.open() as file:
parsed_object = parser_set.parse_data_with_path(file, full_uri, "json")
else:
parsed_object = get_local_file(full_uri)

Expand Down