Formatters factory
This commit is contained in:
42
redmine_reporter/formatters/factory.py
Normal file
42
redmine_reporter/formatters/factory.py
Normal file
@@ -0,0 +1,42 @@
|
||||
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
|
||||
Reference in New Issue
Block a user