From 59d4410f4c86bb501cf139af953899e82c1705de Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Thu, 29 Jan 2026 13:24:04 +0000 Subject: [PATCH] Add formatters module --- src/contextgen/formatters/base.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/contextgen/formatters/base.py 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")