Nyx Node
Loading...
Searching...
No Matches
enums.py
1# -*- coding: utf-8 -*-
2########################################################################################################################
3# NyxNode
4# Author: Jérôme ODIER <jerome.odier@lpsc.in2p3.fr>
5# SPDX-License-Identifier: GPL-3.0+
6
7########################################################################################################################
8
9from __future__ import annotations
10
11########################################################################################################################
12
13import enum
14import typing
15
16########################################################################################################################
17
18## @defgroup ENUMS_PY Enumerations used by NyxNode
19# @brief Enumerations used by NyxNode.
20
21########################################################################################################################
22
23## @ingroup ENUMS_PY
24class NyxState(enum.IntEnum):
25 """!
26 @brief Vector state hint.
27
28 | Symbol | Value | Description |
29 | :--------------- | :---- | :-------------- |
30 | `NyxState.IDLE` | 400 | State is idle. |
31 | `NyxState.OK` | 401 | State is ok. |
32 | `NyxState.BUSY` | 402 | State is busy. |
33 | `NyxState.ALERT` | 403 | State is alert. |
34 """
35
36 IDLE = 400
37 OK = 401
38 BUSY = 402
39 ALERT = 403
40
41 ####################################################################################################################
42
43 @staticmethod
44 def to_int(value: NyxState | int | str) -> int:
45 """!
46 @brief Converts a NyxState value as an integer.
47
48 @param value The value to convert.
49 @return The NyxState value as an integer.
50 """
51
52 return _nyx_enum_int(value, NyxState, _STATE_FROM_STR, 'state')
53
54 ####################################################################################################################
55
56 @staticmethod
57 def to_str(value: NyxState | int | str) -> str:
58 """!
59 @brief Converts a NyxState value as a string.
60
61 @param value The value to convert.
62 @return The NyxState value as a string.
63 """
64
65 return _nyx_enum_str(value, NyxState, _STATE_TO_STR, _STATE_FROM_STR, 'state')
66
67########################################################################################################################
68
69_STATE_TO_STR = {
70 NyxState.IDLE: 'Idle',
71 NyxState.OK: 'Ok',
72 NyxState.BUSY: 'Busy',
73 NyxState.ALERT: 'Alert',
74}
75
76_STATE_FROM_STR = {value: key for key, value in _STATE_TO_STR.items()}
77
78########################################################################################################################
79
80## @ingroup ENUMS_PY
81class NyxPerm(enum.IntEnum):
82 """!
83 @brief Vector permission hint.
84
85 | Symbol | Value | Description |
86 | :----------- | :---- |:------------- |
87 | `NyxPerm.RO` | 500 | Read only. |
88 | `NyxPerm.WO` | 501 | Write only. |
89 | `NyxPerm.RW` | 502 | Read & write. |
90 """
91
92 RO = 500
93 WO = 501
94 RW = 502
95
96 ####################################################################################################################
97
98 @staticmethod
99 def to_int(value: NyxPerm | int | str) -> int:
100 """!
101 @brief Converts a NyxPerm value as an integer.
102
103 @param value The value to convert.
104 @return The NyxPerm value as an integer.
105 """
106
107 return _nyx_enum_int(value, NyxPerm, _PERM_FROM_STR, 'permission')
108
109 ####################################################################################################################
110
111 @staticmethod
112 def to_str(value: NyxPerm | int | str) -> str:
113 """!
114 @brief Converts a NyxPerm value as a string.
115
116 @param value The value to convert.
117 @return The NyxPerm value as a string.
118 """
119
120 return _nyx_enum_str(value, NyxPerm, _PERM_TO_STR, _PERM_FROM_STR, 'permission')
121
122########################################################################################################################
123
124_PERM_TO_STR = {
125 NyxPerm.RO: 'ro',
126 NyxPerm.WO: 'wo',
127 NyxPerm.RW: 'rw',
128}
129
130_PERM_FROM_STR = {value: key for key, value in _PERM_TO_STR.items()}
131
132########################################################################################################################
133
134## @ingroup ENUMS_PY
135class NyxRule(enum.IntEnum):
136 """!
137 @brief Switch vector rule hint.
138
139 | Symbol | Value | Description |
140 | :-------------------- | :---- | :--------------------------------------------------------- |
141 | `NyxRule.ONE_OF_MANY` | 600 | Only one switch of many can be ON (e.g., radio buttons). |
142 | `NyxRule.AT_MOST_ONE` | 601 | At most one switch can be ON, but all switches can be off. |
143 | `NyxRule.ANY_OF_MANY` | 602 | Any number of switches can be ON (e.g., check boxes). |
144 """
145
146 ONE_OF_MANY = 600
147 AT_MOST_ONE = 601
148 ANY_OF_MANY = 602
149
150 ####################################################################################################################
151
152 @staticmethod
153 def nyx_rule_int(value: NyxRule | int | str) -> int:
154 """!
155 @brief Converts a NyxRule value as an integer.
156
157 @param value The value to convert.
158 @return The NyxRule value as an integer.
159 """
160
161 return _nyx_enum_int(value, NyxRule, _RULE_FROM_STR, 'rule')
162
163 ####################################################################################################################
164
165 @staticmethod
166 def nyx_rule_str(value: NyxRule | int | str) -> str:
167 """!
168 @brief Converts a NyxRule value as a string.
169
170 @param value The value to convert.
171 @return The NyxRule value as a string.
172 """
173
174 return _nyx_enum_str(value, NyxRule, _RULE_TO_STR, _RULE_FROM_STR, 'rule')
175
176########################################################################################################################
177
178_RULE_TO_STR = {
179 NyxRule.ONE_OF_MANY: 'OneOfMany',
180 NyxRule.AT_MOST_ONE: 'AtMostOne',
181 NyxRule.ANY_OF_MANY: 'AnyOfMany',
182}
183
184_RULE_FROM_STR = {value: key for key, value in _RULE_TO_STR.items()}
185
186########################################################################################################################
187
188## @ingroup ENUMS_PY
189class NyxOnOff(enum.IntEnum):
190 """!
191 @brief Switch state.
192
193 | Symbol | Value | Description |
194 | :------------- | :---- | :------------- |
195 | `NyxOnOff.ON` | 700 | Switch is ON. |
196 | `NyxOnOff.OFF` | 701 | Switch is OFF. |
197 """
198
199 ON = 700
200 OFF = 701
201
202 ####################################################################################################################
203
204 @staticmethod
205 def to_int(value: NyxOnOff | int | str | bool) -> int:
206 """!
207 @brief Converts a NyxOnOff value as an integer.
208
209 @param value The value to convert.
210 @return The NyxOnOff value as an integer.
211 """
212
213 if isinstance(value, bool):
214
215 return int(NyxOnOff.ON if value else NyxOnOff.OFF)
216
217 return _nyx_enum_int(value, NyxOnOff, _ONOFF_FROM_STR, 'onoff')
218
219 ####################################################################################################################
220
221 @staticmethod
222 def to_str(value: NyxOnOff | int | str | bool) -> str:
223 """!
224 @brief Converts a NyxOnOff value as a string.
225
226 @param value The value to convert.
227 @return The NyxOnOff value as a string.
228 """
229
230 if isinstance(value, bool):
231
232 return 'On' if value else 'Off'
233
234 return _nyx_enum_str(value, NyxOnOff, _ONOFF_TO_STR, _ONOFF_FROM_STR, 'onoff')
235
236########################################################################################################################
237
238_ONOFF_TO_STR = {
239 NyxOnOff.ON: 'On',
240 NyxOnOff.OFF: 'Off',
241}
242
243_ONOFF_FROM_STR = {value: key for key, value in _ONOFF_TO_STR.items()}
244
245########################################################################################################################
246
247_NyxEnum = typing.TypeVar('_NyxEnum', bound = enum.IntEnum)
248
249########################################################################################################################
250
251def _nyx_enum_int(value: _NyxEnum | int | str, enum_type: type[_NyxEnum], from_str: typing.Mapping[str, _NyxEnum], name: str) -> int:
252
253 ####################################################################################################################
254
255 if isinstance(value, enum_type):
256
257 return int(value)
258
259 ####################################################################################################################
260
261 if type(value) is int:
262
263 try:
264 return int(enum_type(value))
265 except ValueError:
266 raise ValueError(f'Invalid Nyx {name}: {value!r}') from None
267
268 ####################################################################################################################
269
270 if isinstance(value, str):
271
272 try:
273 return int(from_str[value])
274 except KeyError:
275 raise ValueError(f'Invalid Nyx {name}: {value!r}') from None
276
277 ####################################################################################################################
278
279 raise TypeError(f'Expected {enum_type.__name__}, int or str, got {type(value).__name__}')
280
281########################################################################################################################
282
283def _nyx_enum_str(value: _NyxEnum | int | str, enum_type: type[_NyxEnum], to_str: typing.Mapping[_NyxEnum, str], from_str: typing.Mapping[str, _NyxEnum], name: str) -> str:
284
285 return to_str[enum_type(_nyx_enum_int(value, enum_type, from_str, name))]
286
287########################################################################################################################
288
289__all__ = [name for name in globals() if name.lower().startswith('nyx')]
290
291########################################################################################################################
int to_int(NyxOnOff|int|str|bool value)
Converts a NyxOnOff value as an integer.
Definition enums.py:205
str to_str(NyxPerm|int|str value)
Converts a NyxPerm value as a string.
Definition enums.py:112
str to_str(NyxOnOff|int|str|bool value)
Converts a NyxOnOff value as a string.
Definition enums.py:222
int to_int(NyxState|int|str value)
Converts a NyxState value as an integer.
Definition enums.py:44
str to_str(NyxState|int|str value)
Converts a NyxState value as a string.
Definition enums.py:57
int to_int(NyxPerm|int|str value)
Converts a NyxPerm value as an integer.
Definition enums.py:99
int nyx_rule_int(NyxRule|int|str value)
Converts a NyxRule value as an integer.
Definition enums.py:153
str nyx_rule_str(NyxRule|int|str value)
Converts a NyxRule value as a string.
Definition enums.py:166
Switch state.
Definition enums.py:189
Vector permission hint.
Definition enums.py:81
Switch vector rule hint.
Definition enums.py:135
Vector state hint.
Definition enums.py:24