diff --git a/src/contextgen/formatters/base.py b/src/contextgen/formatters/base.py new file mode 100644 index 0000000..d4650ca --- /dev/null +++ b/src/contextgen/formatters/base.py @@ -0,0 +1,24 @@ +"""Base formatter class for output formatting.""" + +from abc import ABC, abstractmethod +from pathlib import Path +from typing import Any + + +class BaseFormatter(ABC): + """Base class for all formatters.""" + + def __init__(self, output_path: Path | None = None): + self.output_path = output_path + + @abstractmethod + def format(self, data: dict[str, Any]) -> str: + """Format data and return as string.""" + pass + + def save(self, data: dict[str, Any]) -> None: + """Format data and save to output path.""" + if self.output_path: + self.output_path.parent.mkdir(parents=True, exist_ok=True) + content = self.format(data) + self.output_path.write_text(content, encoding="utf-8")