Add mini_reporting_block/src/mini_reporting_block/cli.py
This commit is contained in:
parent
71495d1b72
commit
1198dc404d
1 changed files with 60 additions and 0 deletions
60
mini_reporting_block/src/mini_reporting_block/cli.py
Normal file
60
mini_reporting_block/src/mini_reporting_block/cli.py
Normal file
|
|
@ -0,0 +1,60 @@
|
||||||
|
import argparse
|
||||||
|
import json
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from mini_reporting_block.core import generate_reporting_block
|
||||||
|
|
||||||
|
|
||||||
|
def _load_analysis_results(input_path: Path) -> list[dict]:
|
||||||
|
if not input_path.exists() or not input_path.is_file():
|
||||||
|
raise FileNotFoundError(f"Eingabedatei nicht gefunden: {input_path}")
|
||||||
|
try:
|
||||||
|
with input_path.open('r', encoding='utf-8') as f:
|
||||||
|
data = json.load(f)
|
||||||
|
if not isinstance(data, list):
|
||||||
|
raise ValueError("Analyseergebnisse müssen eine Liste von Objekten sein.")
|
||||||
|
if not all(isinstance(item, dict) for item in data):
|
||||||
|
raise ValueError("Jedes Analyseergebnis muss ein Dictionary sein.")
|
||||||
|
return data
|
||||||
|
except json.JSONDecodeError as e:
|
||||||
|
raise ValueError(f"Ungültiges JSON-Format in {input_path}: {e}") from e
|
||||||
|
|
||||||
|
|
||||||
|
def _save_output(output_path: Path, content: dict) -> None:
|
||||||
|
output_path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
with output_path.open('w', encoding='utf-8') as f:
|
||||||
|
json.dump(content, f, indent=2, ensure_ascii=False)
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
parser = argparse.ArgumentParser(description="Mini Reporting Block Generator für Δt<0 Analysen")
|
||||||
|
parser.add_argument('--input', required=True, help='Pfad zur Eingabedatei mit Analyseergebnissen als JSON')
|
||||||
|
parser.add_argument('--output', required=False, help='Optionaler Pfad zur Ausgabedatei für das Reporting')
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
input_path = Path(args.input)
|
||||||
|
output_path: Optional[Path] = Path(args.output) if args.output else None
|
||||||
|
|
||||||
|
analysis_results = _load_analysis_results(input_path)
|
||||||
|
report_text = generate_reporting_block(analysis_results)
|
||||||
|
|
||||||
|
print(report_text)
|
||||||
|
|
||||||
|
# Wenn Output-Datei angegeben, schreibe JSON-Ausgabe
|
||||||
|
if output_path:
|
||||||
|
try:
|
||||||
|
# Falls core.generate_reporting_block auch JSON zurückgeben könnte, hier simulieren wir einen Umwandlungsschritt
|
||||||
|
# Der Text kann z. B. aus ReportingBlock.to_json stammen, wenn dort generiert
|
||||||
|
report_json = {
|
||||||
|
"summary": report_text,
|
||||||
|
"details": analysis_results
|
||||||
|
}
|
||||||
|
_save_output(output_path, report_json)
|
||||||
|
except Exception as e:
|
||||||
|
raise RuntimeError(f"Fehler beim Schreiben der Ausgabe: {e}") from e
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
Loading…
Reference in a new issue