Implements a YAML-file-based registry framework
Implements a single entry of the YAML-based registry framework
- class utopya._yaml_registry.entry.BaseSchema[source]#
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.
validate_assignmentpydantic 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 to
If foregoing validation on item assignment, it is no longer guaranteed that the written data can be loaded without errors.
- class Config[source]#
- extra = 'forbid'#
- validate_all = True#
- validate_assignment = True#
- get(*args) Any [source]#
Get a named attribute from this object.
Behaves exactly like
- _abc_impl = <_abc_data object>#
- class utopya._yaml_registry.entry.RegistryEntry(name: str, *, registry: YAMLRegistry = None, **data)[source]#
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.
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.
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 – 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.
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
dataproperty 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
attris not in
attris actually a property of the schema. Otherwise, regular attribute setting occurs.
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
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.
Writes the registry entry to the corresponding registry file, creating it if it does not exist.
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.
Implements a YAML-based registry infrastructure
- class utopya._yaml_registry.registry.KeyOrderedDict(*args, key: Optional[Callable] = None, **kwds)[source]#
A key-ordered dict that expects string keys and sorts by the lower-case representation of keys.
- class utopya._yaml_registry.registry.YAMLRegistry(EntryCls: type, *, registry_dir: str)[source]#
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.
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_actionAdds a new entry of a certain name; raises if it already exists.
- Return type
EntryExistsError – Description
- remove_entry(name: str)[source]#
Removes a registry entry and deletes the associated registry file.
name (str) – The name of the entry that is to be removed
ValueError – On invalid (non-existing) model