From a52fd70ed2b99f4cd4145f7fb866febe51601e77 Mon Sep 17 00:00:00 2001 From: "Daniel W. Anner" Date: Wed, 27 Mar 2024 17:08:02 -0400 Subject: [PATCH] resolving RefResolver deprecation issue in the master slug generator (#2039) --- tests/generate-slug-list.py | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/tests/generate-slug-list.py b/tests/generate-slug-list.py index cb901c559..473d41ed9 100644 --- a/tests/generate-slug-list.py +++ b/tests/generate-slug-list.py @@ -4,9 +4,10 @@ import glob import yaml import decimal from yaml_loader import DecimalSafeLoader -from jsonschema import Draft4Validator, RefResolver +from referencing import Registry, Resource +from jsonschema import Draft202012Validator from jsonschema.exceptions import ValidationError -from test_configuration import SCHEMAS, KNOWN_SLUGS, ROOT_DIR, KNOWN_MODULES +from test_configuration import SCHEMAS, SCHEMAS_BASEPATH, KNOWN_SLUGS, ROOT_DIR, KNOWN_MODULES from urllib.request import urlopen import pickle_operations @@ -34,6 +35,22 @@ def _get_type_files(device_or_module): return file_list +def _generate_schema_registry(): + """ + Return a list of all definition files within the specified path. + """ + registry = Registry() + + for schema_f in os.listdir(SCHEMAS_BASEPATH): + # Initialize the schema + with open(f"schema/{schema_f}") as schema_file: + resource = Resource.from_contents(json.loads(schema_file.read(), parse_float=decimal.Decimal)) + registry = resource @ registry + + return registry + +SCHEMA_REGISTRY = _generate_schema_registry() + def _decimal_file_handler(uri): """ Handler to work with floating decimals that fail normal validation. @@ -62,13 +79,9 @@ def load_file(file_path, schema): # Validate YAML definition against the supplied schema try: - resolver = RefResolver( - f"file://{os.getcwd()}/schema/devicetype.json", - schema, - handlers={"file": _decimal_file_handler}, - ) # Validate definition against schema - Draft4Validator(schema, resolver=resolver).validate(definition) + validator = Draft202012Validator(schema, registry=SCHEMA_REGISTRY) + validator.validate(definition) except ValidationError as exc: # Schema validation failure. Ensure you are following the proper format. return (False, f'{file_path} failed validation: {exc}') @@ -93,4 +106,4 @@ _generate_knowns('device') pickle_operations.write_pickle_data(KNOWN_SLUGS, f'{ROOT_DIR}/tests/known-slugs.pickle') _generate_knowns('module') -pickle_operations.write_pickle_data(KNOWN_MODULES, f'{ROOT_DIR}/tests/known-modules.pickle') \ No newline at end of file +pickle_operations.write_pickle_data(KNOWN_MODULES, f'{ROOT_DIR}/tests/known-modules.pickle')