Skip to content

Commit

Permalink
deprecate REFResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
h-mayorquin committed Nov 8, 2024
1 parent cbf68d4 commit 95a9ac4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ dependencies = [
"parse>=1.20.0",
"click",
"docstring-parser",
"packaging" # Issue 903
"packaging", # Issue 903
"referencing",
]


Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import sys
from importlib import import_module
from pathlib import Path
from typing import Optional

import click
from jsonschema import RefResolver, validate
from jsonschema import validate
from pydantic import DirectoryPath, FilePath
from referencing import Registry, Resource

from ...nwbconverter import NWBConverter
from ...utils import dict_deep_update, load_dict_from_file
Expand Down Expand Up @@ -79,12 +79,30 @@ def run_conversion_from_yaml(
output_folder_path = Path(output_folder_path)
specification = load_dict_from_file(file_path=specification_file_path)
schema_folder = Path(__file__).parent.parent.parent / "schemas"

# Load all required schemas
specification_schema = load_dict_from_file(file_path=schema_folder / "yaml_conversion_specification_schema.json")
sys_uri_base = "file:/" if sys.platform.startswith("win32") else "file://"
metadata_schema = load_dict_from_file(file_path=schema_folder / "metadata_schema.json")

# Create registry and add schemas starting with the metadata schema
registry = Registry().with_resource(
"metadata_schema.json", Resource.from_contents(metadata_schema) # Base name without './'
)

# Then add the specification schema with the metadata schema reference
registry = registry.with_resource(
"yaml_conversion_specification_schema.json", # This will be the base URI
Resource.from_contents(specification_schema),
)

# Also add with relative path since that's how it's referenced
registry = registry.with_resource("./metadata_schema.json", Resource.from_contents(metadata_schema))

# Validate using the registry
validate(
instance=specification,
schema=specification_schema,
resolver=RefResolver(base_uri=sys_uri_base + str(schema_folder) + "/", referrer=specification_schema),
registry=registry,
)

global_metadata = specification.get("metadata", dict())
Expand Down
22 changes: 15 additions & 7 deletions tests/test_on_data/test_yaml/test_yaml_conversion_specification.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import sys
import unittest
from datetime import datetime
from pathlib import Path

import pytest
from hdmf.testing import TestCase
from jsonschema import RefResolver, validate
from jsonschema import validate
from pynwb import NWBHDF5IO
from referencing import Registry, Resource

from neuroconv import run_conversion_from_yaml
from neuroconv.utils import load_dict_from_file
Expand All @@ -27,16 +27,24 @@
def test_validate_example_specifications(fname):
path_to_test_yml_files = Path(__file__).parent / "conversion_specifications"
schema_folder = path_to_test_yml_files.parent.parent.parent.parent / "src" / "neuroconv" / "schemas"

# Load schemas
specification_schema = load_dict_from_file(file_path=schema_folder / "yaml_conversion_specification_schema.json")
sys_uri_base = "file://"
if sys.platform.startswith("win32"):
sys_uri_base = "file:/"
metadata_schema = load_dict_from_file(file_path=schema_folder / "metadata_schema.json")

# Create registry and add schemas
registry = (
Registry()
.with_resource("metadata_schema.json", Resource.from_contents(metadata_schema))
.with_resource("yaml_conversion_specification_schema.json", Resource.from_contents(specification_schema))
.with_resource("./metadata_schema.json", Resource.from_contents(metadata_schema))
)

yaml_file_path = path_to_test_yml_files / fname
validate(
instance=load_dict_from_file(file_path=yaml_file_path),
schema=load_dict_from_file(file_path=schema_folder / "yaml_conversion_specification_schema.json"),
resolver=RefResolver(base_uri=sys_uri_base + str(schema_folder) + "/", referrer=specification_schema),
schema=specification_schema,
registry=registry,
)


Expand Down

0 comments on commit 95a9ac4

Please sign in to comment.