Skip to content

Commit 6b6a0ef

Browse files
authored
Refactor logic for component macros (#224)
2 parents d10e2d2 + 4b00be7 commit 6b6a0ef

14 files changed

Lines changed: 426 additions & 303 deletions

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "example/t01-services/synoptic/techui-support"]
2+
path = example/t01-services/synoptic/techui-support
3+
url = https://github.com/DiamondLightSource/techui-support.git

example/t01-services/synoptic/techui-support

Lines changed: 0 additions & 1 deletion
This file was deleted.
Submodule techui-support added at b908b8f

src/techui_builder/builder.py

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88

99
import yaml
1010
from epicsdbbuilder.recordbase import Record
11+
from jinja2 import Template
1112
from lxml import etree, objectify
1213
from lxml.objectify import ObjectifiedElement
1314
from softioc.builder import records
1415

1516
from techui_builder.generate import Generator
16-
from techui_builder.models import Entity, TechUi
17+
from techui_builder.models import Entity, SupportEntity, TechUi, TechUiSupport
1718
from techui_builder.validator import Validator
1819

1920
logger_ = logging.getLogger(__name__)
@@ -49,9 +50,10 @@ class Builder:
4950
default_factory=lambda: defaultdict(list), init=False
5051
)
5152
status_pvs: dict[str, Record] = field(default_factory=dict, init=False)
53+
54+
# These are global params for the class (not accessible by user)
5255
_services_dir: Path = field(init=False, repr=False)
53-
_gui_map: dict = field(init=False, repr=False)
54-
_write_directory: Path = field(default=Path("opis"), init=False, repr=False)
56+
_write_directory: Path = field(init=False, repr=False)
5557

5658
def __post_init__(self):
5759
# Populate beamline and components
@@ -61,12 +63,30 @@ def __post_init__(self):
6163

6264
def setup(self):
6365
"""Run intial setup, e.g. extracting entries from service ioc.yaml."""
66+
# This needs to be before _read_map()
67+
self.support_path = self._write_directory.joinpath("techui-support")
68+
69+
self._read_map()
70+
6471
self._extract_services()
65-
synoptic_dir = self._write_directory
6672

6773
self.clean_files()
6874

69-
self.generator = Generator(synoptic_dir, self.conf.beamline.url)
75+
self.generator = Generator(
76+
self._write_directory,
77+
self.conf.beamline.url,
78+
self.support_path,
79+
self.techui_support,
80+
)
81+
82+
def _read_map(self):
83+
"""Read the techui-support.yaml file from techui-support."""
84+
support_yaml = self.support_path.joinpath("techui-support.yaml").absolute()
85+
logger_.debug(f"techui-support.yaml location: {support_yaml}")
86+
87+
self.techui_support = TechUiSupport.model_validate(
88+
yaml.safe_load(support_yaml.read_text(encoding="utf-8"))
89+
)
7090

7191
def clean_files(self):
7292
exclude = {"index.bob"}
@@ -177,17 +197,28 @@ def _extract_entities(self, service_name: str, ioc_yaml: Path):
177197
with open(ioc_yaml) as ioc:
178198
ioc_conf: dict[str, list[dict[str, str]]] = yaml.safe_load(ioc)
179199
for entity in ioc_conf["entities"]:
180-
if "P" in entity.keys():
200+
if entity["type"] in self.techui_support.support_modules:
201+
support_mapping: SupportEntity = (
202+
self.techui_support.support_modules[entity["type"]]
203+
)
204+
support_macros = support_mapping.macros
205+
206+
macros = {k: v for k, v in entity.items() if k in support_macros}
207+
208+
prefix_template = Template(support_mapping.prefix)
209+
prefix: str = prefix_template.render(macros)
210+
181211
# Create Entity and append to entity list
182212
new_entity = Entity(
183213
service_name=service_name,
184214
type=entity["type"],
185215
desc=entity.get("desc", None),
186-
P=entity["P"],
187-
M=None if (val := entity.get("M")) is None else val,
188-
R=None if (val := entity.get("R")) is None else val,
216+
prefix=prefix,
217+
macros=macros,
189218
)
190-
self.entities[new_entity.P].append(new_entity)
219+
220+
pv_root = prefix.split(":", maxsplit=1)[0]
221+
self.entities[pv_root].append(new_entity)
191222

192223
def _generate_screen(self, screen_name: str):
193224
self.generator.build_screen(screen_name)

0 commit comments

Comments
 (0)