Skip to content

Commit c4db300

Browse files
committed
Add functionality to validating macros
1 parent 888e9dc commit c4db300

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

src/techui_builder/validator.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from pathlib import Path
55

66
from lxml import etree
7+
from lxml.objectify import ObjectifiedElement
78
from phoebusgen.widget.widgets import ActionButton, EmbeddedDisplay
89

910
from techui_builder.utils import read_bob
@@ -74,4 +75,43 @@ def validate_bob(
7475
f"{pwidget.get_element_value('file')} != {file_widget.file}"
7576
)
7677

78+
self._validate_macros(pwidget, file_widget)
79+
7780
LOGGER.info(f"{screen_name}.bob has been validated successfully")
81+
82+
def _validate_macros(
83+
self, pwidget: EmbeddedDisplay | ActionButton, file_widget: ObjectifiedElement
84+
):
85+
pmacros_element = pwidget.find_element("macros")
86+
# Annoyingly iterating over this also includes the element tag\
87+
# so it needs ignoring, hence the '!= "macros"'
88+
pmacros = {
89+
macro.tag: macro.text for macro in pmacros_element if macro.tag != "macros"
90+
}
91+
pmacros_keys = set(pmacros.keys())
92+
93+
fmacros = file_widget.macros.getchildren()
94+
fmacros_keys = {str(macro.tag) for macro in fmacros}
95+
96+
# Checks if there is any difference in expected macros
97+
diff_expected_macros = pmacros_keys - fmacros_keys
98+
if diff_expected_macros:
99+
LOGGER.error(
100+
f"Expected macros {diff_expected_macros} missing from \
101+
{file_widget.name}."
102+
)
103+
104+
# ---------- This is how we could overwrite macros in the future ----------
105+
106+
# for expected_macro in diff_expected_macros:
107+
# macro_element = Element(expected_macro)
108+
# # Get the macro value from generated pwidget macros
109+
# macro_element.text = pmacros[expected_macro]
110+
# print(pmacros[expected_macro])
111+
112+
# # Convert xml.etree.Element to ObjectifiedElement
113+
# new_macro = fromstring(tostring(macro_element))
114+
115+
# file_widget.macros.append(new_macro)
116+
117+
# write_bob("")

tests/test_files/widget.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
<macros>
1010
<P>BL23B-DI-MOD-02</P>
1111
<R>CAM</R>
12+
<IOC>test_url/bl23b-di-mod-02</IOC>
1213
</macros>
1314
</widget>

tests/test_validator.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,12 @@ def test_validator_validate_bob(validator, example_embedded_widget):
4949
validator.validate = {"motor-edited": Path("tests/test_files/motor-edited.bob")}
5050
test_pwidget = EmbeddedDisplay(
5151
"motor",
52-
"example/t01-services/synoptic/techui-support/bob/pmac/motor_embed.bob",
52+
"tests/test-files/motor_embed.bob",
5353
0,
5454
0,
5555
205,
5656
120,
5757
)
58+
test_pwidget.macro("macro1", "test_macro_1")
5859

5960
validator.validate_bob("motor-edited", "motor", [test_pwidget])

0 commit comments

Comments
 (0)