Skip to content

Commit

Permalink
make sure repeat values are evaluated in sub-entities
Browse files Browse the repository at this point in the history
  • Loading branch information
gilesknap committed Mar 1, 2025
1 parent 5f2ced4 commit 69a8b78
Show file tree
Hide file tree
Showing 15 changed files with 112 additions and 107 deletions.
3 changes: 3 additions & 0 deletions src/ibek/entity_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ def _resolve_repeat(

# ensure correct class - it will be a generic Entity in subentities
repeat_entity = RepeatEntity(**repeat_entity.model_dump())
# also ensure repeat fields are rendered (in subentities they are not)
repeat_entity.values = UTILS.render(parent_entity, repeat_entity.values, "list")
repeat_entity.variable = UTILS.render(parent_entity, repeat_entity.variable)

for value in repeat_entity.values:
new_entity_cls = self._entity_types[repeat_entity.entity["type"]]
Expand Down
10 changes: 5 additions & 5 deletions src/ibek/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,16 @@ def render(self, context: Any, template_text: Any, typ: str = "str") -> Any:
"""
if isinstance(template_text, list):
result = (self.render(context, item) for item in template_text)
result = list(result)
result = list(result) # type: ignore
elif isinstance(template_text, dict):
result = {
result = { # type: ignore

Check warning on line 97 in src/ibek/utils.py

View check run for this annotation

Codecov / codecov/patch

src/ibek/utils.py#L97

Added line #L97 was not covered by tests
key: self.render(context, value) for key, value in template_text.items()
}
elif isinstance(template_text, str):
# if the template is not a string, jinja render it
try:
jinja_template = Template(template_text, undefined=StrictUndefined)
result = jinja_template.render(
result = jinja_template.render( # type: ignore
context,
# global context for all jinja renders
_global=self,
Expand All @@ -115,11 +115,11 @@ def render(self, context: Any, template_text: Any, typ: str = "str") -> Any:
if typ in ["list", "int", "float", "dict", "bool"]:
# coerce the rendered parameter to its intended type
cast_type = getattr(builtins, typ)
result = cast_type(ast.literal_eval(result))
result = cast_type(ast.literal_eval(result)) # type: ignore

except Exception as e:
raise ValueError(
f"Error rendering template type {typ}:\n{template_text}\nError"
f"Error rendering template type {typ}:\n{template_text}\nError: {e}"
) from e
else:
# because this function is used to template arguments, it may
Expand Down
7 changes: 5 additions & 2 deletions tests/generate_samples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,18 @@ echo making the support yaml schema
ibek support generate-schema --output schemas/ibek.support.schema.json



############################################################################
# ibek.repeat example IOC
# repeat tests IOCs
############################################################################

echo making repeat ioc
EPICS_ROOT=`pwd`/epics ibek runtime generate iocs/repeat.ibek.ioc.yaml support/asyn.ibek.support.yaml support/epics.ibek.support.yaml support/motorSim.ibek.support.yaml
mv `pwd`/epics/{runtime,opi}/* `pwd`/outputs/repeat

echo making subentity repeat ioc
EPICS_ROOT=`pwd`/epics ibek runtime generate iocs/subentity_test.ibek.ioc.yaml support/subentity_test.ibek.support.yaml support/epics.ibek.support.yaml
mv `pwd`/epics/{runtime,opi}/* `pwd`/outputs/subentity_test


############################################################################
# quadem example
Expand Down
8 changes: 8 additions & 0 deletions tests/samples/iocs/subentity_test.ibek.ioc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ioc_name: "{{ _global.get_env('IOC_NAME') }}"

description: Example for testing ibek.repeat

entities:
- type: subentity.SubEntityTest
count: 3
name: "subentity_test"
4 changes: 2 additions & 2 deletions tests/samples/outputs/dlsPLC/index.bob
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<display version="2.0.0">
<name></name>
<name>BL01T-EA-TST-02</name>
<x>0</x>
<y use_class="true">0</y>
<width>10</width>
Expand All @@ -9,7 +9,7 @@
<widget type="label" version="2.0.0">
<name>Title</name>
<class>TITLE</class>
<text>Title</text>
<text>BL01T-EA-TST-02</text>
<x use_class="true">0</x>
<y use_class="true">0</y>
<width>10</width>
Expand Down
4 changes: 2 additions & 2 deletions tests/samples/outputs/quadem/index.bob
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<display version="2.0.0">
<name></name>
<name>BL01T-EA-TST-02</name>
<x>0</x>
<y use_class="true">0</y>
<width>388</width>
Expand All @@ -9,7 +9,7 @@
<widget type="label" version="2.0.0">
<name>Title</name>
<class>TITLE</class>
<text>Title</text>
<text>BL01T-EA-TST-02</text>
<x use_class="true">0</x>
<y use_class="true">0</y>
<width>388</width>
Expand Down
4 changes: 2 additions & 2 deletions tests/samples/outputs/quadem_repeat/index.bob
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<display version="2.0.0">
<name></name>
<name>BL01T-EA-TST-02</name>
<x>0</x>
<y use_class="true">0</y>
<width>388</width>
Expand All @@ -9,7 +9,7 @@
<widget type="label" version="2.0.0">
<name>Title</name>
<class>TITLE</class>
<text>Title</text>
<text>BL01T-EA-TST-02</text>
<x use_class="true">0</x>
<y use_class="true">0</y>
<width>388</width>
Expand Down
4 changes: 2 additions & 2 deletions tests/samples/outputs/repeat/index.bob
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<display version="2.0.0">
<name></name>
<name>BL01T-EA-TST-02</name>
<x>0</x>
<y use_class="true">0</y>
<width>388</width>
Expand All @@ -9,7 +9,7 @@
<widget type="label" version="2.0.0">
<name>Title</name>
<class>TITLE</class>
<text>Title</text>
<text>BL01T-EA-TST-02</text>
<x use_class="true">0</x>
<y use_class="true">0</y>
<width>388</width>
Expand Down
100 changes: 8 additions & 92 deletions tests/samples/outputs/repeat/st.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -12,102 +12,18 @@ ioc_registerRecordDeviceDriver pdbbase
hello world from a
hello world from b
hello world from c
hello world from {
hello world from {
hello world from
hello world from r
hello world from a
hello world from n
hello world from g
hello world from e
hello world from (
hello world from 1
hello world from ,
hello world from
hello world from 4
hello world from )
hello world from
hello world from |
hello world from
hello world from l
hello world from i
hello world from s
hello world from t
hello world from
hello world from }
hello world from }
NESTED {a
NESTED {b
NESTED {c
NESTED {a
NESTED {b
NESTED {c
NESTED a
NESTED b
NESTED c
NESTED ra
NESTED rb
NESTED rc
NESTED aa
NESTED ab
NESTED ac
NESTED na
NESTED nb
NESTED nc
NESTED ga
NESTED gb
NESTED gc
NESTED ea
NESTED eb
NESTED ec
NESTED (a
NESTED (b
NESTED (c
hello world from 2
hello world from 3
NESTED 1a
NESTED 1b
NESTED 1c
NESTED ,a
NESTED ,b
NESTED ,c
NESTED a
NESTED b
NESTED c
NESTED 4a
NESTED 4b
NESTED 4c
NESTED )a
NESTED )b
NESTED )c
NESTED a
NESTED b
NESTED c
NESTED |a
NESTED |b
NESTED |c
NESTED a
NESTED b
NESTED c
NESTED la
NESTED lb
NESTED lc
NESTED ia
NESTED ib
NESTED ic
NESTED sa
NESTED sb
NESTED sc
NESTED ta
NESTED tb
NESTED tc
NESTED a
NESTED b
NESTED c
NESTED }a
NESTED }b
NESTED }c
NESTED }a
NESTED }b
NESTED }c
NESTED 2a
NESTED 2b
NESTED 2c
NESTED 3a
NESTED 3b
NESTED 3c
# Setting up Asyn Port controllerPort1 on 192.168.0.55:2003:
# AsynIPConfigure({{name}}, {{port}}, {{stop}}, {{parity}}, {{bits}})
AsynIPConfigure(controllerPort1, 192.168.0.55:2003, 1, none, 8)
Expand Down
28 changes: 28 additions & 0 deletions tests/samples/outputs/subentity_test/index.bob
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<display version="2.0.0">
<name>BL01T-EA-TST-02</name>
<x>0</x>
<y use_class="true">0</y>
<width>10</width>
<height>35</height>
<grid_step_x>4</grid_step_x>
<grid_step_y>4</grid_step_y>
<widget type="label" version="2.0.0">
<name>Title</name>
<class>TITLE</class>
<text>BL01T-EA-TST-02</text>
<x use_class="true">0</x>
<y use_class="true">0</y>
<width>10</width>
<height>25</height>
<font use_class="true">
<font name="Header 1" family="Liberation Sans" style="BOLD" size="22.0">
</font>
</font>
<foreground_color use_class="true">
<color name="Text" red="0" green="0" blue="0">
</color>
</foreground_color>
<transparent use_class="true">true</transparent>
<horizontal_alignment>1</horizontal_alignment>
</widget>
</display>
3 changes: 3 additions & 0 deletions tests/samples/outputs/subentity_test/ioc.subst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#############################################################################
# DB substitution file generated by http://github.com/epics-containers/ibek #
#############################################################################
12 changes: 12 additions & 0 deletions tests/samples/outputs/subentity_test/st.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# EPICS IOC Startup Script generated by https://github.com/epics-containers/ibek

cd "/epics/ioc"
dbLoadDatabase dbd/ioc.dbd
ioc_registerRecordDeviceDriver pdbbase

Command number 1 for subentity_test
Command number 2 for subentity_test
Command number 3 for subentity_test

dbLoadRecords /epics/runtime/ioc.db
iocInit
22 changes: 22 additions & 0 deletions tests/samples/support/subentity_test.ibek.support.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module: subentity

entity_models:
- name: SubEntityTest
description: Example for testing ibek.repeat
parameters:
count:
description: Number of sub entities to create
type: int
default: 5

name:
description: Name to use in the sub entities
type: str

sub_entities:
- type: ibek.repeat
values: "{{ range(count) | list }}"
variable: number
entity:
type: epics.StartupCommand
command: "Command number {{ number | int + 1}} for {{ name }}"
9 changes: 9 additions & 0 deletions tests/test_repeat.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,12 @@ def test_subentity_repeats(tmp_epics_root: Path, tmp_path, samples):

for f in files:
assert (samples / "outputs" / "quadem" / f.name).read_text() == f.read_text()


def test_subentity_repeats_with_substitutions(tmp_epics_root: Path, tmp_path, samples):
generic_generate(
tmp_epics_root,
samples,
"subentity_test",
["subentity_test", "epics"],
)
1 change: 1 addition & 0 deletions tests/test_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def test_symlink_ibek(tmp_path: Path, samples: Path):
"motorSim.ibek.support.yaml",
"quadem.ibek.support.yaml",
"quadem_repeat.ibek.support.yaml",
"subentity_test.ibek.support.yaml",
"technosoft.ibek.support.yaml",
"utils.ibek.support.yaml",
]
Expand Down

0 comments on commit 69a8b78

Please # to comment.