onyo.lib.utils module

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 None and returns None in that case.

Parameters:

sequence (list | None) – List to deduplicate.

Return type:

list | None

onyo.lib.utils.dict_to_yaml(d)[source]

Convert a dictionary to a YAML string.

Dictionaries that contain a map of comments (ruamel, etc) will have those comments included in the string.

Parameters:

d (Dict) – Dictionary to render as YAML.

Return type:

str

onyo.lib.utils.get_asset_content(asset_file)[source]

Get the contents of a Path as a dictionary.

Parameters:

asset_file (Path) – Path to get the contents of.

Raises:

NotAnAssetError – The YAML is invalid.

Return type:

dict

onyo.lib.utils.get_patched_yaml()[source]

Return a YAML object that interprets all keys and values as strings.

With the exception of YAML’s nulls, which are typed as None.

Return type:

YAML

onyo.lib.utils.get_temp_file(suffix='.yaml')[source]

Return the Path of a new temporary file.

Parameters:

suffix (default: '.yaml') – String to append to the filename. Passed to tempfile.mkstemp().

Return type:

Path

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_to_file(asset, path=None)[source]

Write asset content to a file.

Pseudokeys are not included in the written YAML.

Parameters:
  • asset (Item) – Item to write to disk.

  • path (Path | None (default: None)) – The Path to write content to. Default is the asset’s 'onyo.path.file' pseudokey.

Return type:

None

onyo.lib.utils.yaml_to_dict(s)[source]

Convert a YAML string to a dictionary.

YAML that contains comments will have them retained as a comment map.

Parameters:

s (str) – YAML string to load as a dictionary.

Raises:

NotAnAssetError – The YAML is invalid.

Return type:

Union[Dict, CommentedMap]

onyo.lib.utils.yaml_to_dict_multi(stream)[source]

Yield dictionaries from a (potential) multi-document YAML.

Return type:

Generator[dict | CommentedMap, None, None]