Add formatters module
This commit is contained in:
24
src/contextgen/formatters/yaml_formatter.py
Normal file
24
src/contextgen/formatters/yaml_formatter.py
Normal file
@@ -0,0 +1,24 @@
|
||||
"""YAML formatter for structured output."""
|
||||
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
import yaml
|
||||
|
||||
|
||||
class YAMLFormatter:
|
||||
"""Formats context data as YAML."""
|
||||
|
||||
def __init__(self, output_path: Path | None = None):
|
||||
self.output_path = output_path
|
||||
|
||||
def format(self, data: dict[str, Any]) -> str:
|
||||
"""Format data as YAML string."""
|
||||
return yaml.dump(data, default_flow_style=False, allow_unicode=True, sort_keys=False)
|
||||
|
||||
def save(self, data: dict[str, Any]) -> None:
|
||||
"""Save formatted data to file."""
|
||||
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