Skip to content

Commit

Permalink
resolving RefResolver deprecation issue in the master slug generator (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
danner26 authored Mar 27, 2024
1 parent 70a75ec commit a52fd70
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions tests/generate-slug-list.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
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

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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}')
Expand All @@ -93,4 +106,4 @@ def _generate_knowns(device_or_module):
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')
pickle_operations.write_pickle_data(KNOWN_MODULES, f'{ROOT_DIR}/tests/known-modules.pickle')

0 comments on commit a52fd70

Please sign in to comment.