Skip to content

Commit

Permalink
ENH/TST: add back an offline option and switch the existing test to u…
Browse files Browse the repository at this point in the history
…sing it
  • Loading branch information
ZLLentz committed Nov 14, 2024
1 parent 79ccf69 commit ce4587f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
15 changes: 13 additions & 2 deletions beams/bin/gen_test_ioc.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand Down
26 changes: 23 additions & 3 deletions beams/bin/gen_test_ioc_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -78,15 +94,19 @@ 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),
trim_blocks=True,
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)


Expand Down
2 changes: 1 addition & 1 deletion beams/bin/test_ioc.py.j2
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}",
Expand Down
4 changes: 2 additions & 2 deletions beams/tests/test_bin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit ce4587f

Please sign in to comment.