""" CSV renderer for report generation. """ from .base_renderer import BaseRenderer from typing import Dict, Any, Tuple, List import csv import io class CsvRenderer(BaseRenderer): """Renders content to CSV format with format-specific extraction.""" @classmethod def get_supported_formats(cls) -> List[str]: """Return supported CSV formats.""" return ['csv'] @classmethod def get_format_aliases(cls) -> List[str]: """Return format aliases.""" return ['spreadsheet', 'table'] @classmethod def get_priority(cls) -> int: """Return priority for CSV renderer.""" return 70 def getExtractionPrompt(self, user_prompt: str, title: str) -> str: """Get CSV-specific extraction prompt.""" return f""" {user_prompt} Generate a comprehensive CSV report with the title: "{title}" CSV FORMAT REQUIREMENTS: - Create structured data in CSV format - Use proper CSV syntax with commas and quotes - Include headers for all columns - Structure data in rows and columns - Include source document information - Add metadata as additional rows CSV STRUCTURE: - First row: Headers (Section, Type, Heading, Content, Source) - Data rows: One per section/item - Use quotes around content that contains commas - Escape quotes properly - Include metadata rows at the end FORMATTING RULES: - Headers: Section, Type, Heading, Content, Source - Content: Escape commas and quotes, limit length - Source: Include document name and page if available - Metadata: Add special rows for generation info OUTPUT POLICY: - Return ONLY CSV data - No markdown, no code blocks, no additional text - Properly formatted CSV - Include all necessary information - Valid CSV that can be imported Generate the complete CSV report: """ async def render(self, extracted_content: str, title: str) -> Tuple[str, str]: """Render extracted content to CSV format.""" try: # The extracted content should already be CSV from the AI # Just clean it up csv_content = self._clean_csv_content(extracted_content, title) return csv_content, "text/csv" except Exception as e: self.logger.error(f"Error rendering CSV: {str(e)}") # Return minimal CSV fallback return f"Title,Content\n{title},Error rendering report: {str(e)}", "text/csv" def _clean_csv_content(self, content: str, title: str) -> str: """Clean and validate CSV content from AI.""" content = content.strip() # Remove markdown code blocks if present if content.startswith("```") and content.endswith("```"): lines = content.split('\n') if len(lines) > 2: content = '\n'.join(lines[1:-1]).strip() return content