From 14df35ca07bcf2083920978553367895167814a6 Mon Sep 17 00:00:00 2001 From: Mika Date: Tue, 20 Jan 2026 12:12:24 +0000 Subject: [PATCH] Add trace_aggpy/src/trace_aggpy/cli.py --- trace_aggpy/src/trace_aggpy/cli.py | 56 ++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 trace_aggpy/src/trace_aggpy/cli.py diff --git a/trace_aggpy/src/trace_aggpy/cli.py b/trace_aggpy/src/trace_aggpy/cli.py new file mode 100644 index 0000000..7d8a03d --- /dev/null +++ b/trace_aggpy/src/trace_aggpy/cli.py @@ -0,0 +1,56 @@ +import argparse +import json +from pathlib import Path +from typing import Any + +from trace_aggpy.main import analyze_reads + + +def _load_json(path: Path) -> Any: + if not path.exists(): + raise FileNotFoundError(f"Input file does not exist: {path}") + with path.open('r', encoding='utf-8') as f: + data = json.load(f) + if not isinstance(data, list): + raise ValueError("Expected a list of read events in JSON input.") + return data + + +def _write_json(path: Path, data: dict) -> None: + with path.open('w', encoding='utf-8') as f: + json.dump(data, f, ensure_ascii=False, indent=2, sort_keys=True) + + +def main() -> None: + """CLI entry point: Reads JSON input, calls analyze_reads, writes summary output.""" + parser = argparse.ArgumentParser(description="Analyze N40 Read Events and output JSON summary.") + parser.add_argument("--input", required=True, help="Pfad zur JSON-Input-Datei mit Read-Events.") + parser.add_argument("--output", required=True, help="Pfad zur zu erzeugenden Summary-JSON.") + args = parser.parse_args() + + input_path = Path(args.input) + output_path = Path(args.output) + + read_data = _load_json(input_path) + + if not read_data: + raise ValueError("Input read data is empty.") + + corr_id = read_data[0].get('corr_id') if isinstance(read_data[0], dict) else None + if not corr_id or not isinstance(corr_id, str): + raise ValueError("Missing or invalid 'corr_id' in input data.") + + result = analyze_reads(corr_id=corr_id, read_data=read_data) + + if hasattr(result, 'to_json') and callable(getattr(result, 'to_json')): + output_data = result.to_json() + elif isinstance(result, dict): + output_data = result + else: + raise TypeError("Unexpected output type from analyze_reads. Expected dict or object with to_json().") + + _write_json(output_path, output_data) + + +if __name__ == "__main__": + main()