Add 1_interferenz_metrics_parser/src/interferenz_metrics_parser/cli.py
This commit is contained in:
parent
377877040a
commit
95127b4826
1 changed files with 51 additions and 0 deletions
|
|
@ -0,0 +1,51 @@
|
||||||
|
import argparse
|
||||||
|
import json
|
||||||
|
from pathlib import Path
|
||||||
|
import sys
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from interferenz_metrics_parser.core import parse_interference_logs
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
"""CLI Entry point for Interference Metrics Parser.
|
||||||
|
|
||||||
|
Parses command-line arguments, invokes the parsing core, and outputs JSON to stdout.
|
||||||
|
"""
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description="Parst eine Log-Datei mit Interferenzdaten und gibt strukturierte Metriken als JSON aus."
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--input",
|
||||||
|
required=True,
|
||||||
|
help="Pfad zur JSON-Logdatei mit Interferenzdaten.",
|
||||||
|
type=str,
|
||||||
|
)
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
log_file_path = Path(args.input)
|
||||||
|
|
||||||
|
# Input validation
|
||||||
|
if not log_file_path.exists():
|
||||||
|
print(f"Fehler: Datei nicht gefunden: {log_file_path}", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
if not log_file_path.is_file():
|
||||||
|
print(f"Fehler: Pfad ist keine Datei: {log_file_path}", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
try:
|
||||||
|
parsed_data = parse_interference_logs(str(log_file_path))
|
||||||
|
except Exception as exc: # noqa: BLE001 - allgemeine Fehlermeldung für CLI
|
||||||
|
print(f"Fehler beim Verarbeiten der Datei: {exc}", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
# Convert data models to JSON-compatible structures
|
||||||
|
def to_json_obj(item: Any) -> Any:
|
||||||
|
if hasattr(item, "__dict__"):
|
||||||
|
return item.__dict__
|
||||||
|
return item
|
||||||
|
|
||||||
|
json_output = json.dumps([to_json_obj(item) for item in parsed_data], indent=2, ensure_ascii=False)
|
||||||
|
print(json_output)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Loading…
Reference in a new issue