Add report_generation/src/report_generation/cli.py

This commit is contained in:
Mika 2026-03-14 17:17:13 +00:00
parent 6c3be5ec4c
commit 4323b73aef

View file

@ -0,0 +1,57 @@
import argparse
import json
import sys
from pathlib import Path
from typing import Any, Dict
from datetime import datetime
# Keine öffentliche API in diesem Modul, dient als CLI-Entry.
def _validate_json_file(path: Path) -> bool:
"""Prüft, ob die Eingabedatei existiert und gültige JSON-Struktur enthält."""
if not path.exists():
raise FileNotFoundError(f"Eingabedatei nicht gefunden: {path}")
try:
with path.open('r', encoding='utf-8') as f:
json.load(f)
except json.JSONDecodeError as e:
raise ValueError(f"Ungültige JSON-Datei: {e}") from e
return True
def _run_cli() -> None:
"""CLI Entry Point für Berichtserzeugung."""
parser = argparse.ArgumentParser(description="CLI für Berichtserzeugung der Pi Day Repetition Study.")
parser.add_argument('--input', required=True, help='Pfad zur JSON-Datei mit Analyseergebnissen.')
parser.add_argument('--output', required=True, help='Pfad zur Ausgabedatei für den Bericht.')
args = parser.parse_args()
input_path = Path(args.input)
output_path = Path(args.output)
try:
_validate_json_file(input_path)
with input_path.open('r', encoding='utf-8') as infile:
analysis_results: Dict[str, Any] = json.load(infile)
# Späte Imports, um zyklische Abhängigkeiten zu vermeiden.
from report_generation.main import generate_report
report_file = generate_report(analysis_results)
# Optional: Kopiere generierte Datei an CLI-Ausgabeort, falls verschieden.
if Path(report_file) != output_path:
with open(report_file, 'r', encoding='utf-8') as src, output_path.open('w', encoding='utf-8') as dst:
dst.write(src.read())
print(f"Bericht erfolgreich erstellt: {output_path}")
except Exception as exc: # Fängt Validierungs- und Laufzeitfehler ab
timestamp = datetime.utcnow().isoformat()
sys.stderr.write(f"[{timestamp}] Fehler: {exc}\n")
sys.exit(1)
if __name__ == '__main__':
_run_cli()