rics.paths#

Utility methods for working with paths and filesystems.

Module Attributes

AnyPath

Reexport of rics.types.AnyPath.

PathT

A specific path-like type.

Functions

any_path_to_path(path, *[, postprocessor])

Convert a path of any type to Path.

any_path_to_str(path, *[, postprocessor])

Convert a path of any type to str.

parse_any_path(path, *, cls[, postprocessor])

Convert a path of any type to of type cls.

AnyPath = str | os.PathLike[str] | pathlib.Path#

Reexport of rics.types.AnyPath.

class PathT#

A specific path-like type.

alias of TypeVar(‘PathT’, str, ~pathlib.Path)

any_path_to_str(path: str | PathLike[str] | Path, *, postprocessor: Callable[[str], str | bool | None] | None = None) str[source]#

Convert a path of any type to str.

Equivalent to parse_any_path(path, cls=str).

Parameters:
  • path – Any path-like type.

  • postprocessor – A callable (str) -> str | None. Returns the post-processed value if postprocessor does not return None | bool.

Returns:

A path of type str.

any_path_to_path(path: str | PathLike[str] | Path, *, postprocessor: Callable[[Path], Path | bool | None] | None = None) Path[source]#

Convert a path of any type to Path.

Equivalent to parse_any_path(path, cls=Path).

Parameters:
  • path – Any path-like type.

  • postprocessor – A callable (Path) -> Path | None. Returns the post-processed value if postprocessor does not return None | bool.

Returns:

A path of type Path.

parse_any_path(path: str | PathLike[str] | Path, *, cls: type[PathT], postprocessor: Callable[[PathT], PathT | bool | None] | None = None) PathT[source]#

Convert a path of any type to of type cls.

Parameters:
  • path – Any path-like type.

  • cls – Desired output type; typically one of str | pathlib.Path.

  • postprocessor – A callable (PathT) -> PathT | bool | None. Returns the post-processed value if postprocessor does not return None | bool.

Returns:

A path of type of type cls.

Examples

>>> from pathlib import Path
>>> parse_any_path("/home/dev/", cls=Path)
PosixPath('/home/dev')
>>> parse_any_path(Path("/home/dev/"), cls=Path)
PosixPath('/home/dev')

See also