Add drift_detector/src/drift_detector/cli.py

This commit is contained in:
Mika 2026-02-14 15:32:02 +00:00
parent bae262abb7
commit 03498ae476

View file

@ -0,0 +1,51 @@
import argparse
import json
import sys
from pathlib import Path
from drift_detector 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'Eingabedatei nicht gefunden: {path}')
return path
def _validate_output_path(path_str: str | None) -> Path | None:
if not path_str:
return None
path = Path(path_str)
path.parent.mkdir(parents=True, exist_ok=True)
return path
def main() -> None:
"""CLI-Einstiegspunkt für die Drift-Erkennung."""
parser = argparse.ArgumentParser(description='Detect structural drift in key logs.')
parser.add_argument('--input', required=True, help='Pfad zur Eingabedatei im JSONL-Format mit Mess-Logs')
parser.add_argument('--output', required=False, help='Optionale Ausgabedatei für den Driftbericht')
args = parser.parse_args()
try:
input_path = _validate_input_file(args.input)
output_path = _validate_output_path(args.output)
drift_detected = core.detect_drift(str(input_path))
result = {
'input_file': str(input_path),
'drift_detected': drift_detected
}
if output_path:
with output_path.open('w', encoding='utf-8') as f:
json.dump(result, f, ensure_ascii=False, indent=2)
else:
json.dump(result, sys.stdout, ensure_ascii=False, indent=2)
sys.stdout.write('\n')
except Exception as e:
sys.stderr.write(f'Fehler: {e}\n')
sys.exit(1)
if __name__ == '__main__':
main()