From 73a443f518be29a50981e7e137e2b4c2c37552dd Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Tue, 31 May 2022 16:52:14 +0200 Subject: improve loading of schema files --- src/hydrilla/util/_util.py | 58 ++++++++++++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 20 deletions(-) (limited to 'src/hydrilla/util/_util.py') diff --git a/src/hydrilla/util/_util.py b/src/hydrilla/util/_util.py index 815b7fd..de7435d 100644 --- a/src/hydrilla/util/_util.py +++ b/src/hydrilla/util/_util.py @@ -117,14 +117,43 @@ 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}') +_schema_name_re = re.compile(r''' +(?P[^/]*) +- +(?P + (?P[1-9][0-9]*) + (?: # this repeated group matches the remaining version numbers + \. + (?:[1-9][0-9]*|0) + )* +) +\.schema\.json +$ +''', re.VERBOSE) + +schema_paths = {} +for path in (here.parent / 'schemas').rglob('*.schema.json'): + match = _schema_name_re.search(path.name) + schema_name_base = match.group('name_base') + schema_ver_list = match.group('ver').split('.') + + for i in range(len(schema_ver_list)): + schema_ver = '.'.join(schema_ver_list[:i+1]) + schema_paths[f'{schema_name_base}-{schema_ver}.schema.json'] = path + +for name, path in [*schema_paths.items()]: + schema_paths[f'https://hydrilla.koszko.org/schemas/{name}'] = path + schemas = {} -for series_dir in (here.parent / 'schemas').glob('*.x'): - for path in series_dir.glob("*.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 +def _get_schema(schema_name: str) -> dict: + """Return loaded JSON of the requested schema. Cache results.""" + path = schema_paths[schema_name] + + if path not in schemas: + schemas[path] = json.loads(path.read_text()) + + return schemas[path] def validator_for(schema: Union[str, dict]) -> Draft7Validator: """ @@ -133,27 +162,16 @@ def validator_for(schema: Union[str, dict]) -> Draft7Validator: Other schemas under '../schemas' can be referenced. """ if isinstance(schema, str): - schema = schemas[f'https://hydrilla.koszko.org/schemas/{schema}'] + schema = _get_schema(schema) resolver = RefResolver( base_uri=schema['$id'], referrer=schema, - handlers={'https': lambda uri: schemas[uri]} + handlers={'https': _get_schema} ) return Draft7Validator(schema, resolver=resolver) -_major_version_re = re.compile(r''' -- -(?P[1-9][0-9]*) -(?: # this repeated group matches the remaining version numbers - \. - (?:[1-9][0-9]*|0) -)* -\.schema\.json -$ -''', re.VERBOSE) - def load_instance_from_file(path: Path) -> tuple[dict, Optional[int]]: """ Open a file and load its contents as a JSON document (with additional @@ -167,7 +185,7 @@ def load_instance_from_file(path: Path) -> tuple[dict, Optional[int]]: major = None if type(instance) is dict and type(instance.get('$schema')) is str: - match = _major_version_re.search(instance.get('$schema')) + match = _schema_name_re.search(instance.get('$schema')) major = match and int(match.group('major')) return instance, major -- cgit v1.2.3