Pywrapid Utils

Documentaiton coming soon

Dict tools

Collection of helper functions

dict_keys_exist(data: dict, expected_keys: list, allow_empty: bool = False, raise_on_fail: bool = True) bool[source]

Validates if expected keys exist (optionally; and has data) in a given dict.

Parameters:
  • data (dict) – The dict to run validation on.

  • expected_keys (list) – List of keys that should exist.

  • allow_empty (bool, optional) – Is key object allowed to have empty value.

  • raise_on_fail (bool, optional) – Raise if validation fails.

Raises:

ValueError – Raised on all validation errors when raise_on_fail = True.

Returns:

True when validation is passed.

Return type:

bool

dict_merge(base: dict, data: dict, path: list | None = None, raise_on_conflict: bool = False) dict[source]

Recursive merge of dict objects into new dict

The merge will make a deep copy of the base dict and merge the data dict into it. It will not modify the base dict being passed in.

If the same key exists in base and data, the value from data will be used unless raise_on_conflict is True, in which case a ValueError will be raised.

Parameters:
  • base (dict) – The base dict to merge into

  • data (dict) – The data dict to merge into base

  • path (list, optional) – _description_. Defaults to None.

  • raise_on_conflict (bool) – Raise on conflict instead of overwriting base

Raises:

ValueError – Raised on leaf conflict when raise_on_conflict is True

Returns: Merged dict

Filesystem tools

Collection of file helper functions

find_directory_content(path: str, depth: int = 0, **options: Any) list[dict][source]

Get directory content and allow os walk. Includes sub levels of user defined depth with metadata and item names as list of dict return.

Parameters:
  • path (str) – Path to configuration file.

  • depth (int, optional) – Depth to walk. Defaults to 0/unlimited.

Options:

exclude_files (bool, optional): Exclude files from return. Defaults to False. exclude_directories (bool, optional): Exclude directories from return. Defaults to False. exclude_symlinks (bool, optional): Exclude symlinks from return. Defaults to False. exclude_pattern (str, optional): Pattern to exclude. Defaults to “”. follow_symlinks (bool, optional): Follow symlinks. Defaults to False. order_by (str, optional): Order by key. Defaults to “path”. Not “symlink” or “mount_point” order (str, optional): Order by direction. Defaults to “desc”. “asc”|”desc”

Returns:

List of dict with metadata and item names

type (file, directory, symlink), name, path, size, (file only) access time, create time, modify time, group, owner, permissions, inode mount_point (directory only) symlink (symlink only)

Return type:

list[dict]

get_metadata(path: str) dict[source]

Internal function to get filesystem object metadata

Parameters:

path (str) – Path to fileystem object.

Returns:

Filesystem object metadata.

name (str), path (str), size (int) Files only, access time (int or float), crete time (int or float), modify time (int or float), group (int), owner (int), permissions (oct), inode (int), mount_point (bool) directory only, symlink (symlink only), type (str) file|directory|symlink,

Return type:

dict

is_directory_readable(path: str) bool[source]
Checks a directory in a given path to make sure it:
  • exists

  • is a directory

  • is readable

Parameters:

path (str) – Path to configuration file.

Returns:

True/False.

Return type:

bool

is_directory_writable(path: str) bool[source]
Checks a directory in a given path to make sure it:
  • exists

  • is a directory

  • is writable

Parameters:

path (str) – Path to configuration file.

Returns:

True/False.

Return type:

bool

is_file_readable(path: str) bool[source]
Checks a file in a given path to make sure it:
  • exists

  • is a file

  • is readable

  • is not an empty file

Parameters:

path (str) – Path to configuration file.

Returns:

True/False.

Return type:

bool

is_file_writable(path: str) bool[source]
Checks a file in a given path to make sure it:
  • exists

  • is a file

  • is writable

Parameters:

path (str) – Path to configuration file.

Returns:

True/False.

Return type:

bool