Skip to content
Merged
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
1 change: 1 addition & 0 deletions example/t01-services/synoptic/techui.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ beamline:
short_dom: t01
long_dom: bl01t
desc: Test Beamline
url: t01-opis.diamond.ac.uk

components:
fshtr:
Expand Down
18 changes: 18 additions & 0 deletions src/techui_builder/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@
)
_LONG_DOM_RE = re.compile(r"^[a-zA-Z]{2}\d{2}[a-zA-Z]$")
_SHORT_DOM_RE = re.compile(r"^[a-zA-Z]{1}\d{2}(-[0-9]{1})?$")
_OPIS_URL_RE = re.compile(r"^(https:\/\/)?([a-z0-9]{3}-(?:[0-9]-)?opis(?:.[a-z0-9]*)*)")


class Beamline(BaseModel):
short_dom: str = Field(description="Short BL domain e.g. b23, ixx-1")
long_dom: str = Field(description="Full BL domain e.g. bl23b")
desc: str = Field(description="Description")
model_config = ConfigDict(extra="forbid")
url: str = Field(description="URL of ixx-opis")

@field_validator("short_dom")
@classmethod
Expand All @@ -75,6 +77,22 @@ def normalize_long_dom(cls, v: str) -> str:

raise ValueError("Invalid long dom.")

@field_validator("url")
@classmethod
def check_url(cls, url: str) -> str:
url = url.strip().lower()
match = _OPIS_URL_RE.match(url)
if match is not None and match.group(2):
# url in correct format
# e.g. t01-opis.diamond.ac.uk
if not match.group(1):
# make sure url leads with 'https://'
# otherwise phoebus treats it as a local file path
url = f"https://{match.group(2)}"
return url

raise ValueError("Invalid opis URL.")


class Component(BaseModel):
prefix: str
Expand Down
8 changes: 7 additions & 1 deletion tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@

@pytest.fixture
def beamline() -> Beamline:
return Beamline(short_dom="t01", long_dom="bl01t", desc="Test Beamline")
return Beamline(
short_dom="t01",
long_dom="bl01t",
desc="Test Beamline",
url="t01-opis.diamond.ac.uk",
)


@pytest.fixture
Expand All @@ -30,6 +35,7 @@ def test_beamline_object(beamline: Beamline):
assert beamline.short_dom == "t01"
assert beamline.long_dom == "bl01t"
assert beamline.desc == "Test Beamline"
assert beamline.url == "https://t01-opis.diamond.ac.uk"


def test_component_object(component: Component):
Expand Down