Schema

Class for managing, building, and caching built schemas.

The nwb.core and hdmf-common schema are statically built and stored in this repository, but to make it feasible to use arbitrary schema, eg. those stored inside of an NWB file, we need a bit of infrastructure for generating and caching pydantic models on the fly.

Relationship to other modules: * adapters manage the conversion from NWB schema language to linkML. * generators create models like pydantic models from the linkML schema * providers then use adapters and generators

to provide models from generated schema!

Providers create a set of directories with namespaces and versions, so eg. for the linkML and pydantic providers:

cache_dir
  - linkml
    - nwb_core
      - v0_2_0
        - namespace.yaml
        - nwb.core.file.yaml
        - ...
      - v0_2_1
        - namespace.yaml
        - ...
    - my_schema
      - v0_1_0
        - ...
  - pydantic
    - nwb_core
      - v0_2_0
        - namespace.py
        - ...
      - v0_2_1
        - namespace.py
        - ...
class SchemaProvider(versions: Dict[str, str] | None = None, **kwargs)

Class to manage building and caching linkml and pydantic models generated from nwb schema language. Combines LinkMLProvider and PydanticProvider

Behaves like a singleton without needing to be one - since we’re working off caches on disk that are indexed by hash in most “normal” conditions you should be able to use this anywhere, though no file-level locks are present to ensure consistency.

Store each generated schema in a directory structure indexed by schema namespace name and version

Parameters:
  • versions (dict) – Dictionary like {'namespace': 'v1.0.0'} used to specify that this provider should always return models from a specific version of a namespace (unless explicitly requested otherwise in a call to get() ).

  • **kwargs – passed to superclass __init__ (see Provider )

build_from_yaml(path: Path, **kwargs: dict) Dict[str | SchemaDefinitionName, LinkMLSchemaBuild]

Alias for LinkMLProvider.build_from_yaml() that also builds a pydantic model

build_from_dicts(schemas: Dict[str, dict], **kwargs: dict) Dict[str | SchemaDefinitionName, LinkMLSchemaBuild]

Alias for LinkMLProvider.build_from_dicts() that also builds a pydantic model

property path: Path

cache_dir provided by Config

build(ns_adapter: NamespacesAdapter, verbose: bool = True, linkml_kwargs: dict | None = None, pydantic_kwargs: dict | None = None, **kwargs) Dict[str, str]

Build a namespace, storing its linkML and pydantic models.

Parameters:
  • ns_adapter

  • verbose (bool) – If True (default), show progress bars

  • linkml_kwargs (Optional[dict]) – Dictionary of kwargs optionally passed to LinkMLProvider.build()

  • pydantic_kwargs (Optional[dict]) – Dictionary of kwargs optionally passed to PydanticProvider.build()

  • **kwargs – Common options added to both linkml_kwargs and pydantic_kwargs

Returns:

Dict[str,str] mapping namespaces to built pydantic sources

get(namespace: str, version: str | None = None) ModuleType

Get a built pydantic model for a given namespace and version.

Wrapper around PydanticProvider.get()

get_class(namespace: str, class_: str, version: str | None = None) Type[BaseModel]

Get a pydantic model class from a given namespace and version!

Wrapper around PydanticProvider.get_class()