MultiFileMixin¶
Provides functionality to load and merge multiple files into a single dictionary. It uses the Strategy pattern for different merge approaches and integrates with the existing cache system.
Key Features¶
- Multiple merge strategies (deep, namespace, priority, conditional, append, replace)
- File pattern support (glob patterns)
- Integration with existing cache system
- Conditional merging based on environment or context
- Namespace prefixing for organized data structure
- Performance optimization with lazy loading
Methods¶
load_multiple_files¶
def load_multiple_files(
self,
file_paths: Union[List[Union[str, Path]], str],
strategy: Union[str, MergeStrategy, BaseMergeStrategy] = MergeStrategy.DEEP,
file_patterns: Optional[List[str]] = None,
conditional_filter: Optional[Callable[[str, Dict[str, Any]], bool]] = None,
use_cache: bool = True,
**kwargs: Any,
) -> Dict[str, Any]
Load and merge multiple files into a single dictionary.
Parameters:
- file_paths (Union[List[Union[str, Path]], str]): List of file paths or single path/pattern string
- strategy (Union[str, MergeStrategy, BaseMergeStrategy]): Strategy to use for merging files
- file_patterns (Optional[List[str]]): Optional list of glob patterns to expand
- conditional_filter (Optional[Callable[[str, Dict[str, Any]], bool]]): Optional function to filter files based on content
- use_cache (bool): Whether to use caching for loaded files
- **kwargs (Any): Additional arguments passed to the strategy
Returns:
- Dict[str, Any]: Dictionary containing merged data from all files
Example:
# Deep merge multiple files
data = fm.load_multiple_files([
"config.json",
"secrets.json"
], strategy="deep")
# Namespace merge with prefix
data = fm.load_multiple_files([
"database.json",
"cache.toml"
], strategy="namespace", namespace_prefix="app")
# Use file patterns
data = fm.load_multiple_files(
"config/*.json",
strategy="deep"
)
get_available_merge_strategies¶
Get list of available merge strategies.
Returns:
- List[str]: List of strategy names
Example:
invalidate_multi_file_cache¶
Invalidate cache for a specific file in multi-file operations.
Parameters:
- file_path (Union[str, Path]): Path to the file to invalidate
Example:
load_file_group¶
def load_file_group(
self,
group_name: str,
config: Dict[str, Any],
**kwargs: Any
) -> Dict[str, Any]
Load a predefined group of files based on configuration.
Parameters:
- group_name (str): Name of the file group
- config (Dict[str, Any]): Configuration dictionary defining the group
- **kwargs (Any): Additional arguments for file loading
Returns:
- Dict[str, Any]: Merged dictionary from the file group
Example: