diff options
author | Wojtek Kosior <koszko@koszko.org> | 2022-02-09 14:41:05 +0100 |
---|---|---|
committer | Wojtek Kosior <koszko@koszko.org> | 2022-02-09 14:41:05 +0100 |
commit | 456ad6c0760329943f4f8e2b2b7dc0e260cba128 (patch) | |
tree | 8a2310e8e9c7e515284c4f29169602482be870a6 /src/hydrilla | |
parent | 8c65ebbddf7a3929b82848efa8f404d8ea56200a (diff) | |
download | hydrilla-builder-456ad6c0760329943f4f8e2b2b7dc0e260cba128.tar.gz hydrilla-builder-456ad6c0760329943f4f8e2b2b7dc0e260cba128.zip |
include new schemas in package and in tests
Diffstat (limited to 'src/hydrilla')
-rw-r--r-- | src/hydrilla/builder/build.py | 8 | ||||
m--------- | src/hydrilla/schemas | 0 | ||||
-rw-r--r-- | src/hydrilla/util/__init__.py | 2 | ||||
-rw-r--r-- | src/hydrilla/util/_util.py | 28 |
4 files changed, 29 insertions, 9 deletions
diff --git a/src/hydrilla/builder/build.py b/src/hydrilla/builder/build.py index dae5579..3503f7a 100644 --- a/src/hydrilla/builder/build.py +++ b/src/hydrilla/builder/build.py @@ -35,7 +35,7 @@ import jsonschema from .. import util -index_json_schema = util.load_schema('package_source-1.schema.json') +index_validator = util.validator_for('package_source-1.schema.json') class FileReferenceError(Exception): """ @@ -275,8 +275,8 @@ class Build: item_list.append(new_item_obj) - return dict([(prop, new_item_obj[prop]) - for prop in ('type', 'identifier', 'version')]) + props_in_ref = ('type', 'identifier', 'version', 'long_name') + return dict([(prop, new_item_obj[prop]) for prop in props_in_ref]) def _process_index_json(self, index_obj: dict): """ @@ -285,7 +285,7 @@ class Build: files and computed definitions of the source package and items defined in it. """ - jsonschema.validate(index_obj, index_json_schema) + index_validator.validate(index_obj) self.source_name = index_obj['source_name'] diff --git a/src/hydrilla/schemas b/src/hydrilla/schemas -Subproject ca1de2ed4a69a71f2f75552ade693d04ea1baa8 +Subproject c72c8438875d20b156d22d975523a19bbb407d9 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) |