9from __future__
import annotations
26 @brief Vector state hint.
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. |
44 def to_int(value: NyxState | int | str) -> int:
46 @brief Converts a NyxState value as an integer.
48 @param value The value to convert.
49 @return The NyxState value as an integer.
52 return _nyx_enum_int(value, NyxState, _STATE_FROM_STR,
'state')
57 def to_str(value: NyxState | int | str) -> str:
59 @brief Converts a NyxState value as a string.
61 @param value The value to convert.
62 @return The NyxState value as a string.
65 return _nyx_enum_str(value, NyxState, _STATE_TO_STR, _STATE_FROM_STR,
'state')
70 NyxState.IDLE:
'Idle',
72 NyxState.BUSY:
'Busy',
73 NyxState.ALERT:
'Alert',
76_STATE_FROM_STR = {value: key
for key, value
in _STATE_TO_STR.items()}
83 @brief Vector permission hint.
85 | Symbol | Value | Description |
86 | :----------- | :---- |:------------- |
87 | `NyxPerm.RO` | 500 | Read only. |
88 | `NyxPerm.WO` | 501 | Write only. |
89 | `NyxPerm.RW` | 502 | Read & write. |
99 def to_int(value: NyxPerm | int | str) -> int:
101 @brief Converts a NyxPerm value as an integer.
103 @param value The value to convert.
104 @return The NyxPerm value as an integer.
107 return _nyx_enum_int(value, NyxPerm, _PERM_FROM_STR,
'permission')
112 def to_str(value: NyxPerm | int | str) -> str:
114 @brief Converts a NyxPerm value as a string.
116 @param value The value to convert.
117 @return The NyxPerm value as a string.
120 return _nyx_enum_str(value, NyxPerm, _PERM_TO_STR, _PERM_FROM_STR,
'permission')
130_PERM_FROM_STR = {value: key
for key, value
in _PERM_TO_STR.items()}
137 @brief Switch vector rule hint.
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). |
155 @brief Converts a NyxRule value as an integer.
157 @param value The value to convert.
158 @return The NyxRule value as an integer.
161 return _nyx_enum_int(value, NyxRule, _RULE_FROM_STR,
'rule')
168 @brief Converts a NyxRule value as a string.
170 @param value The value to convert.
171 @return The NyxRule value as a string.
174 return _nyx_enum_str(value, NyxRule, _RULE_TO_STR, _RULE_FROM_STR,
'rule')
179 NyxRule.ONE_OF_MANY:
'OneOfMany',
180 NyxRule.AT_MOST_ONE:
'AtMostOne',
181 NyxRule.ANY_OF_MANY:
'AnyOfMany',
184_RULE_FROM_STR = {value: key
for key, value
in _RULE_TO_STR.items()}
193 | Symbol | Value | Description |
194 | :------------- | :---- | :------------- |
195 | `NyxOnOff.ON` | 700 | Switch is ON. |
196 | `NyxOnOff.OFF` | 701 | Switch is OFF. |
205 def to_int(value: NyxOnOff | int | str | bool) -> int:
207 @brief Converts a NyxOnOff value as an integer.
209 @param value The value to convert.
210 @return The NyxOnOff value as an integer.
213 if isinstance(value, bool):
215 return int(NyxOnOff.ON
if value
else NyxOnOff.OFF)
217 return _nyx_enum_int(value, NyxOnOff, _ONOFF_FROM_STR,
'onoff')
222 def to_str(value: NyxOnOff | int | str | bool) -> str:
224 @brief Converts a NyxOnOff value as a string.
226 @param value The value to convert.
227 @return The NyxOnOff value as a string.
230 if isinstance(value, bool):
232 return 'On' if value
else 'Off'
234 return _nyx_enum_str(value, NyxOnOff, _ONOFF_TO_STR, _ONOFF_FROM_STR,
'onoff')
243_ONOFF_FROM_STR = {value: key
for key, value
in _ONOFF_TO_STR.items()}
247_NyxEnum = typing.TypeVar(
'_NyxEnum', bound = enum.IntEnum)
251def _nyx_enum_int(value: _NyxEnum | int | str, enum_type: type[_NyxEnum], from_str: typing.Mapping[str, _NyxEnum], name: str) -> int:
255 if isinstance(value, enum_type):
261 if type(value)
is int:
264 return int(enum_type(value))
266 raise ValueError(f
'Invalid Nyx {name}: {value!r}')
from None
270 if isinstance(value, str):
273 return int(from_str[value])
275 raise ValueError(f
'Invalid Nyx {name}: {value!r}')
from None
279 raise TypeError(f
'Expected {enum_type.__name__}, int or str, got {type(value).__name__}')
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:
285 return to_str[enum_type(_nyx_enum_int(value, enum_type, from_str, name))]
289__all__ = [name
for name
in globals()
if name.lower().startswith(
'nyx')]
int to_int(NyxOnOff|int|str|bool value)
Converts a NyxOnOff value as an integer.
str to_str(NyxPerm|int|str value)
Converts a NyxPerm value as a string.
str to_str(NyxOnOff|int|str|bool value)
Converts a NyxOnOff value as a string.
int to_int(NyxState|int|str value)
Converts a NyxState value as an integer.
str to_str(NyxState|int|str value)
Converts a NyxState value as a string.
int to_int(NyxPerm|int|str value)
Converts a NyxPerm value as an integer.
int nyx_rule_int(NyxRule|int|str value)
Converts a NyxRule value as an integer.
str nyx_rule_str(NyxRule|int|str value)
Converts a NyxRule value as a string.