diff --git a/artifact_1/src/artifact_1/cli.py b/artifact_1/src/artifact_1/cli.py new file mode 100644 index 0000000..5a30121 --- /dev/null +++ b/artifact_1/src/artifact_1/cli.py @@ -0,0 +1,64 @@ +import argparse +import datetime +import sys +import time +from pathlib import Path + +from artifact_1 import core + + +def _validate_int(value: str, arg_name: str) -> int: + try: + return int(value) + except ValueError as exc: + raise argparse.ArgumentTypeError(f"Argument {arg_name} muss eine ganze Zahl sein: {value}") from exc + + +def _parse_args(argv=None): + parser = argparse.ArgumentParser(description="CLI zur Protokollierung von Timestamps.") + parser.add_argument("--run-id", required=True, type=str, help="Bezeichner des Runs für die Protokollierung.") + parser.add_argument("--step-id", required=True, type=str, help="Bezeichner des Schritts für die Protokollierung.") + parser.add_argument( + "--tz-offset", + required=False, + type=_validate_int, + metavar="MINUTEN", + help="Optionaler Zeitzonenoffset in Minuten.", + ) + return parser.parse_args(argv) + + +def main(argv=None) -> None: + args = _parse_args(argv) + + epoch_ms = int(time.time() * 1000) + monotonic_ns = time.monotonic_ns() + + if args.tz_offset is not None: + tz_offset_minutes = args.tz_offset + else: + local_offset = int((datetime.datetime.now() - datetime.datetime.utcnow()).total_seconds() / 60) + tz_offset_minutes = local_offset + + # Validierung der Eingaben + assert isinstance(epoch_ms, int) and epoch_ms >= 0, "epoch_ms muss ein nicht-negativer int sein" + assert isinstance(monotonic_ns, int) and monotonic_ns >= 0, "monotonic_ns muss ein nicht-negativer int sein" + assert isinstance(tz_offset_minutes, int), "tz_offset_minutes muss int sein" + assert isinstance(args.run_id, str) and args.run_id, "run_id darf nicht leer sein" + assert isinstance(args.step_id, str) and args.step_id, "step_id darf nicht leer sein" + + try: + core.log_timestamps( + epoch_ms=epoch_ms, + monotonic_ns=monotonic_ns, + tz_offset_minutes=tz_offset_minutes, + run_id=args.run_id, + step_id=args.step_id, + ) + except Exception as exc: + sys.stderr.write(f"Fehler beim Protokollieren der Zeitstempel: {exc}\n") + sys.exit(1) + + +if __name__ == "__main__": + main()