-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Attributes with allowed values as mbb records
Add check for enum attributes and perform conversions in pythonSoftIOC wrapper functions. Co-authored-by: Gary Yendell <gary.yendell@diamond.ac.uk>
- Loading branch information
Showing
8 changed files
with
149 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from fastcs.attributes import Attribute | ||
from fastcs.datatypes import String, T | ||
|
||
_MBB_FIELD_PREFIXES = ( | ||
"ZR", | ||
"ON", | ||
"TW", | ||
"TH", | ||
"FR", | ||
"FV", | ||
"SX", | ||
"SV", | ||
"EI", | ||
"NI", | ||
"TE", | ||
"EL", | ||
"TV", | ||
"TT", | ||
"FT", | ||
"FF", | ||
) | ||
|
||
MBB_STATE_FIELDS = tuple(f"{p}ST" for p in _MBB_FIELD_PREFIXES) | ||
MBB_VALUE_FIELDS = tuple(f"{p}VL" for p in _MBB_FIELD_PREFIXES) | ||
MBB_MAX_CHOICES = len(_MBB_FIELD_PREFIXES) | ||
|
||
|
||
def convert_if_enum(attribute: Attribute[T], value: T) -> T | int: | ||
"""Check if `attribute` is a string enum and if so convert `value` to index of enum. | ||
Args: | ||
`attribute`: The attribute to be set | ||
`value`: The value | ||
Returns: | ||
The index of the `value` if the `attribute` is an enum, else `value` | ||
Raises: | ||
ValueError: If `attribute` is an enum and `value` is not in its allowed values | ||
""" | ||
match attribute: | ||
case Attribute( | ||
datatype=String(), allowed_values=allowed_values | ||
) if allowed_values is not None: | ||
if value in allowed_values: | ||
return allowed_values.index(value) | ||
else: | ||
raise ValueError(f"'{value}' not in allowed values {allowed_values}") | ||
case _: | ||
return value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,4 +21,3 @@ def test_ioc(ioc: None): | |
"c": {"w": "DEVICE:Child:C"}, | ||
"d": {"x": "DEVICE:Child:D"}, | ||
} | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import pytest | ||
|
||
from fastcs.attributes import AttrR | ||
from fastcs.backends.epics.util import convert_if_enum | ||
from fastcs.datatypes import String | ||
|
||
|
||
def test_convert_if_enum(): | ||
string_attr = AttrR(String()) | ||
enum_attr = AttrR(String(), allowed_values=["disabled", "enabled"]) | ||
|
||
assert convert_if_enum(string_attr, "enabled") == "enabled" | ||
|
||
assert convert_if_enum(enum_attr, "enabled") == 1 | ||
|
||
with pytest.raises(ValueError): | ||
convert_if_enum(enum_attr, "off") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters