Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "example/t01-services/synoptic/techui-support"]
path = example/t01-services/synoptic/techui-support
url = https://github.com/DiamondLightSource/techui-support.git
18 changes: 18 additions & 0 deletions example/t01-services/services/bl01t-ea-ioc-01/config/fastcs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# yaml-language-server: $schema=schema.json
controllers:
- name: BL01T-EA-TEST-01
type: fastcs.TemperatureController
ip_settings:
ip: "localhost"
port: 25565
num_ramp_controllers: 4

transport:
- graphql:
host: localhost
port: 8083
log_level: info
- epicsca: {}
gui:
title: Temperature Controller Demo
output_dir: .
1 change: 0 additions & 1 deletion example/t01-services/synoptic/techui-support

This file was deleted.

1 change: 1 addition & 0 deletions example/t01-services/synoptic/techui-support
Submodule techui-support added at b908b8
97 changes: 74 additions & 23 deletions src/techui_builder/builder.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import json
import logging
import os
import re
from collections import defaultdict
from dataclasses import _MISSING_TYPE, dataclass, field
from pathlib import Path
from typing import Any

import yaml
from epicsdbbuilder.recordbase import Record
from jinja2 import Template
from lxml import etree, objectify
from lxml.objectify import ObjectifiedElement
from softioc.builder import records

from techui_builder.generate import Generator
from techui_builder.models import Entity, TechUi
from techui_builder.models import Entity, SupportEntity, TechUi, TechUiSupport
from techui_builder.validator import Validator

logger_ = logging.getLogger(__name__)
Expand Down Expand Up @@ -49,9 +51,10 @@ class Builder:
default_factory=lambda: defaultdict(list), init=False
)
status_pvs: dict[str, Record] = field(default_factory=dict, init=False)

# These are global params for the class (not accessible by user)
_services_dir: Path = field(init=False, repr=False)
_gui_map: dict = field(init=False, repr=False)
_write_directory: Path = field(default=Path("opis"), init=False, repr=False)
_write_directory: Path = field(init=False, repr=False)

def __post_init__(self):
# Populate beamline and components
Expand All @@ -60,13 +63,34 @@ def __post_init__(self):
)

def setup(self):
"""Run intial setup, e.g. extracting entries from service ioc.yaml."""
"""
Run intial setup, e.g. extracting entries
from service ioc.yaml or fastcs.yaml.
"""
# This needs to be before _read_map()
self.support_path = self._write_directory.joinpath("techui-support")

self._read_map()

self._extract_services()
synoptic_dir = self._write_directory

self.clean_files()

self.generator = Generator(synoptic_dir, self.conf.beamline.url)
self.generator = Generator(
self._write_directory,
self.conf.beamline.url,
self.support_path,
self.techui_support,
)

def _read_map(self):
"""Read the techui-support.yaml file from techui-support."""
support_yaml = self.support_path.joinpath("techui-support.yaml").absolute()
logger_.debug(f"techui-support.yaml location: {support_yaml}")

self.techui_support = TechUiSupport.model_validate(
yaml.safe_load(support_yaml.read_text(encoding="utf-8"))
)

def clean_files(self):
exclude = {"index.bob"}
Expand Down Expand Up @@ -159,35 +183,62 @@ def _extract_services(self):
service_name = service.name
# If service doesn't exist, file open will fail throwing exception
try:
service_yaml_dir = service.joinpath("config")
service_yaml = next(service_yaml_dir.glob("*.yaml"), None)
if service_yaml is None:
raise OSError()

self._extract_entities(
service_name=service_name,
ioc_yaml=service.joinpath("config/ioc.yaml"),
service_yaml=service_yaml,
)

except OSError:
logger_.error(
f"No ioc.yaml file for service: [bold]{service_name}[/bold]."
" Does it exist?"
"No ioc.yaml or fastcs.yaml found for service: "
f"[bold]{service_name}[/bold]. Does it exist?"
)

def _extract_entities(self, service_name: str, ioc_yaml: Path):
def _extract_entities(self, service_name: str, service_yaml: Path):
"""
Extracts the entries in ioc.yaml matching the defined prefix
"""

with open(ioc_yaml) as ioc:
with open(service_yaml) as ioc:
ioc_conf: dict[str, list[dict[str, str]]] = yaml.safe_load(ioc)
for entity in ioc_conf["entities"]:
if "P" in entity.keys():
# Create Entity and append to entity list
new_entity = Entity(
service_name=service_name,
type=entity["type"],
desc=entity.get("desc", None),
P=entity["P"],
M=None if (val := entity.get("M")) is None else val,
R=None if (val := entity.get("R")) is None else val,
)
self.entities[new_entity.P].append(new_entity)

for key in ioc_conf.keys():
_regex = re.compile(r"^(?:(entities)|(controllers))$")
match = _regex.match(key)
if match:
entity_key = match.group()

for entity in ioc_conf[entity_key]:
if entity["type"] in self.techui_support.support_modules:
support_mapping: SupportEntity = (
self.techui_support.support_modules[entity["type"]]
)
support_macros = support_mapping.macros

macros = {
k: v for k, v in entity.items() if k in support_macros
}

prefix_template = Template(support_mapping.prefix)
prefix: str = prefix_template.render(macros)

# Create Entity and append to entity list
new_entity = Entity(
service_name=service_name,
type=entity["type"],
desc=entity.get("desc", None),
prefix=prefix,
macros=macros,
)

pv_root = prefix.split(":", maxsplit=1)[0]
self.entities[pv_root].append(new_entity)
break

def _generate_screen(self, screen_name: str):
self.generator.build_screen(screen_name)
Expand Down
Loading