Skip to content

Commit 8149587

Browse files
committed
Implement TagVal parsers
1 parent f7a5563 commit 8149587

25 files changed

+1055
-79
lines changed

tcmenu/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
"__version__",
77
]
88

9-
__version__ = "4.1.1"
9+
__version__ = "4.1.2"

tcmenu/domain/edit_item_type.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,7 @@ class EditItemType(Enum):
3939
@property
4040
def message_id(self):
4141
return self._value_
42+
43+
@staticmethod
44+
def from_id(item_id: int):
45+
return EditItemType(item_id) if item_id in (item.value for item in EditItemType) else EditItemType.PLAIN_TEXT

tcmenu/domain/menu_items.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,18 @@ class BooleanMenuItem(MenuItem):
9696

9797
# noinspection PyArgumentList
9898
class BooleanNaming(Enum):
99-
ON_OFF = auto()
100-
YES_NO = auto()
101-
TRUE_FALSE = auto()
102-
CHECKBOX = auto()
99+
TRUE_FALSE = 0
100+
ON_OFF = 1
101+
YES_NO = 2
102+
CHECKBOX = 3
103+
104+
@staticmethod
105+
def from_id(item_id: int):
106+
return (
107+
BooleanMenuItem.BooleanNaming(item_id)
108+
if item_id in (item.value for item in BooleanMenuItem.BooleanNaming)
109+
else BooleanMenuItem.BooleanNaming.TRUE_FALSE
110+
)
103111

104112
"""the naming for this boolean, that describes how to render the true/false choice."""
105113
naming: BooleanNaming = BooleanNaming.ON_OFF

tcmenu/domain/state/list_response.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,6 @@ def from_string(value) -> Optional[ListResponse]:
4747
return ListResponse(row, response_type)
4848

4949
return None
50+
51+
52+
ListResponse.EMPTY = ListResponse(0, ListResponse.ResponseType.SELECT_ITEM)

tcmenu/remote/commands/ack_status.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,11 @@ def status_code(self) -> int:
4040
def is_error(self) -> bool:
4141
""":return: True if the status code is an error, otherwise False."""
4242
return self._value_.status_code > 0
43+
44+
@classmethod
45+
def from_status_code(cls, status_code: int):
46+
"""Retrieve status based on numeric code."""
47+
for status in cls:
48+
if status.status_code == status_code:
49+
return status
50+
return AckStatus.UNKNOWN_ERROR

tcmenu/remote/commands/command_factory.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ def new_menu_text_boot_command(
226226

227227
@staticmethod
228228
def new_menu_large_item_boot_command(
229-
parent_id: int, item: EditableLargeNumberMenuItem, current_value: int
229+
parent_id: int, item: EditableLargeNumberMenuItem, current_value: float
230230
) -> MenuLargeNumBootCommand:
231231
"""
232232
Create a new large number bootstrap command.

tcmenu/remote/commands/dialog_mode.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,12 @@ class DialogMode(Enum):
1515

1616
""" Perform the following action on the dialog. """
1717
ACTION = auto()
18+
19+
@staticmethod
20+
def from_string(mode: str) -> "DialogMode":
21+
if mode == "S":
22+
return DialogMode.SHOW
23+
elif mode == "H":
24+
return DialogMode.HIDE
25+
else:
26+
return DialogMode.ACTION

tcmenu/remote/commands/menu_boot_commands.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
from abc import ABC
22
from dataclasses import dataclass
3-
from typing import Any, Optional
3+
from typing import Any, Optional, TypeVar, Generic
44

5-
from tcmenu.domain.menu_items import MenuItem
5+
from tcmenu.domain.menu_items import MenuItem, AnalogMenuItem
66
from tcmenu.domain.state.menu_state import MenuState
77
from tcmenu.domain.util.menu_item_helper import MenuItemHelper
88
from tcmenu.remote.commands.menu_command import MenuCommand
99
from tcmenu.remote.commands.menu_command_type import MenuCommandType
1010
from tcmenu.remote.protocol.message_field import MessageField
1111

1212

13+
T = TypeVar("T", bound=MenuItem)
14+
15+
1316
@dataclass(frozen=True)
14-
class BootItemMenuCommand(MenuCommand, ABC):
15-
menu_item: MenuItem
17+
class BootItemMenuCommand(MenuCommand, ABC, Generic[T]):
18+
menu_item: T
1619

1720
current_value: Any
1821

tcmenu/remote/commands/menu_button_type.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,10 @@ def button_name(self) -> str:
3232
@property
3333
def type_value(self) -> int:
3434
return self._value_.type_value
35+
36+
@staticmethod
37+
def from_id(button_id: int):
38+
for button_type in MenuButtonType:
39+
if button_type.type_value == button_id:
40+
return button_type
41+
return MenuButtonType.NONE

tcmenu/remote/commands/menu_change_command.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ class ChangeType(Enum):
1616
ABSOLUTE_LIST = 2
1717
LIST_STATE_CHANGE = 3
1818

19+
@classmethod
20+
def from_id(cls, id_: int):
21+
"""Retrieve ChangeType based on numeric ID with a fallback to DELTA."""
22+
return cls(id_) if id_ in cls._value2member_map_ else cls.DELTA
23+
1924
menu_item_id: int
2025

2126
correlation_id: CorrelationId

0 commit comments

Comments
 (0)