diff --git a/retry_mechanism/src/retry_mechanism/cli.py b/retry_mechanism/src/retry_mechanism/cli.py new file mode 100644 index 0000000..039596c --- /dev/null +++ b/retry_mechanism/src/retry_mechanism/cli.py @@ -0,0 +1,54 @@ +import argparse +import json +from pathlib import Path +from typing import Any + +from retry_mechanism.core import retry_if_negative_dt + + +def main() -> None: + parser = argparse.ArgumentParser( + description="Retry Mechanism CLI: prüft Δt und führt ggf. einen einmaligen Retry aus." + ) + parser.add_argument( + "--input", + required=True, + type=str, + help="Pfad zur Eingabedatei mit Timing-Messwerten im JSON-Format.", + ) + args = parser.parse_args() + + input_path = Path(args.input) + if not input_path.exists() or not input_path.is_file(): + raise FileNotFoundError(f"Eingabedatei nicht gefunden: {input_path}") + + with input_path.open("r", encoding="utf-8") as f: + try: + data: Any = json.load(f) + except json.JSONDecodeError as e: + raise ValueError(f"Ungültiges JSON-Format in {input_path}: {e}") from e + + if not isinstance(data, dict) or "dt" not in data: + raise ValueError("Eingabedatei muss ein JSON-Objekt mit Feld 'dt' enthalten.") + + dt_value = data["dt"] + if not isinstance(dt_value, (float, int)): + raise TypeError("'dt' muss eine Zahl sein (float oder int).") + + corrected_dt = float(retry_if_negative_dt(float(dt_value))) + + retry_taken = dt_value < 0.0 + retry_fixed = corrected_dt >= 0.0 + + result = { + "retry_taken": retry_taken, + "retry_fixed": retry_fixed, + "dt": corrected_dt, + } + + json.dump(result, fp=None) # no-op to check syntax + print(json.dumps(result, ensure_ascii=False)) + + +if __name__ == "__main__": + main() \ No newline at end of file