|
4 | 4 | from pathlib import Path |
5 | 5 |
|
6 | 6 | from lxml import etree |
| 7 | +from lxml.objectify import ObjectifiedElement |
7 | 8 | from phoebusgen.widget.widgets import ActionButton, EmbeddedDisplay |
8 | 9 |
|
9 | 10 | from techui_builder.utils import read_bob |
@@ -74,4 +75,43 @@ def validate_bob( |
74 | 75 | f"{pwidget.get_element_value('file')} != {file_widget.file}" |
75 | 76 | ) |
76 | 77 |
|
| 78 | + self._validate_macros(pwidget, file_widget) |
| 79 | + |
77 | 80 | 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("") |
0 commit comments