rics.paths#
Utility methods for working with paths and filesystems.
Module Attributes
Reexport of |
|
A specific path-like type. |
Functions
|
Convert a path of any type to |
|
Convert a path of any type to |
|
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-liketype.postprocessor – A callable
(str) -> str | None. Returns the post-processed value if postprocessor does not returnNone | 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-liketype.postprocessor – A callable
(Path) -> Path | None. Returns the post-processed value if postprocessor does not returnNone | 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-liketype.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 returnNone | bool.
- Returns:
A path of type of type cls.
- Raises:
TypeError – If the postprocessor returns an invalid type.
ValueError – If the postprocessor returns
False.
- Postprocessor return types:
None: Validator. Should raise if there’s an issue.bool: Validator, e.g.is_dir(). Raises aValueErrorif the function returnsFalse.PathT: Postprocessor, e.g.absolute(). Result is returned as-is.
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
any_path_to_str(): Alias ofparse_any_path(cls=str)any_path_to_path(): Alias ofparse_any_path(cls=pathlib.Path)