Strategies¶
BaseFileStrategy¶
Abstract base protocol for all file handling strategies.
Methods¶
load¶
Load data from a file.
Parameters:
- file_path (Union[Path, str]): Path to the file to load
Returns:
- Any: The parsed file contents, typically a dictionary or list
Raises:
- FileNotFoundError: If the file doesn't exist
- ValueError: If the file format is invalid
Example:
save¶
Save data to a file.
Parameters:
- file_path (Union[Path, str]): Path where to save the file
- data (Any): The data to save, typically a dictionary or list
Raises:
- PermissionError: If the file cannot be written due to permissions
- ValueError: If the data cannot be serialized to the target format
Example:
strategy = TomlStrategy()
data = {"database": {"host": "localhost", "port": 5432}}
strategy.save("config.toml", data)
navigate¶
Navigate through the document structure.
Parameters:
- document (Any): The document to navigate through
- path (List[str]): List of keys representing the path to traverse
- create (bool): Whether to create missing intermediate structures
Returns:
- Optional[Any]: The value at the specified path, or None if not found and create is False
Example:
strategy = TomlStrategy()
document = {"database": {"host": "localhost"}}
value = strategy.navigate(document, ["database", "host"])
print(value) # "localhost"
# Create missing path
value = strategy.navigate(document, ["cache", "redis"], create=True)
print(document) # {"database": {...}, "cache": {"redis": None}}
JsonStrategy¶
Strategy for handling JSON files.
Features: - Standard JSON with pretty printing - 2-space indentation - UTF-8 encoding support
Example:
TomlStrategy¶
Strategy for handling TOML files.
Features: - Full TOML specification support - Comment and formatting preservation - Type-safe operations with tomlkit - Support for nested tables and arrays
Example:
YamlStrategy¶
Strategy for handling YAML files.
Features: - YAML 1.2 with safe loading - UTF-8 encoding support - Pretty printing with proper indentation
Example: