Skip to content

Commit 5ab83c6

Browse files
committed
Add support for Rule restriction by NT Type, apply to alarmNTEnum rule
1 parent 07dbcd2 commit 5ab83c6

File tree

8 files changed

+28
-30
lines changed

8 files changed

+28
-30
lines changed

examples/thread/mailbox_sharedntenum.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,8 @@
1111
"value.choices": ["STOP", "START", "STANDBY"],
1212
"display.description": "Pump on/off control word.",
1313
},
14-
handler_constructors={
15-
"alarmNTEnum": {
16-
"STOP": AlarmDict(
17-
severity=AlarmSeverity.MAJOR_ALARM, status=AlarmStatus.NO_STATUS, message="Shouldn't be off"
18-
)
19-
}
14+
alarmNTEnum={
15+
"STOP": AlarmDict(severity=AlarmSeverity.MAJOR_ALARM, status=AlarmStatus.NO_STATUS, message="Shouldn't be off")
2016
},
2117
) # setting initial value also open()'s
2218

p4pillon/nt/identify.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44

55
import re
6-
from enum import Enum, auto
6+
from enum import IntEnum, auto
77

88
from p4p import Type, Value
99

@@ -17,7 +17,7 @@
1717
)
1818

1919

20-
class NTType(Enum):
20+
class NTType(IntEnum):
2121
"""
2222
Normative Types in the Specification Document, excluding those in Appendix A
2323
"""

p4pillon/rules/alarm_ntenum_rule.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class AlarmNTEnumRule(AlarmRule):
1616
Uses a dictionary to map NTEnum values to severity, status, and message.
1717
"""
1818

19-
name = "alarmntenum"
19+
name = "alarmNTEnum"
2020
nttypes = [SupportedNTTypes.NTENUM]
2121
fields = ["alarm"]
2222

p4pillon/rules/rules.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from p4p.server import ServerOperation
2020
from p4p.server.raw import ServOpWrap
2121

22+
from p4pillon.nt.identify import NTType
2223
from p4pillon.utils import overwrite_marked
2324

2425
logger = logging.getLogger(__name__)
@@ -27,11 +28,11 @@
2728
class SupportedNTTypes(IntEnum):
2829
"""Supported Normative Types."""
2930

30-
NTSCALAR = auto()
31-
NTSCALARARRAY = auto()
32-
NTENUM = auto()
33-
NTTABLE = auto() # basic support only
34-
NTNDARRAY = auto() # basic support only
31+
NTSCALAR = NTType.NTSCALAR
32+
NTSCALARARRAY = NTType.NTSCALARARRAY
33+
NTENUM = NTType.NTENUM
34+
NTTABLE = NTType.NTTABLE # basic support only
35+
NTNDARRAY = NTType.NTNDARRAY # basic support only
3536
ALL = auto() # signal that this is essentially type independent
3637

3738

p4pillon/sharednt.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from p4p import Type, Value
1313

1414
from p4pillon.composite_handler import CompositeHandler
15-
from p4pillon.nt.identify import is_scalararray
15+
from p4pillon.nt.identify import id_nttype, is_scalararray
1616
from p4pillon.nthandlers import ComposeableRulesHandler
1717
from p4pillon.rules import (
1818
AlarmNTEnumRule,
@@ -56,6 +56,7 @@ class SharedNT(SharedPV, ABC):
5656
registered_handlers: list[type[BaseRule]] = [
5757
AlarmRule,
5858
ControlRule,
59+
AlarmNTEnumRule,
5960
ValueAlarmRule,
6061
TimestampRule,
6162
CalcRule,
@@ -66,9 +67,12 @@ def __init__(
6667
*,
6768
auth_handlers: OrderedDict[str, Handler] | None = None,
6869
user_handlers: OrderedDict[str, Handler] | None = None,
69-
handler_constructors: dict[str, Any] | None = None,
70+
registered_handlers: list[type[BaseRule]] | None = None,
7071
**kwargs,
7172
):
73+
if registered_handlers:
74+
self.registered_handlers = registered_handlers
75+
7276
# Create a CompositeHandler. If there is no user supplied handler, and this is not
7377
# an NT type then it won't do anything. Unfortunately, an empty CompositeHandler
7478
# will be discarded and won't be passed to the super().__init__
@@ -85,11 +89,6 @@ def __init__(
8589
if name and component_handler:
8690
handler[name] = component_handler
8791

88-
if handler_constructors and "alarmNTEnum" in handler_constructors:
89-
handler["alarmNTEnum"] = ComposeableRulesHandler(
90-
AlarmNTEnumRule(handler_constructors["alarmNTEnum"])
91-
)
92-
9392
if user_handlers:
9493
handler = handler | user_handlers
9594

@@ -240,7 +239,13 @@ def __setup_registered_rule(
240239
if len(supported_nttypes) == 1 and supported_nttypes == [SupportedNTTypes.ALL]:
241240
pass
242241
else:
243-
raise NotImplementedError("We're not yet testing for NT type!")
242+
matchfound = False
243+
type_id = id_nttype(nttype)
244+
for supported_nttype in supported_nttypes:
245+
if supported_nttype == type_id:
246+
matchfound = True
247+
if not matchfound:
248+
return (name, None, kwargs)
244249

245250
if required_fields:
246251
for required_field in required_fields:

tests/unit/test_sharednt.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,7 @@ def testntscalar_create_with_handlers(pvtype, expected_handlername):
121121

122122

123123
def testntenum_create():
124-
testpv = SharedNT(
125-
nt=NTEnum(), initial={"index": 0, "choices": ["OFF", "ON"]}, handler_constructors={"alarmNTEnum": {}}
126-
)
124+
testpv = SharedNT(nt=NTEnum(), initial={"index": 0, "choices": ["OFF", "ON"]}, alarmNTEnum={})
127125

128126
assert set(testpv.handler.keys()) == set(["alarm", "alarmNTEnum", "timestamp"])
129127
assert list(testpv.handler.keys())[-1] == "timestamp"
@@ -133,9 +131,9 @@ def testntenum_create_with_handlers():
133131
testpv = SharedNT(
134132
nt=NTEnum(),
135133
initial={"index": 0, "choices": ["OFF", "ON"]},
134+
alarmNTEnum={},
136135
auth_handlers=OrderedDict({"pre1": Handler(), "pre2": Handler()}),
137136
user_handlers=OrderedDict({"post1": Handler(), "post2": Handler()}),
138-
handler_constructors={"alarmNTEnum": {}},
139137
)
140138

141139
assert list(testpv.handler.keys()) == ["pre1", "pre2", "alarm", "alarmNTEnum", "post1", "post2", "timestamp"]

tests/unit/thread/test_pvrecipe.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ def test_ntenum_create_pv(mock_time):
370370
PVTypes.ENUM,
371371
description="test enum",
372372
initial_value={"index": 0, "choices": ["OFF", "ON"]},
373-
handler_constructors={"alarmNTEnum": {}},
373+
alarmNTEnum={},
374374
)
375375

376376
pv = recipe.create_pv("TEST:PV:ENUM")

tests/unit/thread/test_sharednt.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ def testntscalar_thread_create(pvtype, expected_handlername):
2727

2828

2929
def testntenum_thread_create():
30-
testpv = SharedNT(
31-
nt=NTEnum(), initial={"index": 0, "choices": ["OFF", "ON"]}, handler_constructors={"alarmNTEnum": {}}
32-
)
30+
testpv = SharedNT(nt=NTEnum(), initial={"index": 0, "choices": ["OFF", "ON"]}, alarmNTEnum={})
3331

3432
assert len(testpv.handler) == 3
3533
assert list(testpv.handler.keys()) == ["alarm", "alarmNTEnum", "timestamp"]

0 commit comments

Comments
 (0)