Add stability_analysis/src/stability_analysis/cli.py
This commit is contained in:
parent
7435c2e3e3
commit
a4e9e98165
1 changed files with 63 additions and 0 deletions
63
stability_analysis/src/stability_analysis/cli.py
Normal file
63
stability_analysis/src/stability_analysis/cli.py
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
import argparse
|
||||
import json
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
import sys
|
||||
import logging
|
||||
|
||||
from stability_analysis import core # type: ignore
|
||||
|
||||
|
||||
logging.basicConfig(level=logging.INFO, format='[%(levelname)s] %(message)s')
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _validate_input_path(input_path: Path) -> None:
|
||||
if not input_path.exists():
|
||||
raise FileNotFoundError(f"Eingabedatei nicht gefunden: {input_path}")
|
||||
if not input_path.is_file():
|
||||
raise ValueError(f"Eingabepfad ist keine Datei: {input_path}")
|
||||
|
||||
|
||||
def _validate_output_path(output_path: Path) -> None:
|
||||
out_dir = output_path.parent
|
||||
if not out_dir.exists():
|
||||
logger.info(f"Ausgabeverzeichnis wird erstellt: {out_dir}")
|
||||
out_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
|
||||
def main(argv: list[str] | None = None) -> None:
|
||||
parser = argparse.ArgumentParser(description="CLI zur Analyse der Stabilitaet von Laufzeitdaten.")
|
||||
parser.add_argument("--input", required=True, help="Pfad zur JSON-Datei mit Messdaten.")
|
||||
parser.add_argument("--output", required=True, help="Pfad für die Ausgabe der Analyseergebnisse.")
|
||||
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
input_path = Path(args.input)
|
||||
output_path = Path(args.output)
|
||||
|
||||
try:
|
||||
_validate_input_path(input_path)
|
||||
_validate_output_path(output_path)
|
||||
|
||||
with input_path.open('r', encoding='utf-8') as infile:
|
||||
data_list: Any = json.load(infile)
|
||||
|
||||
if not isinstance(data_list, list):
|
||||
raise ValueError("Eingabedatei muss eine Liste von Messwert-Dictionaries enthalten.")
|
||||
|
||||
logger.info("Starte Analyse der Laufzeitdaten...")
|
||||
results = core.analyze_data(data_list)
|
||||
|
||||
with output_path.open('w', encoding='utf-8') as outfile:
|
||||
json.dump(results, outfile, indent=2, ensure_ascii=False)
|
||||
|
||||
logger.info(f"Analyse abgeschlossen. Ergebnisse gespeichert unter: {output_path}")
|
||||
|
||||
except Exception as exc:
|
||||
logger.error(f"Fehler bei der Ausführung: {exc}")
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Loading…
Reference in a new issue