Add log_handler/src/log_handler/cli.py
This commit is contained in:
parent
95156ff5d3
commit
198cc9bb85
1 changed files with 65 additions and 0 deletions
65
log_handler/src/log_handler/cli.py
Normal file
65
log_handler/src/log_handler/cli.py
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
import argparse
|
||||
import json
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from typing import Any, List
|
||||
|
||||
from log_handler import core
|
||||
|
||||
|
||||
def _setup_logger() -> None:
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format="%(asctime)s [%(levelname)s] %(message)s",
|
||||
)
|
||||
|
||||
|
||||
def _validate_input_file(file_path: str) -> Path:
|
||||
path = Path(file_path)
|
||||
if not path.exists() or not path.is_file():
|
||||
raise FileNotFoundError(f"Logdatei nicht gefunden: {file_path}")
|
||||
return path
|
||||
|
||||
|
||||
def _write_output(data: List[Any], output_path: Path) -> None:
|
||||
try:
|
||||
with output_path.open('w', encoding='utf-8') as f:
|
||||
json.dump([d.__dict__ if hasattr(d, '__dict__') else d for d in data], f, indent=2)
|
||||
logging.info(f"Ergebnis geschrieben nach: {output_path}")
|
||||
except Exception as e:
|
||||
logging.error(f"Fehler beim Schreiben der Ausgabedatei: {e}")
|
||||
raise
|
||||
|
||||
|
||||
def main() -> None:
|
||||
_setup_logger()
|
||||
|
||||
parser = argparse.ArgumentParser(description="CLI für den Langzeitbelichtung Log Analyzer")
|
||||
parser.add_argument("--input", required=True, help="Pfad zur Logdatei, die analysiert werden soll.")
|
||||
parser.add_argument("--output", required=False, help="Pfad für das Ausgabe-JSON mit den analysierten Daten.")
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
try:
|
||||
log_file = _validate_input_file(args.input)
|
||||
logging.info(f"Analysiere Logdatei: {log_file}")
|
||||
|
||||
parsed_data = core.parse_log_file(str(log_file))
|
||||
assert isinstance(parsed_data, list), "parse_log_file muss eine Liste zurückgeben."
|
||||
|
||||
if args.output:
|
||||
output_path = Path(args.output)
|
||||
else:
|
||||
output_path = Path("output/parsed_data.json")
|
||||
output_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
_write_output(parsed_data, output_path)
|
||||
logging.info("Analyse abgeschlossen.")
|
||||
|
||||
except Exception as err:
|
||||
logging.error(f"Fehler bei der Ausführung: {err}")
|
||||
raise SystemExit(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Loading…
Reference in a new issue