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: 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 to str(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.

model_config: ClassVar[ConfigDict] = {'extra': 'forbid', 'validate_assignment': True, 'validate_default': True}#

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

__getitem__(name: str)[source]#

Retrieves an item from the underlying schema data.

get(*args) Any[source]#

Get a named attribute from this object.

Behaves exactly like getattr(self, *args).

_abc_impl = <_abc._abc_data object>#
model_computed_fields: ClassVar[dict[str, ComputedFieldInfo]] = {}#

A dictionary of computed field names and their corresponding ComputedFieldInfo objects.

model_fields: ClassVar[dict[str, FieldInfo]] = {}#

Metadata about the fields defined on the model, mapping of field names to [FieldInfo][pydantic.fields.FieldInfo].

This replaces Model.__fields__ from Pydantic V1.

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

alias of BaseSchema

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) 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 name: str#

Name of this entry

property has_registry: bool#

Whether a registry is associated with this entry

property registry_dir: str#

The associated registry directory

property registry_file_path: str#

The absolute path to the registry file

property data: BaseSchema#

The entry’s data

dict() dict[source]#

The entry’s data in pydantic’s dict format, deep-copied.

__str__() str[source]#

String descriptor of this object

__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 the attr 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.

__getitem__(name: str)[source]#

Retrieves an item from the underlying entry data.

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.

remove_registry_file()[source]#

Removes the corresponding registry file

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: 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.

Parameters:
  • EntryCls (type) – Type of the individual entries

  • registry_dir (str) – Path to the directory in which the individual registry entry files are to be stored.

property registry_dir: str#

The associated registry directory

reload()[source]#

Load all available entries from the registry directory.

If called multiple times, will only load entries that are not already loaded.

keys()[source]#
values()[source]#
items()[source]#
__contains__(name: str) bool[source]#

Whether an entry of the given name exists in the registry

__getitem__(name: str)[source]#

Retrieve a deep copy of a model registration entry for the given model name.

__delitem__(name: str)[source]#

Removes a registry entry

add_entry(name: str, *, exists_action: str = 'raise', **data) 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:
  • name (str) – Description

  • exists_action (str, optional) – Description

  • **data – Description

Returns:

Description

Return type:

RegistryEntry

Raises:
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