43 lines
1.5 KiB
Python
43 lines
1.5 KiB
Python
from typing import Dict, Type, Optional
|
||
from .base import Formatter
|
||
from .console import TableFormatter, CompactFormatter
|
||
from .csv import CSVFormatter
|
||
from .markdown import MarkdownFormatter
|
||
from .odt import ODTFormatter
|
||
|
||
|
||
# Словарь для сопоставления расширений файлов с классами форматтеров
|
||
FORMATTER_MAP: Dict[str, Type[Formatter]] = {
|
||
".odt": ODTFormatter,
|
||
".csv": CSVFormatter,
|
||
".md": MarkdownFormatter,
|
||
}
|
||
|
||
|
||
# Словарь для сопоставления типа вывода (консоль) с классами форматтеров
|
||
CONSOLE_FORMATTER_MAP: Dict[str, Type[Formatter]] = {
|
||
"table": TableFormatter,
|
||
"compact": CompactFormatter,
|
||
}
|
||
|
||
|
||
def get_formatter_by_extension(extension: str, **kwargs) -> Optional[Formatter]:
|
||
"""
|
||
Возвращает экземпляр форматтера по расширению файла.
|
||
Ключевые аргументы (**kwargs) передаются в конструктор форматтера.
|
||
"""
|
||
formatter_class = FORMATTER_MAP.get(extension.lower())
|
||
if formatter_class:
|
||
return formatter_class(**kwargs)
|
||
return None
|
||
|
||
|
||
def get_console_formatter(formatter_type: str) -> Optional[Formatter]:
|
||
"""
|
||
Возвращает экземпляр консольного форматтера по его типу.
|
||
"""
|
||
formatter_class = CONSOLE_FORMATTER_MAP.get(formatter_type.lower())
|
||
if formatter_class:
|
||
return formatter_class()
|
||
return None
|