From 591e55928a323b3a2962e3527a2448e72fab79d9 Mon Sep 17 00:00:00 2001 From: Mika Date: Mon, 16 Mar 2026 13:59:13 +0000 Subject: [PATCH] Add simulation_tool/src/simulation_tool/cli.py --- simulation_tool/src/simulation_tool/cli.py | 65 ++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 simulation_tool/src/simulation_tool/cli.py diff --git a/simulation_tool/src/simulation_tool/cli.py b/simulation_tool/src/simulation_tool/cli.py new file mode 100644 index 0000000..1d00d49 --- /dev/null +++ b/simulation_tool/src/simulation_tool/cli.py @@ -0,0 +1,65 @@ +import argparse +import json +import sys +from pathlib import Path +import logging +from typing import Any, Dict + +from simulation_tool.core import simulate_scheduling + + +def _setup_logging() -> None: + logging.basicConfig( + level=logging.INFO, + format="[%(asctime)s] %(levelname)s:%(name)s: %(message)s", + ) + + +def _load_config(config_path: Path) -> Dict[str, Any]: + if not config_path.exists() or not config_path.is_file(): + raise FileNotFoundError(f"Config file not found: {config_path}") + with config_path.open("r", encoding="utf-8") as f: + try: + config = json.load(f) + except json.JSONDecodeError as e: + raise ValueError(f"Invalid JSON format in config file: {e}") from e + if not isinstance(config, dict): + raise ValueError("Loaded configuration must be a dictionary.") + return config + + +def main() -> None: + _setup_logging() + parser = argparse.ArgumentParser( + description="Simulation CLI for Resonanzband Scheduling Tool" + ) + parser.add_argument( + "--config", required=True, help="Pfad zur JSON-Konfigurationsdatei mit Scheduling-Parametern." + ) + parser.add_argument( + "--output", required=False, help="Pfad zur Ausgabe der Simulationsergebnisse im JSON-Format." + ) + + args = parser.parse_args() + config_path = Path(args.config) + output_path = Path(args.output) if args.output else Path("output/simulation_results.json") + + try: + schedule_params = _load_config(config_path) + logging.info("Starting scheduling simulation...%s", schedule_params) + results = simulate_scheduling(schedule_params) + + output_data = results.to_json() + output_path.parent.mkdir(parents=True, exist_ok=True) + + with output_path.open("w", encoding="utf-8") as f: + json.dump(output_data, f, indent=2) + + logging.info("Simulation completed successfully. Results written to %s", output_path) + except Exception as e: + logging.error("Simulation failed: %s", e) + sys.exit(1) + + +if __name__ == "__main__": + main()