onyo.lib.items module
- class onyo.lib.items.Item(item=None, repo=None, **kwargs)[source]
Bases:
ItemSpecAn item that an
onyo.lib.inventory.Inventorycan potentially track.In contrast to an
ItemSpec, anItemis associated with a repository. Thus, it gains the ability to lookup/generate pseudokey values and the automatic resolution of user-defined key aliases.On initialization, sanity checks are performed on the provided pseudokey-related values (e.g. all
'onyo.path'are consistent, generate the asset name from keys, etc). However, once instantiated,Itemallows changes to be made and makes no assurances about the consistency of the information stored in it. For example, modifyingonyo.is.directoryor a key used in the asset name will not automatically update the value ofonyo.path.file.It is the responsibility of those modifying values to clear caches and ensure consistency.
It is safest to use the operations available in
onyo.lib.inventory.Inventoryto modify Items.- update_from_path(path)[source]
Update the internal dictionary with key/values from a YAML file.
YAML comments are preserved on a best-effort basis. There is no straightforward way to merge YAML comments, and thus ones from
pathmay overwrite internal ones.- Parameters:
path (
Path) – Path of YAML file to update from.- Return type:
- class onyo.lib.items.ItemSpec(spec=None, alias_map=None, **kwargs)[source]
Bases:
UserDictNested dictionaries of static instructions to create an
Item.Compared to a dictionary, the primary features are:
load YAML (e.g.
ItemSpec(Path('file.yaml').read_text())dump YAML (e.g.
spec.yaml())equality including YAML comments (e.g.
ItemSpec() == ItemSpec())dot notation (e.g.
spec['nested.dict.key'])alias resolution
In contrast to
Item, anItemSpecis entirely static. It is not associated with a repository, and thus has no sanity checks nor pseudokey lookup capabilities. Alias resolution is possible, but the mapping must be provided manually.Multidimensional dictionaries are traversed using a dot as the delimiter. In other words, it provides a view of the flattened dictionary:
> d = {'key': 'value', 'nested': {'key': 'another value'}} > spec = ItemSpec(d) > spec['nested.key'] 'another value' > list(spec.keys()) ['key', 'nested.key']
Iteration only considers the flattened view. Keys that contain a dictionary are not yielded when using
.keys(),.values(), and.items().The underlying dictionary is available via the
.dataattribute when the standard Python behavior is needed.- equal_content(other)[source]
Whether another ItemSpec/Item and self have the same content and comments.
Pseudokeys are ignored entirely.