Add simulation_tool/src/simulation_tool/cli.py
This commit is contained in:
parent
873e56f317
commit
591e55928a
1 changed files with 65 additions and 0 deletions
65
simulation_tool/src/simulation_tool/cli.py
Normal file
65
simulation_tool/src/simulation_tool/cli.py
Normal file
|
|
@ -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()
|
||||||
Loading…
Reference in a new issue