diff options
author | Wojtek Kosior <koszko@koszko.org> | 2022-05-27 19:43:51 +0200 |
---|---|---|
committer | Wojtek Kosior <koszko@koszko.org> | 2022-05-31 15:17:11 +0200 |
commit | f42f5c1957e9c6ff8a61ef295b63dccead9b0ae8 (patch) | |
tree | efc3d1eb06f6cabd95635d88f957537312985a2a /src/hydrilla/builder/build.py | |
parent | 496d90f70a58c0040f8493aa8a5e4e2f106bfac7 (diff) | |
download | hydrilla-builder-f42f5c1957e9c6ff8a61ef295b63dccead9b0ae8.tar.gz hydrilla-builder-f42f5c1957e9c6ff8a61ef295b63dccead9b0ae8.zip |
incorporate version 2 of Hydrilla JSON schemas
Diffstat (limited to 'src/hydrilla/builder/build.py')
-rw-r--r-- | src/hydrilla/builder/build.py | 27 |
1 files changed, 19 insertions, 8 deletions
diff --git a/src/hydrilla/builder/build.py b/src/hydrilla/builder/build.py index 8354331..5de9351 100644 --- a/src/hydrilla/builder/build.py +++ b/src/hydrilla/builder/build.py @@ -51,7 +51,14 @@ here = Path(__file__).resolve().parent _ = util.translation(here / 'locales').gettext -index_validator = util.validator_for('package_source-2.schema.json') +def index_validator(major_schema_version): + """ + Create an index.json schema validator specific to the requested schema + version series. + """ + exact_version = {1: '1.0.1', 2: '2'}[major_schema_version] + + return util.validator_for(f'package_source-{exact_version}.schema.json') schemas_root = 'https://hydrilla.koszko.org/schemas' @@ -158,16 +165,18 @@ class Build: if not index_json_path.is_absolute(): index_json_path = (self.srcdir / index_json_path) - with open(index_json_path, 'rt') as index_file: - index_json_text = index_file.read() + index_obj, major = util.load_instance_from_file(index_json_path) - index_obj = json.loads(util.strip_json_comments(index_json_text)) + if major not in (1, 2): + msg = _('unknown_schema_package_source_{}')\ + .format(index_json_path) + raise util.UnknownSchemaError(msg) index_desired_path = PurePosixPath('index.json') self.files_by_path[index_desired_path] = \ - FileRef(index_desired_path, index_json_text.encode()) + FileRef(index_desired_path, index_json_path.read_bytes()) - self._process_index_json(index_obj) + self._process_index_json(index_obj, major) def _process_file(self, filename: Union[str, PurePosixPath], piggybacked: Piggybacked, @@ -308,14 +317,16 @@ class Build: 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): + def _process_index_json(self, index_obj: dict, + major_schema_version: int) -> None: """ Process 'index_obj' as contents of source package's index.json and store in memory this source package's zipfile as well as package's individual files and computed definitions of the source package and items defined in it. """ - index_validator.validate(index_obj) + index_validator(major_schema_version).validate(index_obj) + match = re.match(r'.*-((([1-9][0-9]*|0)\.)+)schema\.json$', index_obj['$schema']) self.source_schema_ver = \ |