fitzzftw.patch.utils¶
General Utilities and Configuration Handling¶
This module provides helper functions for filesystem operations, string normalization, and hierarchical configuration merging.
Core Functions:¶
- get_string_of_type:
A robust helper to extract human-readable names from type objects or strings. It ensures consistent naming even when dealing with complex Protocols or edge cases where a simple __name__ access might fail.
- get_backup_extension:
Normalizes backup extensions and handles dynamic keywords like ‘timestamp’ to generate ISO-compliant suffixes.
- get_merged_config:
Implements the configuration layering logic, merging project-specific TOML files with global user preferences.
Key Features:¶
- Configuration Priority:
Ensures that
pyproject.tomlsettings always override user-level defaults to maintain project standards.
- Platform Awareness:
Uses
platformdirs.user_config_path()to correctly resolve user configuration paths across different operating systems.
Functions
|
Normalize the backup extension and handle dynamic keywords. |
|
Merge configuration from hierarchical sources into a single dictionary. |
|
Extracts a human-readable string representation of a type. |
|
Replace predefined placeholders in a path string with an ISO timestamp. |
- get_backup_extension(ext, now=datetime.datetime(2026, 4, 2, 10, 1, 18, 305967))[source]¶
Normalize the backup extension and handle dynamic keywords.
This function cleans the input by removing outer whitespace and dots. If a keyword from DATETIME_KEYWORDS is detected, it is replaced by an ISO 8601 compliant timestamp.
- get_merged_config(app_name='ftw', manual_user_cfg='')[source]¶
Merge configuration from hierarchical sources into a single dictionary.
This function implements the configuration layering logic before the Pimple-object (args) is fully initialized. It resolves the ‘chicken-and-egg’ problem of locating the user configuration file via CLI before parsing the remaining arguments.
The priority order for settings (highest to lowest):
Project Configuration: Defined in ‘pyproject.toml’ under [tool.fitzzftw.patch]. This level ensures project-specific standards (e.g., .gitignore compliance) always override global user preferences.
Manual User Configuration: A TOML file specified via the ‘–userconfig’ flag.
Platform User Configuration: The default OS-specific path (e.g., ~/.config/ftw/patch.toml on Linux).
- Parameters:
- Raises:
tomllib.TOMLDecodeError – If any encountered TOML file is syntactically invalid.
OSError – If there are permission issues accessing the configuration files.
- Returns:
A dictionary containing the effective configuration defaults.
- Return type:
- get_string_of_type(protocol_type)[source]¶
Extracts a human-readable string representation of a type.
This helper is used during protocol introspection to handle both actual class objects and pre-defined type strings. If the input has a ‘__name__’ attribute (e.g., a class), it returns that name; otherwise, it returns the input as is.
- Parameters:
protocol_type – The type object or string to convert.
- Returns:
A string representation of the type.
- Return type:
- replace_keywords_to_isodatetime(path_str, now)[source]¶
Replace predefined placeholders in a path string with an ISO timestamp.
This function searches the string for tokens defined in DATETIME_KEYWORDS enclosed by ‘@’ characters. It replaces them with a timestamp generated from ‘now’ using the format YYYYMMDDTHHMMSS.