From ce4587f884afef283f0b99840b9157a6a8828dd6 Mon Sep 17 00:00:00 2001 From: Zachary Lentz Date: Thu, 14 Nov 2024 11:12:17 -0800 Subject: [PATCH] ENH/TST: add back an offline option and switch the existing test to using it --- beams/bin/gen_test_ioc.py | 15 +++++++++++++-- beams/bin/gen_test_ioc_main.py | 26 +++++++++++++++++++++++--- beams/bin/test_ioc.py.j2 | 2 +- beams/tests/test_bin.py | 4 ++-- 4 files changed, 39 insertions(+), 8 deletions(-) diff --git a/beams/bin/gen_test_ioc.py b/beams/bin/gen_test_ioc.py index 41a4653..1d85416 100644 --- a/beams/bin/gen_test_ioc.py +++ b/beams/bin/gen_test_ioc.py @@ -1,10 +1,15 @@ """ -Generate a test caproto IOC containing all the PVs from a tree set to 0 with no special logic. +Generate a test caproto IOC containing all the PVs from a tree as they are in the live control system. Intended usage is something like: beams gen_test_ioc my_tree.json > outfile.py -Then, edit outfile.py to set useful starting values and add logic if needed. +You can use this file as-is or edit it for your purposes. + +If running in online mode (the default, unless you pass --offline), +this will query the controls system for live starting values and data types. + +In offline mode (--offline) everything will start as an int with value 0. """ from __future__ import annotations @@ -30,6 +35,12 @@ def build_arg_parser(argparser=None): help="Behavior Tree configuration filepath" ) + argparser.add_argument( + "--offline", + action="store_true", + help="Use default values instead of live values. Useful when running offline.", + ) + def main(*args, **kwargs): from beams.bin.gen_test_ioc_main import main diff --git a/beams/bin/gen_test_ioc_main.py b/beams/bin/gen_test_ioc_main.py index c297947..0c9460e 100644 --- a/beams/bin/gen_test_ioc_main.py +++ b/beams/bin/gen_test_ioc_main.py @@ -22,7 +22,7 @@ class PVInfoForJ2: precision: int @classmethod - def from_result(cls: type[PVInfoForJ2], pvname: str, response: ReadNotifyResponse): + def from_result(cls: type[PVInfoForJ2], pvname: str, response: ReadNotifyResponse) -> PVInfoForJ2: try: enum_strings = response.metadata.enum_strings except AttributeError: @@ -40,6 +40,17 @@ def from_result(cls: type[PVInfoForJ2], pvname: str, response: ReadNotifyRespons precision=precision, ) + @classmethod + def as_default(cls: type[PVInfoForJ2], pvname: str) -> PVInfoForJ2: + return cls( + python_name=pvname.lower().replace(":", "_").replace(".", "_"), + pvname=pvname, + value=0, + dtype="INT", + enum_strings=[], + precision=0, + ) + def collect_pv_info(pvnames: Iterable[str]) -> list[PVInfoForJ2]: ctx = Context() @@ -68,8 +79,13 @@ def stash_results(pvname: str, response: ReadNotifyResponse): return results +def default_pv_info(pvnames: Iterable[str]) -> list[PVInfoForJ2]: + return [PVInfoForJ2.as_default(pvname=pvname) for pvname in pvnames] + + def main( filepath: str, + offline: bool, ): with open(filepath, "r") as fd: deser = json.load(fd) @@ -78,7 +94,11 @@ def main( if not all_pvnames: raise RuntimeError(f"Found zero PVs in {filepath}") - all_pvinfo = sorted(collect_pv_info(all_pvnames)) + if offline: + pv_info = default_pv_info(all_pvnames) + else: + pv_info = collect_pv_info(all_pvnames) + sorted_pv_info = sorted(pv_info) jinja_env = jinja2.Environment( loader=jinja2.FileSystemLoader(Path(__file__).parent), @@ -86,7 +106,7 @@ def main( lstrip_blocks=True, ) template = jinja_env.get_template("test_ioc.py.j2") - rendered = template.render(all_pvinfo=all_pvinfo) + rendered = template.render(all_pv_info=sorted_pv_info) print(rendered) diff --git a/beams/bin/test_ioc.py.j2 b/beams/bin/test_ioc.py.j2 index 3511f68..dff7ffb 100644 --- a/beams/bin/test_ioc.py.j2 +++ b/beams/bin/test_ioc.py.j2 @@ -9,7 +9,7 @@ class BTSimIOC(PVGroup): """ An IOC to replicate the PVs used by your behavior tree. """ -{% for pv_info in all_pvinfo %} +{% for pv_info in all_pv_info %} {{ pv_info.python_name }} = pvproperty( name="{{ pv_info.pvname }}", doc="Fake {{ pv_info.pvname }}", diff --git a/beams/tests/test_bin.py b/beams/tests/test_bin.py index 0237a58..f3d855a 100644 --- a/beams/tests/test_bin.py +++ b/beams/tests/test_bin.py @@ -62,9 +62,9 @@ def test_run(added_args: tuple[str]): main() -def test_gen_test_ioc(capsys: pytest.CaptureFixture, monkeypatch: pytest.MonkeyPatch): +def test_gen_test_ioc_offline(capsys: pytest.CaptureFixture, monkeypatch: pytest.MonkeyPatch): test_cfg = Path(__file__).parent / "artifacts" / "eggs.json" - args = ["beams", "gen_test_ioc", str(test_cfg)] + args = ["beams", "gen_test_ioc", "--offline", str(test_cfg)] with cli_args(args), restore_logging(): main() result = capsys.readouterr()