Add report_generation/src/report_generation/cli.py
This commit is contained in:
parent
ed76bfee9f
commit
b5678d9932
1 changed files with 80 additions and 0 deletions
80
report_generation/src/report_generation/cli.py
Normal file
80
report_generation/src/report_generation/cli.py
Normal file
|
|
@ -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()
|
||||||
Loading…
Reference in a new issue