|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import argparse |
| 4 | +import json |
| 5 | +from pathlib import Path |
| 6 | +from typing import Any, Dict, List, Optional |
| 7 | + |
| 8 | +from ngraph.scenario import Scenario |
| 9 | + |
| 10 | + |
| 11 | +def _run_scenario(path: Path, output: Optional[Path]) -> None: |
| 12 | + """Run a scenario file and store results as JSON.""" |
| 13 | + yaml_text = path.read_text() |
| 14 | + scenario = Scenario.from_yaml(yaml_text) |
| 15 | + scenario.run() |
| 16 | + |
| 17 | + results_dict: Dict[str, Dict[str, Any]] = scenario.results.to_dict() |
| 18 | + json_str = json.dumps(results_dict, indent=2, default=str) |
| 19 | + if output: |
| 20 | + output.write_text(json_str) |
| 21 | + else: |
| 22 | + print(json_str) |
| 23 | + |
| 24 | + |
| 25 | +def main(argv: Optional[List[str]] = None) -> None: |
| 26 | + """Entry point for the ``ngraph`` command.""" |
| 27 | + parser = argparse.ArgumentParser(prog="ngraph") |
| 28 | + subparsers = parser.add_subparsers(dest="command", required=True) |
| 29 | + |
| 30 | + run_parser = subparsers.add_parser("run", help="Run a scenario") |
| 31 | + run_parser.add_argument("scenario", type=Path, help="Path to scenario YAML") |
| 32 | + run_parser.add_argument( |
| 33 | + "--results", |
| 34 | + "-r", |
| 35 | + type=Path, |
| 36 | + default=None, |
| 37 | + help="Write JSON results to this file instead of stdout", |
| 38 | + ) |
| 39 | + |
| 40 | + args = parser.parse_args(argv) |
| 41 | + |
| 42 | + if args.command == "run": |
| 43 | + _run_scenario(args.scenario, args.results) |
| 44 | + |
| 45 | + |
| 46 | +if __name__ == "__main__": |
| 47 | + main() |
0 commit comments