Coverage for lintro / formatters / styles / csv.py: 100%
15 statements
« prev ^ index » next coverage.py v7.13.0, created at 2026-04-03 18:53 +0000
« prev ^ index » next coverage.py v7.13.0, created at 2026-04-03 18:53 +0000
1"""CSV output style implementation."""
3import csv
4import io
5from typing import Any
7from lintro.formatters.core.format_registry import OutputStyle
10class CsvStyle(OutputStyle):
11 """Output format that renders data as CSV."""
13 def format(
14 self,
15 columns: list[str],
16 rows: list[list[Any]],
17 tool_name: str | None = None,
18 **kwargs: Any,
19 ) -> str:
20 """Format a table given columns and rows as CSV.
22 Args:
23 columns: List of column header names.
24 rows: List of row values (each row is a list of cell values).
25 tool_name: Optional tool name to include in context.
26 **kwargs: Extra options ignored by this formatter.
28 Returns:
29 Formatted data as CSV string.
30 """
31 if not rows:
32 return ""
34 # Create a string buffer to write CSV data
35 output = io.StringIO()
36 writer = csv.writer(output)
38 # Write header
39 writer.writerow(columns)
41 # Write rows
42 for row in rows:
43 # Ensure row has same number of elements as columns
44 padded_row = row + [""] * (len(columns) - len(row))
45 writer.writerow(padded_row)
47 return output.getvalue()