|
@@ -1,14 +1,16 @@
|
|
|
-"""CSV export helpers for parsed bus data."""
|
|
|
|
|
|
|
+"""CSV export helpers for parsed bus data and parameters."""
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
+import csv
|
|
|
from pathlib import Path
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
+from .constants import TYPE_DISPLAY_NAMES
|
|
|
from .models import ParsedLog
|
|
from .models import ParsedLog
|
|
|
|
|
|
|
|
|
|
|
|
|
class CsvExporter:
|
|
class CsvExporter:
|
|
|
- """Export each bus table in a parsed log to an individual CSV file."""
|
|
|
|
|
|
|
+ """Export each bus table and, when present, all parameters to CSV files."""
|
|
|
|
|
|
|
|
def export_all(self, parsed_log: ParsedLog, output_dir: str | Path) -> list[Path]:
|
|
def export_all(self, parsed_log: ParsedLog, output_dir: str | Path) -> list[Path]:
|
|
|
output_path = Path(output_dir)
|
|
output_path = Path(output_dir)
|
|
@@ -19,4 +21,29 @@ class CsvExporter:
|
|
|
csv_path = output_path / f"{bus_name}.csv"
|
|
csv_path = output_path / f"{bus_name}.csv"
|
|
|
bus_frame.frame.to_csv(csv_path, index=False)
|
|
bus_frame.frame.to_csv(csv_path, index=False)
|
|
|
written_files.append(csv_path)
|
|
written_files.append(csv_path)
|
|
|
|
|
+
|
|
|
|
|
+ if parsed_log.header.parameter_groups:
|
|
|
|
|
+ # Keep bus filenames unchanged, even if a bus is named "parameters".
|
|
|
|
|
+ bus_filenames = {path.name.casefold() for path in written_files}
|
|
|
|
|
+ filename = "parameters.csv"
|
|
|
|
|
+ suffix = 1
|
|
|
|
|
+ while filename.casefold() in bus_filenames:
|
|
|
|
|
+ filename = f"parameters_{suffix}.csv"
|
|
|
|
|
+ suffix += 1
|
|
|
|
|
+
|
|
|
|
|
+ csv_path = output_path / filename
|
|
|
|
|
+ with csv_path.open("w", encoding="utf-8", newline="") as stream:
|
|
|
|
|
+ writer = csv.writer(stream)
|
|
|
|
|
+ writer.writerow(["group", "name", "type", "value"])
|
|
|
|
|
+ for group in parsed_log.header.parameter_groups:
|
|
|
|
|
+ for parameter in group.parameters:
|
|
|
|
|
+ writer.writerow(
|
|
|
|
|
+ [
|
|
|
|
|
+ group.name,
|
|
|
|
|
+ parameter.name,
|
|
|
|
|
+ TYPE_DISPLAY_NAMES.get(parameter.type_id, str(parameter.type_id)),
|
|
|
|
|
+ parameter.value,
|
|
|
|
|
+ ]
|
|
|
|
|
+ )
|
|
|
|
|
+ written_files.append(csv_path)
|
|
|
return written_files
|
|
return written_files
|