Repository navigation
Expand file tree
/
Copy pathcommand_parser.py
More file actions
148 lines (121 loc) · 4.8 KB
/
Copy pathcommand_parser.py
File metadata and controls
148 lines (121 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import re
from dataclasses import dataclass
from enum import Enum
from typing import List, Union
class ActionKind(Enum):
BOOT = "boot"
REBOOT = "reboot"
ROLLBACK = "rollback"
COMBO = "combo"
KEY = "key"
TYPE = "type"
SENDM = "sendm"
MUTE = "mute"
@dataclass
class ParsedAction:
kind: ActionKind
value: Union[str, List[str]]
COMMANDS = {"boot", "reboot", "rollback", "act", "key", "type", "sendm", "mute"}
_PREFIXES = ('!',)
def _is_command_token(token: str) -> bool:
"""Check whether the token is a command of the form '.cmd' or '!cmd'."""
return len(token) > 1 and token[0] in _PREFIXES and token[1:].lower() in COMMANDS
def _split_combo(value: str) -> List[str]:
text = value.strip()
if not text:
return []
return [part.strip().lower() for part in text.split('+') if part.strip()]
def parse_message(text: str) -> List[ParsedAction]:
"""Parse a message into a list of actions.
Commands start with '!' (e.g. '!act', '!type', '!key'):
- !boot
- !reboot
- !rollback
- !act win+r !key enter !type cmd
- !type text !key enter
- !mute alice
- !type hello ! reboot
Unknown commands like '!word' are treated as plain text and ignored.
"""
if not text:
return []
normalized = text.strip()
if not normalized or normalized[0] not in _PREFIXES:
return []
tokens = re.split(r'\s+', normalized)
actions: List[ParsedAction] = []
i = 0
while i < len(tokens):
token = tokens[i]
if not token:
i += 1
continue
if token in _PREFIXES:
i += 1
if i < len(tokens):
next_raw = tokens[i].strip()
if next_raw and next_raw.lower() in COMMANDS:
token = token + next_raw.lower()
else:
continue
else:
continue
if len(token) > 1 and token[0] in _PREFIXES:
command = token[1:].lower()
if command in COMMANDS:
if command in {ActionKind.BOOT.value, ActionKind.REBOOT.value, ActionKind.ROLLBACK.value}:
actions.append(ParsedAction(kind=ActionKind(command), value=command))
i += 1
continue
if command == 'act':
payload_tokens = []
i += 1
while i < len(tokens):
next_token = tokens[i]
if next_token in _PREFIXES or _is_command_token(next_token):
break
payload_tokens.append(next_token)
i += 1
if payload_tokens:
actions.append(ParsedAction(kind=ActionKind.COMBO, value=_split_combo(' '.join(payload_tokens))))
continue
if command == ActionKind.KEY.value:
i += 1
if i < len(tokens):
next_token = tokens[i]
if next_token not in _PREFIXES and not _is_command_token(next_token):
actions.append(ParsedAction(kind=ActionKind.KEY, value=next_token))
continue
if command == ActionKind.TYPE.value:
payload_tokens = []
i += 1
while i < len(tokens):
next_token = tokens[i]
if next_token in _PREFIXES or _is_command_token(next_token):
break
payload_tokens.append(next_token)
i += 1
if payload_tokens:
actions.append(ParsedAction(kind=ActionKind.TYPE, value=' '.join(payload_tokens)))
continue
if command == ActionKind.SENDM.value:
payload_tokens = []
i += 1
while i < len(tokens):
next_token = tokens[i]
if next_token in _PREFIXES or _is_command_token(next_token):
break
payload_tokens.append(next_token)
i += 1
if payload_tokens:
actions.append(ParsedAction(kind=ActionKind.SENDM, value=' '.join(payload_tokens)))
continue
if command == ActionKind.MUTE.value:
i += 1
if i < len(tokens):
next_token = tokens[i]
if next_token not in _PREFIXES and not _is_command_token(next_token):
actions.append(ParsedAction(kind=ActionKind.MUTE, value=next_token))
continue
i += 1
return actions