doFolder.path module

This module provides utility functions for handling and formatting paths within the doFolder package.

doFolder.path.relativePathableFormat(path: str | Path | Iterable[str], relativeTo: Path) Tuple[str, ...]

Converts a given path into a tuple representation relative to a base path.

This function accepts a path in various formats (e.g., string, Path object, or an iterable) and converts it into a tuple of strings representing the relative path components. If the input path is already a tuple, it is returned as-is. If the path is absolute, it is made relative to the provided base path.

Parameters:
  • path (_tt.RelativePathable) – The input path to be formatted. It can be: - A string representing a path. - A Path object. - An iterable of path components.

  • relativeTo (Path) – The base path to which the input path will be made relative. This must be an absolute path if the input path is absolute.

Returns:

A tuple of strings representing the relative path components.

Return type:

_tt.Tuple[str, …]

Raises:
  • ValueError – If the input path is invalid, empty, or cannot be made relative to the base path.

  • TypeError – If the input path cannot be converted into a tuple.

Examples

>>> from pathlib import Path
>>> relativePathableFormat("subdir/file.txt", Path("/base"))
('subdir', 'file.txt')
>>> relativePathableFormat(Path("/base/subdir/file.txt"), Path("/base"))
('subdir', 'file.txt')
>>> relativePathableFormat(("subdir", "file.txt"), Path("/base"))
('subdir', 'file.txt')