Skip to content

Commit a4550a3

Browse files
authored
Add opis URL to Beamline model (#180)
* Add opis URL to Beamline model * Add url to t01-services techui.yaml
1 parent d2e5561 commit a4550a3

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

example/t01-services/synoptic/techui.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ beamline:
33
short_dom: t01
44
long_dom: bl01t
55
desc: Test Beamline
6+
url: t01-opis.diamond.ac.uk
67

78
components:
89
fshtr:

src/techui_builder/models.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,15 @@
4646
)
4747
_LONG_DOM_RE = re.compile(r"^[a-zA-Z]{2}\d{2}[a-zA-Z]$")
4848
_SHORT_DOM_RE = re.compile(r"^[a-zA-Z]{1}\d{2}(-[0-9]{1})?$")
49+
_OPIS_URL_RE = re.compile(r"^(https:\/\/)?([a-z0-9]{3}-(?:[0-9]-)?opis(?:.[a-z0-9]*)*)")
4950

5051

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

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

7678
raise ValueError("Invalid long dom.")
7779

80+
@field_validator("url")
81+
@classmethod
82+
def check_url(cls, url: str) -> str:
83+
url = url.strip().lower()
84+
match = _OPIS_URL_RE.match(url)
85+
if match is not None and match.group(2):
86+
# url in correct format
87+
# e.g. t01-opis.diamond.ac.uk
88+
if not match.group(1):
89+
# make sure url leads with 'https://'
90+
# otherwise phoebus treats it as a local file path
91+
url = f"https://{match.group(2)}"
92+
return url
93+
94+
raise ValueError("Invalid opis URL.")
95+
7896

7997
class Component(BaseModel):
8098
prefix: str

tests/test_models.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@
1010

1111
@pytest.fixture
1212
def beamline() -> Beamline:
13-
return Beamline(short_dom="t01", long_dom="bl01t", desc="Test Beamline")
13+
return Beamline(
14+
short_dom="t01",
15+
long_dom="bl01t",
16+
desc="Test Beamline",
17+
url="t01-opis.diamond.ac.uk",
18+
)
1419

1520

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

3440

3541
def test_component_object(component: Component):

0 commit comments

Comments
 (0)