Path Utilities¶
The rite.filesystem.path submodule offers helpers for checking and manipulating filesystem paths.
Path helpers for filesystem package.
Modules¶
path_clean
¶
Path Cleaning Module¶
Provides path normalization and cleaning utilities.
Functions¶
path_clean(path: str) -> str
¶
Clean a path by normalizing slashes.
Ensures path has exactly one leading slash and no trailing slash.
path: Path string to clean.
str: Cleaned path with single leading slash.
Example:¶
>>> path_clean("//path/to/file//")
'/path/to/file'
>>> path_clean("path/to/file")
'/path/to/file'
path_exists
¶
path_is_dir
¶
path_is_file
¶
path_leaf
¶
Path Leaf Extraction¶
Get the leaf (final component) of a path.
Functions¶
path_leaf(path: str) -> str
¶
Return the last part (leaf) of a given file path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
The input file path |
required |
Returns:
| Type | Description |
|---|---|
str
|
The leaf name (file or folder name) of the path |
Example
path_leaf("/some/folder/file.txt") 'file.txt' path_leaf("Documents/project") 'project'
path_safe_join
¶
Safe Path Join Module¶
Provides safe path joining preventing traversal outside base directory.
Functions¶
path_safe_join(base_directory_path: str, *path_components: Any) -> str
¶
Safely join path components ensuring result stays under the base directory.
This function prevents directory traversal attacks by ensuring the resulting path never escapes the base directory.
base_directory_path: Base directory path.
*path_components: Path components to join.
str: Joined path relative to base (without leading slash).
ValueError: If joined path escapes base directory.
Example:¶
>>> path_safe_join("/var/data", "uploads", "file.txt")
'var/data/uploads/file.txt'
>>> path_safe_join("/var/data", "../etc/passwd")
Traceback (most recent call last):
ValueError: the joined path is located outside of the base path
path_secure
¶
Path Security Module¶
Provides secure path operations preventing traversal attacks.
Functions¶
path_secure(base_path: str | Path, user_path: str | Path) -> str
¶
Secure a file path against traversal attacks.
Normalizes and secures a file path, ensuring it stays within the base directory by using only the basename of the user path.
base_path: The base directory path.
user_path: The user-provided path to secure.
str: A secured path string.
Example:¶
>>> path_secure("/var/data", "../../../etc/passwd")
'/var/data/passwd'
options: show_root_heading: true show_source: false heading_level: 2