Skip to content

Commit

Permalink
Create ComboBox widgets for Attributes with allowed_values
Browse files Browse the repository at this point in the history
  • Loading branch information
GDYendell committed Jul 24, 2024
1 parent 0bc835e commit 68c1930
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 20 deletions.
29 changes: 18 additions & 11 deletions src/fastcs/backends/epics/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from pvi.device import (
LED,
ButtonPanel,
ComboBox,
Component,
Device,
Grid,
Expand All @@ -27,7 +28,7 @@

from fastcs.attributes import Attribute, AttrR, AttrRW, AttrW
from fastcs.cs_methods import Command
from fastcs.datatypes import Bool, DataType, Float, Int, String
from fastcs.datatypes import Bool, Float, Int, String
from fastcs.exceptions import FastCSException
from fastcs.mapping import Mapping, SingleMapping, _get_single_mapping
from fastcs.util import snake_to_pascal
Expand Down Expand Up @@ -55,27 +56,33 @@ def _get_pv(self, attr_path: list[str], name: str):
return f"{attr_prefix}:{name.title().replace('_', '')}"

@staticmethod
def _get_read_widget(datatype: DataType) -> ReadWidget:
match datatype:
def _get_read_widget(attribute: AttrR) -> ReadWidget:
match attribute.datatype:
case Bool():
return LED()
case Int() | Float():
return TextRead()
case String():
return TextRead(format=TextFormat.string)
case _:
case datatype:

Check warning on line 67 in src/fastcs/backends/epics/gui.py

View check run for this annotation

Codecov / codecov/patch

src/fastcs/backends/epics/gui.py#L67

Added line #L67 was not covered by tests
raise FastCSException(f"Unsupported type {type(datatype)}: {datatype}")

@staticmethod
def _get_write_widget(datatype: DataType) -> WriteWidget:
match datatype:
def _get_write_widget(attribute: AttrW) -> WriteWidget:
match attribute.allowed_values:
case allowed_values if allowed_values is not None:
return ComboBox(choices=allowed_values)
case _:
pass

match attribute.datatype:
case Bool():
return ToggleButton()
case Int() | Float():
return TextWrite()
case String():
return TextWrite(format=TextFormat.string)
case _:
case datatype:

Check warning on line 85 in src/fastcs/backends/epics/gui.py

View check run for this annotation

Codecov / codecov/patch

src/fastcs/backends/epics/gui.py#L85

Added line #L85 was not covered by tests
raise FastCSException(f"Unsupported type {type(datatype)}: {datatype}")

def _get_attribute_component(
Expand All @@ -86,8 +93,8 @@ def _get_attribute_component(

match attribute:
case AttrRW():
read_widget = self._get_read_widget(attribute.datatype)
write_widget = self._get_write_widget(attribute.datatype)
read_widget = self._get_read_widget(attribute)
write_widget = self._get_write_widget(attribute)
return SignalRW(
name=name,
write_pv=pv,
Expand All @@ -96,10 +103,10 @@ def _get_attribute_component(
read_widget=read_widget,
)
case AttrR():
read_widget = self._get_read_widget(attribute.datatype)
read_widget = self._get_read_widget(attribute)
return SignalR(name=name, read_pv=pv, read_widget=read_widget)
case AttrW():
write_widget = self._get_write_widget(attribute.datatype)
write_widget = self._get_write_widget(attribute)
return SignalW(name=name, write_pv=pv, write_widget=write_widget)

def _get_command_component(self, attr_path: list[str], name: str):
Expand Down
13 changes: 8 additions & 5 deletions tests/backends/epics/test_gui.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from pvi.device import (
ButtonPanel,
ComboBox,
SignalR,
SignalRW,
SignalW,
Expand Down Expand Up @@ -32,18 +33,20 @@ def test_get_components(mapping):
read_pv="DEVICE:ReadInt",
read_widget=TextRead(),
),
SignalR(
name="ReadString",
read_pv="DEVICE:ReadString",
read_widget=TextRead(format="string"),
),
SignalRW(
name="ReadWriteFloat",
write_pv="DEVICE:ReadWriteFloat",
write_widget=TextWrite(),
read_pv="DEVICE:ReadWriteFloat_RBV",
read_widget=TextRead(),
),
SignalRW(
name="StringEnum",
read_pv="DEVICE:StringEnum_RBV",
read_widget=TextRead(format="string"),
write_pv="DEVICE:StringEnum",
write_widget=ComboBox(choices=["red", "green", "blue"]),
),
SignalW(
name="WriteBool",
write_pv="DEVICE:WriteBool",
Expand Down
6 changes: 3 additions & 3 deletions tests/backends/tango/test_dsr.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ def test_collect_attributes(mapping):
# Check that attributes are created and of expected type
assert list(attributes.keys()) == [
"ReadInt",
"ReadString",
"ReadWriteFloat",
"StringEnum",
"WriteBool",
]
assert attributes["ReadInt"].attr_write == AttrWriteType.READ
assert attributes["ReadInt"].attr_type == CmdArgType.DevLong64
assert attributes["ReadString"].attr_write == AttrWriteType.READ
assert attributes["ReadString"].attr_type == CmdArgType.DevString
assert attributes["StringEnum"].attr_write == AttrWriteType.READ_WRITE
assert attributes["StringEnum"].attr_type == CmdArgType.DevString
assert attributes["ReadWriteFloat"].attr_write == AttrWriteType.READ_WRITE
assert attributes["ReadWriteFloat"].attr_type == CmdArgType.DevDouble
assert attributes["WriteBool"].attr_write == AttrWriteType.WRITE
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class TestController(Controller):
read_int: AttrR = AttrR(Int())
read_write_float: AttrRW = AttrRW(Float())
write_bool: AttrW = AttrW(Bool())
read_string: AttrR = AttrR(String())
string_enum: AttrRW = AttrRW(String(), allowed_values=["red", "green", "blue"])

@command()
async def go(self):
Expand Down

0 comments on commit 68c1930

Please sign in to comment.