utopya._yaml_registry package#
Implements a YAML-file-based registry framework
Submodules#
utopya._yaml_registry.entry module#
Implements a single entry of the YAML-based registry framework
- class utopya._yaml_registry.entry.BaseSchema[source]#
Bases:
pydantic.main.BaseModel
A base schema for registry entries.
This base schema is configured such that it provides safe defaults: Extra keys are not allowed and all default values as well as assignments are validated.
Note
The
validate_assignment
pydantic configuration option may also lead to type coercion! For instance, an integer type value is always representable as a string; thus, assigning an integer to a field of type string will simply lead tostr(my_int)
being stored.Warning
If foregoing validation on item assignment, it is no longer guaranteed that the written data can be loaded without errors.
- class Config[source]#
Bases:
object
- extra = 'forbid'#
- validate_all = True#
- validate_assignment = True#
- get(*args) Any [source]#
Get a named attribute from this object.
Behaves exactly like
getattr(self, *args)
.
- _abc_impl = <_abc_data object>#
- class utopya._yaml_registry.entry.RegistryEntry(name: str, *, registry: YAMLRegistry = None, **data)[source]#
Bases:
object
A registry entry holds some data (in the form of a validated pydantic schema) and allows to read that data from a registry file and write any changes to that file.
- SCHEMA#
The data schema that is used for validation
- FILE_EXTENSION: str = '.yml'#
The file extension that is used for the yaml files – case-sensitive and with leading dot.
- _NO_FORWARDING_ATTRS: tuple = ('_name', '_data', '_registry')#
Attribute names that are not forwarded to the underlying data object
- __init__(name: str, *, registry: YAMLRegistry = None, **data)[source]#
Initialize a registry entry with a certain name.
- Parameters
name (str) – The name of the registry entry, corresponding to a file in the registry directory.
registry (YAMLRegistry, optional) – A registry object as part of which this entry is managed. If not given, requires
data
.**data – If given, uses this data to initialize the entry. If a registry is associated with this entry, will also write that data to the corresponding file immediately.
- _parse_data(d: dict) utopya._yaml_registry.entry.BaseSchema [source]#
Uses the schema to set the entry’s data from the given dict
- _set_registry(registry: YAMLRegistry)[source]#
Associates a registry with this entry
- property data: utopya._yaml_registry.entry.BaseSchema#
The entry’s data
- __eq__(other: Any) bool [source]#
An entry compares equal if the type is identical and the name and data compare equal.
Note
The associated registry is not compared!
- __getattr__(attr: str)[source]#
Forward attribute calls (that do not match the entry’s other attributes) to the underlying entry data.
Alternatively, use the
data
property to directly access the data.
- __setattr__(attr: str, value: Any)[source]#
Forwards attribute setting calls to the underlying entry data.
This is only done if
attr
is not in_NO_FORWARDING_ATTRS
and theattr
is actually a property of the schema. Otherwise, regular attribute setting occurs.Warning
Changes to the data are not automatically written to the YAML file. To do so, call
write()
after all changes have been completed.Note that validation will occur at that point and not when changing any data values.
- get(*args) Any [source]#
Get a named attribute from this object’s entry or return a fallback.
Behaves exactly like
getattr(self, *args)
.
- load()[source]#
Reads the entry from the registry file, raising an error if the file does not exist. The loaded data overwrites whatever is stored in the entry already.
- write()[source]#
Writes the registry entry to the corresponding registry file, creating it if it does not exist.
Note
The data is written with an intermediate json-serialization carried out by pydantic. That ensures that the data contains only native data types which can be written to YAML without any custom representers.
utopya._yaml_registry.registry module#
Implements a YAML-based registry infrastructure
- class utopya._yaml_registry.registry.KeyOrderedDict(*args, key: Optional[Callable] = None, **kwds)[source]#
Bases:
dantro.utils.ordereddict.KeyOrderedDict
A key-ordered dict that expects string keys and sorts by the lower-case representation of keys.
- DEFAULT_KEY_COMPARATOR(k)#
- class utopya._yaml_registry.registry.YAMLRegistry(EntryCls: type, *, registry_dir: str)[source]#
Bases:
object
A registry framework that persistently stores the registry entries as YAML files within a common directory.
Individual registry entries can be retrieved via a dict-like interface.
- __init__(EntryCls: type, *, registry_dir: str)[source]#
Set up a registry directory for a certain class of registry entries.
- reload()[source]#
Load all available entries from the registry directory.
If called multiple times, will only load entries that are not already loaded.
- __getitem__(name: str)[source]#
Retrieve a deep copy of a model registration entry for the given model name.
- add_entry(name: str, *, exists_action: str = 'raise', **data) utopya._yaml_registry.entry.RegistryEntry [source]#
Creates a new entry and stores it in the registry. If an entry of the same name already exists, allows according to the
exists_action
Adds a new entry of a certain name; raises if it already exists.TODO Write
- Parameters
- Returns
Description
- Return type
- Raises
EntryExistsError – Description
- remove_entry(name: str)[source]#
Removes a registry entry and deletes the associated registry file.
- Parameters
name (str) – The name of the entry that is to be removed
- Raises
ValueError – On invalid (non-existing) model