aboutsummaryrefslogtreecommitdiff
path: root/src/hydrilla/util/_util.py
diff options
context:
space:
mode:
authorWojtek Kosior <koszko@koszko.org>2022-02-09 14:41:05 +0100
committerWojtek Kosior <koszko@koszko.org>2022-02-09 14:41:05 +0100
commit456ad6c0760329943f4f8e2b2b7dc0e260cba128 (patch)
tree8a2310e8e9c7e515284c4f29169602482be870a6 /src/hydrilla/util/_util.py
parent8c65ebbddf7a3929b82848efa8f404d8ea56200a (diff)
downloadhydrilla-builder-456ad6c0760329943f4f8e2b2b7dc0e260cba128.tar.gz
hydrilla-builder-456ad6c0760329943f4f8e2b2b7dc0e260cba128.zip
include new schemas in package and in tests
Diffstat (limited to 'src/hydrilla/util/_util.py')
-rw-r--r--src/hydrilla/util/_util.py28
1 files changed, 24 insertions, 4 deletions
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)