onyo.lib.utils module
- class onyo.lib.utils.DotNotationWrapper(_DotNotationWrapper__dict=None, **kwargs)[source]
Bases:
UserDictDictionary wrapper for providing access to nested dictionaries via hierarchical keys.
This class wraps a dictionary (available from the attribute .data) to allow traversing multidimensional dictionaries using a dot as the delimiter. In other words, it provides a view on the flattened dictionary:
> d = {‘key’: ‘value’, ‘nested’: {‘key’: ‘another value’}} > wrapper = DotNotationWrapper(d) > wrapper[‘nested.key’] ‘another value’ > list(wrapper.keys()) [‘key’, ‘nested.key’]
Iteration only considers the flattened view, and keys that contain dictionaries will not be yielded when using wrapper.keys(), wrapper.values(), and wrapper.items(). Whenever the python standard behavior is needed, the underlying dictionary is available from the .data attribute.
- class onyo.lib.utils.YAMLDumpWrapper(d)[source]
Bases:
UserDictWrapper class for asset dicts accessing ruamel’s representation of data rather than the provided object.
This works around the issue that something like serial: 001234 yields a {‘serial’: 1234} but is dumped as serial: 001234, which messes up onyo’s comparisons for whether there’s a modification of an asset.
- onyo.lib.utils.deduplicate(sequence)[source]
Deduplicate a list and preserve its order.
The first occurrence of a value is kept. All later occurrences are discarded.
For convenience, also accepts
Noneand returnsNonein that case.- Parameters:
sequence (
list|None) – List to deduplicate.- Return type:
list|None
- onyo.lib.utils.dict_to_asset_yaml(d)[source]
Convert a dictionary to a YAML string, stripped of reserved-keys.
Dictionaries that contain a map of comments (ruamel, etc) will have those comments included in the string.
See also
- Parameters:
d (
Union[Dict,UserDict]) – Dictionary to strip of reserved-keys and convert to a YAML string.- Return type:
str
- onyo.lib.utils.get_asset_content(asset_file)[source]
Get the contents of an asset as a dictionary.
If the asset file’s contents are not valid YAML, an error is printed.
- Parameters:
asset_file (
Path) – The Path of the asset file to get the contents of.- Return type:
dict[str,bool|float|int|str|Path]
- onyo.lib.utils.get_temp_file()[source]
Create and return the Path of a new temporary file.
- Return type:
Path
- onyo.lib.utils.has_unique_names(asset_files)[source]
Check files for unique file names.
If duplicates are found, an error is printed listing them.
- Parameters:
asset_files (
Set[Path]) – A set of files to check for the uniqueness of their file names.- Return type:
bool
- onyo.lib.utils.is_equal_assets_dict(a, b)[source]
Whether two asset dictionaries have the same content.
This accounts for comments in YAML. For this to return True, both assets need to be equal not only in terms of their key-value pairs, but also in terms of annotated comments.
This also accounts for nested dicts recursively.
- Return type:
bool
- onyo.lib.utils.validate_yaml(asset_files)[source]
Check files for valid YAML.
If files with invalid YAML are detected, an error is printed listing them.
- Parameters:
asset_files (
list[Path] |None) – A list of files to check for valid YAML.- Return type:
bool
- onyo.lib.utils.write_asset_file(path, asset)[source]
Write content to an asset file.
All
RESERVED_KEYSwill be stripped from the content before writing.- Parameters:
path (
Path) – The Path to write content to.asset (
Union[Dict,UserDict]) – A dictionary of content to write to the path.
- Return type:
None