|
| 1 | +from pathlib import Path |
| 2 | +from typing import ClassVar, Literal |
| 3 | + |
| 4 | +from project.common.utils.file.base import FileHandler |
| 5 | +from project.common.utils.file.json import JsonFileHandler |
| 6 | +from project.common.utils.file.toml import TomlFileHandler |
| 7 | +from project.common.utils.file.xml import XmlFileHandler |
| 8 | +from project.common.utils.file.yaml import YamlFileHandler |
| 9 | + |
| 10 | +FileFormat = Literal['json', 'yaml', 'toml', 'xml'] |
| 11 | + |
| 12 | + |
| 13 | +class FileHandlerFactory: |
| 14 | + """Factory for creating file handlers based on file format.""" |
| 15 | + |
| 16 | + _handlers: ClassVar[dict[FileFormat, type[FileHandler]]] = { |
| 17 | + 'json': JsonFileHandler, |
| 18 | + 'yaml': YamlFileHandler, |
| 19 | + 'toml': TomlFileHandler, |
| 20 | + 'xml': XmlFileHandler, |
| 21 | + } |
| 22 | + |
| 23 | + @classmethod |
| 24 | + def create(cls, format_type: FileFormat) -> FileHandler: |
| 25 | + """Create a file handler for the specified format. |
| 26 | +
|
| 27 | + Args: |
| 28 | + format_type: File format ('json', 'yaml', or 'toml') |
| 29 | +
|
| 30 | + Returns: |
| 31 | + File handler instance for the specified format |
| 32 | +
|
| 33 | + Raises: |
| 34 | + ValueError: If format_type is not supported |
| 35 | +
|
| 36 | + """ |
| 37 | + handler_class = cls._handlers.get(format_type) |
| 38 | + if handler_class is None: |
| 39 | + supported = ', '.join(cls._handlers.keys()) |
| 40 | + msg = f'Unsupported file format: {format_type}. Supported formats: {supported}' |
| 41 | + raise ValueError(msg) |
| 42 | + return handler_class() |
| 43 | + |
| 44 | + @classmethod |
| 45 | + def from_path(cls, path: str | Path) -> FileHandler: |
| 46 | + """Create a file handler by detecting format from file extension. |
| 47 | +
|
| 48 | + Args: |
| 49 | + path: File path with extension |
| 50 | +
|
| 51 | + Returns: |
| 52 | + File handler instance for the detected format |
| 53 | +
|
| 54 | + Raises: |
| 55 | + ValueError: If file extension is not recognized or missing |
| 56 | +
|
| 57 | + """ |
| 58 | + suffix = Path(path).suffix.lstrip('.') |
| 59 | + if not suffix: |
| 60 | + msg = f'Cannot detect file format: no extension in {path}' |
| 61 | + raise ValueError(msg) |
| 62 | + |
| 63 | + # Map common extensions to format types |
| 64 | + extension_map: dict[str, FileFormat] = { |
| 65 | + 'json': 'json', |
| 66 | + 'yaml': 'yaml', |
| 67 | + 'yml': 'yaml', |
| 68 | + 'toml': 'toml', |
| 69 | + 'xml': 'xml', |
| 70 | + } |
| 71 | + |
| 72 | + format_type = extension_map.get(suffix.lower()) |
| 73 | + if format_type is None: |
| 74 | + supported = ', '.join(extension_map.keys()) |
| 75 | + msg = f'Unsupported file extension: .{suffix}. Supported extensions: {supported}' |
| 76 | + raise ValueError(msg) |
| 77 | + |
| 78 | + return cls.create(format_type) |
| 79 | + |
| 80 | + |
| 81 | +def get_file_handler(path: str | Path) -> FileHandler: |
| 82 | + """Get a file handler from a file path. |
| 83 | +
|
| 84 | + Args: |
| 85 | + path: File path with extension |
| 86 | +
|
| 87 | + Returns: |
| 88 | + File handler instance for the detected format |
| 89 | +
|
| 90 | + """ |
| 91 | + return FileHandlerFactory.from_path(path) |
0 commit comments