ksuit.utils.common.path

Functions

create_path_from_str(str_path[, mkdir, should_exist, ...])

Converts a string to a Path and optionally performs file-system operations on it.

validate_path(path[, exists, suffix, mkdir])

Converts a string to a Path, validates it, and optionally creates it.

select_with_path(obj, path)

Access values of an object, a list or a dictionary using a string path.

Module Contents

ksuit.utils.common.path.create_path_from_str(str_path, mkdir=False, should_exist=True, check_exists=True, suffix=None)

Converts a string to a Path and optionally performs file-system operations on it.

Parameters:
  • str_path (str) – The path to convert.

  • mkdir (bool) – If True, creates the directory if it doesn’t exist.

  • should_exist (bool) – If True, checks if the path exists.

  • check_exists (bool) – If True, checks if the path exists or not.

  • suffix (str | None) – If not None, checks if the path ends with this suffix.

Return type:

pathlib.Path

ksuit.utils.common.path.validate_path(path, exists='must', suffix=None, mkdir=False)

Converts a string to a Path, validates it, and optionally creates it.

Parameters:
  • path (str | pathlib.Path) – The path string to validate.

  • exists (Literal['must', 'must_not', 'any']) –

    • “must”: Raises FileNotFoundError if the path doesn’t exist.

    • ”must_not”: Raises FileExistsError if the path already exists.

    • ”any”: Performs no existence check.

  • suffix (str | None) – If provided, checks if the path ends with this suffix.

  • mkdir (bool) – If True, creates the directory path (like mkdir -p).

Returns:

The validated path.

Return type:

Path

ksuit.utils.common.path.select_with_path(obj, path)

Access values of an object, a list or a dictionary using a string path.

Parameters:
  • obj (dict[str, Any] | list[Any] | object) – The object to access.

  • path (str | None) – The path to the value, e.g. “a.b.c” or “a[0].b.c”.

Return type:

object