Registry¶
FileStrategyRegistry¶
Registry for managing file strategies.
Class Methods¶
register_strategy¶
@classmethod
def register_strategy(
cls,
file_exts: Union[str, List[str]],
strategy_cls: Type[BaseFileStrategy]
) -> None
Register one or multiple extensions for a strategy class.
Parameters:
- file_exts (Union[str, List[str]]): File extension(s) to register the strategy for
- strategy_cls (Type[BaseFileStrategy]): Strategy class to register
Raises:
- TypeError: If the strategy does not inherit from BaseFileStrategy
Example:
from yapfm.strategies import JsonStrategy
# Register single extension
FileStrategyRegistry.register_strategy(".json", JsonStrategy)
# Register multiple extensions
FileStrategyRegistry.register_strategy([".json", ".jsonc"], JsonStrategy)
unregister_strategy¶
Unregister a strategy for a file extension.
Parameters:
- file_ext (str): File extension to unregister the strategy for
Example:
get_strategy¶
Get a strategy for a file extension or path.
Parameters:
- file_ext_or_path (str): File extension or path to get the strategy for
Returns:
- Optional[BaseFileStrategy]: The strategy for the file extension or path
Example:
# Get strategy by extension
strategy = FileStrategyRegistry.get_strategy(".json")
# Get strategy by path
strategy = FileStrategyRegistry.get_strategy("config.json")
list_strategies¶
List all registered strategies.
Returns:
- Dict[str, Type[BaseFileStrategy]]: Dictionary mapping extensions to strategy classes
Example:
strategies = FileStrategyRegistry.list_strategies()
print(strategies) # {'.json': <class 'JsonStrategy'>, '.toml': <class 'TomlStrategy'>}
get_supported_formats¶
Get the supported formats for all registered strategies.
Returns:
- List[str]: List of supported file extensions
Example:
is_format_supported¶
Check if a format is supported.
Parameters:
- file_ext (str): File extension to check
Returns:
- bool: True if the format is supported, False otherwise
Example:
register_file_strategy¶
Decorator to register a strategy for one or more formats.
Usage¶
@register_file_strategy(".json")
class MyJsonStrategy:
def load(self, file_path):
# Implementation
pass
def save(self, file_path, data):
# Implementation
pass
def navigate(self, document, path, create=False):
# Implementation
pass
Parameters:
- file_exts (Union[str, List[str]]): The extensions to register the strategy for
- registry (Type[FileStrategyRegistry]): The registry to register the strategy for
Example: