Skip to content

Commit

Permalink
Merge pull request #73 from epics-containers/name-validation
Browse files Browse the repository at this point in the history
Fix name conversion and validation
  • Loading branch information
GDYendell authored Jan 19, 2024
2 parents d500bcd + 85dec90 commit 67062a8
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 40 deletions.
14 changes: 7 additions & 7 deletions schemas/pvi.device.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
"properties": {
"name": {
"description": "PascalCase name to uniquely identify",
"pattern": "([A-Z][a-z0-9]*)*$",
"pattern": "^([A-Z][a-z0-9]*)*$",
"title": "Name",
"type": "string"
},
Expand Down Expand Up @@ -207,7 +207,7 @@
"properties": {
"name": {
"description": "PascalCase name to uniquely identify",
"pattern": "([A-Z][a-z0-9]*)*$",
"pattern": "^([A-Z][a-z0-9]*)*$",
"title": "Name",
"type": "string"
},
Expand Down Expand Up @@ -385,7 +385,7 @@
"properties": {
"name": {
"description": "PascalCase name to uniquely identify",
"pattern": "([A-Z][a-z0-9]*)*$",
"pattern": "^([A-Z][a-z0-9]*)*$",
"title": "Name",
"type": "string"
},
Expand Down Expand Up @@ -472,7 +472,7 @@
"properties": {
"name": {
"description": "PascalCase name to uniquely identify",
"pattern": "([A-Z][a-z0-9]*)*$",
"pattern": "^([A-Z][a-z0-9]*)*$",
"title": "Name",
"type": "string"
},
Expand Down Expand Up @@ -615,7 +615,7 @@
"properties": {
"name": {
"description": "PascalCase name to uniquely identify",
"pattern": "([A-Z][a-z0-9]*)*$",
"pattern": "^([A-Z][a-z0-9]*)*$",
"title": "Name",
"type": "string"
},
Expand Down Expand Up @@ -649,7 +649,7 @@
"properties": {
"name": {
"description": "PascalCase name to uniquely identify",
"pattern": "([A-Z][a-z0-9]*)*$",
"pattern": "^([A-Z][a-z0-9]*)*$",
"title": "Name",
"type": "string"
},
Expand Down Expand Up @@ -732,7 +732,7 @@
"properties": {
"name": {
"description": "PascalCase name to uniquely identify",
"pattern": "([A-Z][a-z0-9]*)*$",
"pattern": "^([A-Z][a-z0-9]*)*$",
"title": "Name",
"type": "string"
},
Expand Down
4 changes: 2 additions & 2 deletions src/pvi/_convert/_template_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from pathlib import Path
from typing import List, Tuple

from pvi.device import ComponentUnion, Grid, Group, Tree
from pvi.device import ComponentUnion, Grid, Group, Tree, enforce_pascal_case

from ._asyn_convert import (
Action,
Expand All @@ -24,7 +24,7 @@ def __init__(self, templates: List[Path]):
def convert(self) -> Tree:
return [
Group(
name=template.stem,
name=enforce_pascal_case(template.stem),
layout=Grid(labelled=True),
children=template_components,
)
Expand Down
8 changes: 3 additions & 5 deletions src/pvi/_format/screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,15 +457,13 @@ def generate_component_formatters(
):
# Convert W of Signal(R)W into SignalX for each button
row_components = [
SignalX(name=label, pv=c.pv, value=value)
for label, value in c.widget.actions.items()
SignalX(name=action, pv=c.pv, value=value)
for action, value in c.widget.actions.items()
]
if isinstance(c, SignalRW):
row_components += [
# TODO: Improve optional read_pv with property?
SignalR(
name=c.get_label(), pv=c.read_pv or c.pv, widget=c.read_widget
)
SignalR(name=c.name, pv=c.read_pv or c.pv, widget=c.read_widget)
]
else:
row_components = [c] # Create one widget for row
Expand Down
16 changes: 15 additions & 1 deletion src/pvi/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from pvi.utils import find_pvi_yaml

PASCAL_CASE_REGEX = re.compile(r"(?<![A-Z])[A-Z]|[A-Z][a-z/d]|(?<=[a-z])\d")
NON_PASCAL_CHARS_RE = re.compile(r"[^A-Za-z0-9]")


def to_title_case(pascal_s: str) -> str:
Expand All @@ -46,6 +47,19 @@ def to_snake_case(pascal_s: str) -> str:
return PASCAL_CASE_REGEX.sub(lambda m: "_" + m.group().lower(), pascal_s)[1:]


def enforce_pascal_case(s: str) -> str:
"""Enforce a pascal case string, removing any invalid characters.
Args:
s: String to convert
Returns: PascalCase string
"""
s = NON_PASCAL_CHARS_RE.sub(lambda _: "", s)
return s[0].upper() + s[1:]


class TextFormat(Enum):
"""Format to use for display of Text{Read,Write} widgets on a UI"""

Expand Down Expand Up @@ -249,7 +263,7 @@ class SubScreen(Layout):
class Named(BaseSettings):
name: str = Field(
description="PascalCase name to uniquely identify",
pattern=r"([A-Z][a-z0-9]*)*$",
pattern=r"^([A-Z][a-z0-9]*)*$",
)


Expand Down
2 changes: 1 addition & 1 deletion tests/convert/output/simDetector.pvi.device.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ parent: ADDriver
children:

- type: Group
name: simDetector
name: SimDetector
layout:
type: Grid
labelled: true
Expand Down
6 changes: 3 additions & 3 deletions tests/format/output/sub_screen.bob
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
<name>OpenDisplay</name>
<actions>
<action type="open_display">
<file>sub_screen_Group_1.bob</file>
<file>sub_screen_Group1.bob</file>
<target>tab</target>
<description>Open Display</description>
</action>
Expand All @@ -71,7 +71,7 @@
<transparent>true</transparent>
<widget type="label" version="2.0.0">
<name>Label</name>
<text>Group 3 E</text>
<text>Group 3 E</text>
<x>0</x>
<y>0</y>
<width>120</width>
Expand Down Expand Up @@ -102,7 +102,7 @@
<name>OpenDisplay</name>
<actions>
<action type="open_display">
<file>sub_screen_Group_4.bob</file>
<file>sub_screen_Group4.bob</file>
<target>tab</target>
<description>Open Display</description>
</action>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<widget type="label" version="2.0.0">
<name>Title</name>
<class>TITLE</class>
<text>Group 1</text>
<text>Group1</text>
<x use_class="true">0</x>
<y use_class="true">0</y>
<width>290</width>
Expand All @@ -27,7 +27,7 @@
</widget>
<widget type="label" version="2.0.0">
<name>Label</name>
<text>Group 1 B</text>
<text>Group 1 B</text>
<x>22</x>
<y>30</y>
<width>120</width>
Expand Down Expand Up @@ -55,7 +55,7 @@
<transparent>true</transparent>
<widget type="label" version="2.0.0">
<name>Label</name>
<text>Group 2 C</text>
<text>Group 2 C</text>
<x>0</x>
<y>0</y>
<width>120</width>
Expand All @@ -76,7 +76,7 @@
</widget>
<widget type="label" version="2.0.0">
<name>Label</name>
<text>Group 2 D</text>
<text>Group 2 D</text>
<x>0</x>
<y>24</y>
<width>120</width>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<widget type="label" version="2.0.0">
<name>Title</name>
<class>TITLE</class>
<text>Group 4</text>
<text>Group4</text>
<x use_class="true">0</x>
<y use_class="true">0</y>
<width>290</width>
Expand All @@ -27,7 +27,7 @@
</widget>
<widget type="label" version="2.0.0">
<name>Label</name>
<text>Group 4 F</text>
<text>Group 4 F</text>
<x>22</x>
<y>30</y>
<width>120</width>
Expand Down Expand Up @@ -55,7 +55,7 @@
<transparent>true</transparent>
<widget type="label" version="2.0.0">
<name>Label</name>
<text>Group 5 G</text>
<text>Group 5 G</text>
<x>0</x>
<y>0</y>
<width>120</width>
Expand All @@ -76,7 +76,7 @@
</widget>
<widget type="label" version="2.0.0">
<name>Label</name>
<text>Group 5 H</text>
<text>Group 5 H</text>
<x>0</x>
<y>24</y>
<width>120</width>
Expand Down
26 changes: 13 additions & 13 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def test_pva_table_panda(tmp_path, helper):
formatter = Formatter.deserialize(formatter_yaml)

table = SignalR(
name="PandA ",
name="PandA",
pv="PANDAQSRV:SEQ1:TABLE",
widget=TableRead(
widgets=[TextRead()] * 4 + [LED()] * 6 + [TextRead()] + [LED()] * 6
Expand Down Expand Up @@ -178,47 +178,47 @@ def test_group_sub_screen(tmp_path, helper):
signals = [
SignalR(name="A", pv="A", widget=TextRead()),
Group(
name="Group 1",
name="Group1",
layout=SubScreen(),
children=[
SignalR(name="Group 1 B", pv="GROUP1:B", widget=TextRead()),
SignalR(name="Group1B", pv="GROUP1:B", widget=TextRead()),
Group(
name="Group 2",
name="Group2",
layout=Grid(),
children=[
SignalR(
name="Group 2 C", pv="GROUP1:GROUP2:C", widget=TextRead()
name="Group2C", pv="GROUP1:GROUP2:C", widget=TextRead()
),
SignalR(
name="Group 2 D", pv="GROUP1:GROUP2:D", widget=TextRead()
name="Group2D", pv="GROUP1:GROUP2:D", widget=TextRead()
),
],
),
],
),
Group(
name="Group 3",
name="Group3",
layout=Grid(),
children=[
SignalR(name="Group 3 E", pv="GROUP3:E", widget=TextRead()),
SignalR(name="Group3E", pv="GROUP3:E", widget=TextRead()),
Group(
name="Group 4",
name="Group4",
layout=SubScreen(),
children=[
SignalR(
name="Group 4 F", pv="GROUP3:GROUP4:F", widget=TextRead()
name="Group4F", pv="GROUP3:GROUP4:F", widget=TextRead()
),
Group(
name="Group 5",
name="Group5",
layout=Grid(),
children=[
SignalR(
name="Group 5 G",
name="Group5G",
pv="GROUP3:GROUP4:GROUP5:G",
widget=TextRead(),
),
SignalR(
name="Group 5 H",
name="Group5H",
pv="GROUP3:GROUP4:GROUP5:H",
widget=TextRead(),
),
Expand Down

0 comments on commit 67062a8

Please sign in to comment.