From b5678d993283c39f036b857b4c23bb0a00ee1875 Mon Sep 17 00:00:00 2001 From: Mika Date: Mon, 30 Mar 2026 16:33:39 +0000 Subject: [PATCH] Add report_generation/src/report_generation/cli.py --- .../src/report_generation/cli.py | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 report_generation/src/report_generation/cli.py diff --git a/report_generation/src/report_generation/cli.py b/report_generation/src/report_generation/cli.py new file mode 100644 index 0000000..9606d73 --- /dev/null +++ b/report_generation/src/report_generation/cli.py @@ -0,0 +1,80 @@ +import argparse +import json +import sys +from pathlib import Path +from typing import Any + +import pandas as pd + +from report_generation import core + + +def _validate_input_file(path_str: str) -> Path: + path = Path(path_str) + if not path.exists() or not path.is_file(): + raise FileNotFoundError(f"Input file not found: {path}") + if path.suffix.lower() != ".json": + raise ValueError(f"Input file must be a JSON file, got {path.suffix}") + return path + + +def _validate_output_path(path_str: str) -> Path: + path = Path(path_str) + if not path.parent.exists(): + raise FileNotFoundError(f"Output directory does not exist: {path.parent}") + return path + + +def _load_analysis_results(input_path: Path) -> Any: + with input_path.open("r", encoding="utf-8") as f: + data = json.load(f) + # Input validation: expect list[dict] or dict + if isinstance(data, list): + return pd.DataFrame(data) + elif isinstance(data, dict): + return data + else: + raise TypeError( + f"Invalid input structure: expected list or dict, got {type(data)}" + ) + + +def main() -> None: + parser = argparse.ArgumentParser( + description="CLI zum Generieren eines Stabilitätsberichts aus Analyseergebnissen." + ) + parser.add_argument( + "--input", + required=True, + help="Pfad zur JSON-Datei mit den Analyseergebnissen.", + ) + parser.add_argument( + "--output", + required=True, + help="Zielpfad für die generierte Report-Datei.", + ) + + args = parser.parse_args() + + try: + input_path = _validate_input_file(args.input) + output_path = _validate_output_path(args.output) + analysis_results = _load_analysis_results(input_path) + + report_path = core.generate_report(analysis_results) + + # Falls generate_report Rückgabe nicht ins Ziel schreibt, kopiere ggf. + generated_path = Path(report_path) + if generated_path != output_path: + output_content = Path(report_path).read_text(encoding="utf-8") + output_path.write_text(output_content, encoding="utf-8") + + print(f"Report generated successfully: {output_path}") + + except Exception as e: + print(f"Error: {e}", file=sys.stderr) + sys.exit(1) + + +if __name__ == "__main__": + main()