aboutsummaryrefslogtreecommitdiff
path: root/src/hydrilla/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/hydrilla/util')
-rw-r--r--src/hydrilla/util/__init__.py2
-rw-r--r--src/hydrilla/util/_util.py28
2 files changed, 25 insertions, 5 deletions
diff --git a/src/hydrilla/util/__init__.py b/src/hydrilla/util/__init__.py
index d435c0a..2a69e0d 100644
--- a/src/hydrilla/util/__init__.py
+++ b/src/hydrilla/util/__init__.py
@@ -5,4 +5,4 @@
# Available under the terms of Creative Commons Zero v1.0 Universal.
from ._util import strip_json_comments, normalize_version, parse_version, \
- version_string, load_schema
+ version_string, validator_for
diff --git a/src/hydrilla/util/_util.py b/src/hydrilla/util/_util.py
index 0ce62f6..9bebdc1 100644
--- a/src/hydrilla/util/_util.py
+++ b/src/hydrilla/util/_util.py
@@ -30,6 +30,8 @@ import json
from pathlib import Path
from typing import Optional
+from jsonschema import RefResolver, Draft7Validator
+
here = Path(__file__).resolve().parent
_strip_comment_re = re.compile(r'''
@@ -103,7 +105,25 @@ def version_string(ver: list[int], rev: Optional[int]=None) -> str:
"""
return '.'.join([str(n) for n in ver]) + ('' if rev is None else f'-{rev}')
-def load_schema(schema_filename: str) -> dict:
- """Find schema JSON file in '../schemas' and json.load() it."""
- with open(here.parent / 'schemas' / schema_filename, 'rt') as file_handle:
- return json.load(file_handle)
+schemas = {}
+for path in (here.parent / 'schemas').glob('*-1.schema.json'):
+ schema = json.loads(path.read_text())
+ schemas[schema['$id']] = schema
+
+common_schema_filename = 'common_definitions-1.schema.json'
+common_schema_path = here.parent / "schemas" / common_schema_filename
+
+resolver = RefResolver(
+ base_uri=f'file://{str(common_schema_path)}',
+ referrer=f'https://hydrilla.koszko.org/{common_schema_filename}',
+ store=schemas
+)
+
+def validator_for(schema_filename: str) -> Draft7Validator:
+ """
+ Prepare a validator for one of the schemas in '../schemas'.
+
+ This function is not thread-safe.
+ """
+ return Draft7Validator(resolver.resolve(schema_filename)[1],
+ resolver=resolver)