Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create lazy function that caches DANDI LinkML schema #11

Merged
merged 3 commits into from
Oct 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions src/dandisets_linkml_status_tools/cli/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from linkml.validator import Validator
from linkml.validator.plugins import JsonschemaValidationPlugin, ValidationPlugin
from linkml.validator.report import ValidationResult
from linkml_runtime.linkml_model import SchemaDefinition
from pydantic import TypeAdapter, ValidationError
from pydantic2linkml.gen_linkml import translate_defs
from yaml import dump as yaml_dump
Expand All @@ -38,9 +39,6 @@
# The names of the collection of modules in which the DANDI models are defined
DANDI_MODULE_NAMES = ["dandischema.models"]

# The LinkML schema produced by the pydantic2linkml translator for DANDI models
DANDI_LINKML_SCHEMA = translate_defs(DANDI_MODULE_NAMES)

# A callable that sorts a given iterable of strings in a case-insensitive manner
isorted = partial(sorted, key=str.casefold)

Expand Down Expand Up @@ -70,6 +68,9 @@ class DandisetLinkmlValidator:
expressed in Pydantic
"""

# The LinkML schema produced by the pydantic2linkml translator for DANDI models
_dandi_linkml_schema: Optional[SchemaDefinition] = None

def __init__(self, validation_plugins: Optional[list[ValidationPlugin]] = None):
"""
Initialize a `DandisetLinkmlValidator` instance that wraps a LinkML validator
Expand All @@ -84,10 +85,22 @@ def __init__(self, validation_plugins: Optional[list[ValidationPlugin]] = None):
validation_plugins = [JsonschemaValidationPlugin(closed=True)]

self._inner_validator = Validator(
DANDI_LINKML_SCHEMA,
self.get_dandi_linkml_schema(),
validation_plugins=validation_plugins,
)

@classmethod
def get_dandi_linkml_schema(cls) -> SchemaDefinition:
"""
Get the LinkML schema produced by the pydantic2linkml translator for DANDI models

:return: The LinkML schema
"""
if cls._dandi_linkml_schema is None:
cls._dandi_linkml_schema = translate_defs(DANDI_MODULE_NAMES)

return cls._dandi_linkml_schema

def validate(self, dandiset_metadata: dict[str, Any]) -> list[ValidationResult]:
"""
Validate the given dandiset metadata against the dandiset metadata model in
Expand Down
Loading