diff options
Diffstat (limited to 'src/hydrilla/util/_util.py')
-rw-r--r-- | src/hydrilla/util/_util.py | 28 |
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) |