Skip to content

Commit c230da6

Browse files
authored
Merge pull request #195 from tschaub/schema-caching
Cache parsed schema
2 parents e10e900 + eea3253 commit c230da6

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
44

55
The format is (loosely) based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).
66

7+
## [v2.4.3] - 2022-03-10
8+
### Changed
9+
10+
- Add schema caching
11+
712
## [v2.4.2] - 2022-03-02
813
### Changed
914

setup.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from setuptools import setup
44

5-
__version__ = "2.4.2"
5+
__version__ = "2.4.3"
66

77
with open("README.md", "r") as fh:
88
long_description = fh.read()
@@ -28,8 +28,8 @@
2828
keywords="STAC validation raster",
2929
long_description=long_description,
3030
long_description_content_type="text/markdown",
31-
url="https://github.com/sparkgeo/stac-validator",
32-
download_url="https://github.com/sparkgeo/stac-validator/archive/v2.3.0.tar.gz",
31+
url="https://github.com/stac-utils/stac-validator",
32+
download_url="https://github.com/stac-utils/stac-validator/archive/v2.4.3.tar.gz",
3333
install_requires=[
3434
"requests>=2.19.1",
3535
"jsonschema>=3.2.0",

stac_validator/utilities.py

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import functools
12
import json
23
from urllib.parse import urlparse
34
from urllib.request import urlopen
@@ -56,6 +57,11 @@ def fetch_and_parse_file(input_path) -> dict:
5657
return data
5758

5859

60+
@functools.lru_cache(maxsize=48)
61+
def fetch_and_parse_schema(input_path) -> dict:
62+
return fetch_and_parse_file(input_path)
63+
64+
5965
# validate new versions at schemas.stacspec.org
6066
def set_schema_addr(version, stac_type: str):
6167
if version in NEW_VERSIONS:

stac_validator/validate.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from .utilities import (
1313
fetch_and_parse_file,
14+
fetch_and_parse_schema,
1415
get_stac_type,
1516
link_request,
1617
set_schema_addr,
@@ -150,15 +151,15 @@ def extensions_validator(self, stac_type: str) -> dict:
150151
def custom_validator(self):
151152
# in case the path to custom json schema is local
152153
# it may contain relative references
153-
schema = fetch_and_parse_file(self.custom)
154+
schema = fetch_and_parse_schema(self.custom)
154155
if os.path.exists(self.custom):
155156
custom_abspath = os.path.abspath(self.custom)
156157
custom_dir = os.path.dirname(custom_abspath).replace("\\", "/")
157158
custom_uri = f"file:///{custom_dir}/"
158159
resolver = RefResolver(custom_uri, self.custom)
159160
jsonschema.validate(self.stac_content, schema, resolver=resolver)
160161
else:
161-
schema = fetch_and_parse_file(self.custom)
162+
schema = fetch_and_parse_schema(self.custom)
162163
jsonschema.validate(self.stac_content, schema)
163164

164165
def core_validator(self, stac_type: str):
@@ -233,7 +234,7 @@ def recursive_validator(self, stac_type: str):
233234
self.custom = set_schema_addr(self.version, stac_type.lower())
234235
message = self.create_message(stac_type, "recursive")
235236
if self.version == "0.7.0":
236-
schema = fetch_and_parse_file(self.custom)
237+
schema = fetch_and_parse_schema(self.custom)
237238
# this next line prevents this: unknown url type: 'geojson.json' ??
238239
schema["allOf"] = [{}]
239240
jsonschema.validate(self.stac_content, schema)

0 commit comments

Comments
 (0)