-
Notifications
You must be signed in to change notification settings - Fork 0
Add NetGraph CLI with run subcommand #67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| """NetGraph package.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from . import cli | ||
|
|
||
| __all__ = ["cli"] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from .cli import main | ||
|
|
||
| if __name__ == "__main__": | ||
| main() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import json | ||
| from pathlib import Path | ||
| from typing import Any, Dict, List, Optional | ||
|
|
||
| from ngraph.scenario import Scenario | ||
|
|
||
|
|
||
| def _run_scenario(path: Path, output: Optional[Path]) -> None: | ||
| """Run a scenario file and store results as JSON.""" | ||
| yaml_text = path.read_text() | ||
| scenario = Scenario.from_yaml(yaml_text) | ||
| scenario.run() | ||
|
|
||
| results_dict: Dict[str, Dict[str, Any]] = scenario.results.to_dict() | ||
| json_str = json.dumps(results_dict, indent=2, default=str) | ||
| if output: | ||
| output.write_text(json_str) | ||
| else: | ||
| print(json_str) | ||
|
|
||
|
|
||
| def main(argv: Optional[List[str]] = None) -> None: | ||
| """Entry point for the ``ngraph`` command.""" | ||
| parser = argparse.ArgumentParser(prog="ngraph") | ||
| subparsers = parser.add_subparsers(dest="command", required=True) | ||
|
|
||
| run_parser = subparsers.add_parser("run", help="Run a scenario") | ||
| run_parser.add_argument("scenario", type=Path, help="Path to scenario YAML") | ||
| run_parser.add_argument( | ||
| "--results", | ||
| "-r", | ||
| type=Path, | ||
| default=None, | ||
| help="Write JSON results to this file instead of stdout", | ||
| ) | ||
|
|
||
| args = parser.parse_args(argv) | ||
|
|
||
| if args.command == "run": | ||
| _run_scenario(args.scenario, args.results) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -54,3 +54,7 @@ def get_all(self, key: str) -> Dict[str, Any]: | |||||
| if key in data: | ||||||
| result[step_name] = data[key] | ||||||
| return result | ||||||
|
|
||||||
| def to_dict(self) -> Dict[str, Dict[str, Any]]: | ||||||
| """Return a dictionary representation of all stored results.""" | ||||||
| return {step: data.copy() for step, data in self._store.items()} | ||||||
|
||||||
| return {step: data.copy() for step, data in self._store.items()} | |
| return {step: copy.deepcopy(data) for step, data in self._store.items()} |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,22 @@ | ||||||
| import json | ||||||
| from pathlib import Path | ||||||
|
|
||||||
| from ngraph import cli | ||||||
|
|
||||||
|
|
||||||
| def test_cli_run_file(tmp_path: Path) -> None: | ||||||
| scenario = Path("tests/scenarios/scenario_1.yaml") | ||||||
|
||||||
| scenario = Path("tests/scenarios/scenario_1.yaml") | |
| scenario = Path(__file__).parent / "scenarios" / "scenario_1.yaml" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Writing JSON without a trailing newline can break POSIX conventions; consider appending
"\n"tojson_strwhen callingwrite_text.