Add formatters module
This commit is contained in:
24
src/contextgen/formatters/base.py
Normal file
24
src/contextgen/formatters/base.py
Normal file
@@ -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")
|
||||||
Reference in New Issue
Block a user