-
Notifications
You must be signed in to change notification settings - Fork 65
/
message.py
348 lines (277 loc) · 10.8 KB
/
message.py
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
from __future__ import annotations
import ast
import base64
import itertools
import string
import sys
from abc import abstractmethod
from base64 import b64encode
from collections import namedtuple
from typing import Any
from typing import cast
from typing import Generator
from typing import List
from typing import NoReturn
from typing import Optional
from typing import Protocol
from typing import runtime_checkable
from typing import Tuple
from typing import Type
from typing import TYPE_CHECKING
if sys.version_info >= (3, 10):
from typing import TypeGuard
else:
from typing_extensions import TypeGuard
from typing import TypeVar
from typing import Union
class OutOfMessageTypes(Exception): ...
Position = namedtuple('Position', ('x', 'y', 'width', 'height'))
@runtime_checkable
class BytesLineReadable(Protocol):
def readline(self) -> bytes: ...
def is_window_control_list_response(resp_obj: object) -> TypeGuard[Tuple[str, List[Tuple[str, str]]]]:
if not isinstance(resp_obj, tuple):
return False
if len(resp_obj) != 2:
return False
if not isinstance(resp_obj[0], str):
return False
expected_win_list = resp_obj[1]
if not isinstance(expected_win_list, list):
return False
for obj in expected_win_list:
if not isinstance(obj, tuple):
return False
if len(obj) != 2:
return False
id_, klass = obj
if not isinstance(id_, str) or not isinstance(klass, str):
return False
return True
def is_winget_response_type(
obj: object,
) -> TypeGuard[
Union[
'StringResponseMessage',
'IntegerResponseMessage',
'WindowListResponseMessage',
'WindowControlListResponseMessage',
]
]:
if isinstance(obj, StringResponseMessage):
return True
elif isinstance(obj, IntegerResponseMessage):
return True
elif isinstance(obj, WindowListResponseMessage):
return True
elif isinstance(obj, WindowControlListResponseMessage):
return True
elif isinstance(obj, NoValueResponseMessage):
return True
else:
return False
T_ResponseMessageType = TypeVar('T_ResponseMessageType', bound='ResponseMessage')
def tom_generator() -> Generator[bytes, None, None]:
characters = string.digits + string.ascii_letters
for a, b, c in itertools.product(characters, characters, characters):
yield bytes(f'{a}{b}{c}', encoding='ascii')
raise OutOfMessageTypes('Out of TOMS')
TOMS = tom_generator()
class ResponseMessage:
_type_order_mark = next(TOMS)
@classmethod
def fqn(cls) -> str:
return f'{cls.__module__}.{cls.__qualname__}'
@classmethod
def __init_subclass__(cls: Type[T_ResponseMessageType], **kwargs: Any) -> None:
tom = next(TOMS)
cls._type_order_mark = tom
assert tom not in _message_registry, f'cannot register class {cls!r} with TOM {tom!r} which is already in use'
_message_registry[tom] = cls
super().__init_subclass__(**kwargs)
def __init__(self, raw_content: bytes, engine: Optional[Union[AsyncAHK[Any], AHK[Any]]] = None):
self._raw_content: bytes = raw_content
self._engine: Optional[Union[AsyncAHK[Any], AHK[Any]]] = engine
def __repr__(self) -> str:
return f'ResponseMessage<fqn={self.fqn()!r}>'
@staticmethod
def _tom_lookup(tom: bytes) -> 'ResponseMessageClassTypes':
klass = _message_registry.get(tom)
if klass is None:
raise ValueError(f'No such TOM {tom!r}')
return klass
@classmethod
def from_bytes(
cls: Type[T_ResponseMessageType], b: bytes, engine: Optional[Union[AsyncAHK[Any], AHK[Any]]] = None
) -> 'ResponseMessageTypes':
tom, _, message_bytes = b.split(b'\n', 2)
klass = cls._tom_lookup(tom)
return klass(raw_content=message_bytes, engine=engine)
def to_bytes(self) -> bytes:
content_lines = self._raw_content.count(b'\n')
return self._type_order_mark + b'\n' + bytes(str(content_lines), 'ascii') + b'\n' + self._raw_content
@abstractmethod
def unpack(self) -> Any:
return NotImplemented
_message_registry: dict[bytes, 'ResponseMessageClassTypes'] = {}
class TupleResponseMessage(ResponseMessage):
def unpack(self) -> Tuple[Any, ...]:
s = self._raw_content.decode(encoding='utf-8')
val = ast.literal_eval(s)
assert isinstance(val, tuple)
return val
class CoordinateResponseMessage(ResponseMessage):
def unpack(self) -> Tuple[int, int]:
s = self._raw_content.decode(encoding='utf-8')
val = ast.literal_eval(s)
assert isinstance(val, tuple)
x, y = cast(Tuple[int, int], val)
return x, y
class IntegerResponseMessage(ResponseMessage):
def unpack(self) -> int:
s = self._raw_content.decode(encoding='utf-8')
val = ast.literal_eval(s)
assert isinstance(val, int)
return val
class BooleanResponseMessage(IntegerResponseMessage):
def unpack(self) -> bool:
val = super().unpack()
assert val in (1, 0)
return bool(val)
class StringResponseMessage(ResponseMessage):
def unpack(self) -> str:
return self._raw_content.decode('utf-8')
class WindowListResponseMessage(ResponseMessage):
def unpack(self) -> Union[List[Window], List[AsyncWindow]]:
from ._async.engine import AsyncAHK
from ._async.window import AsyncWindow
from ._sync.window import Window
from ._sync.engine import AHK
s = self._raw_content.decode(encoding='utf-8')
s = s.rstrip(',')
window_ids = s.split(',')
if isinstance(self._engine, AsyncAHK):
async_ret = [AsyncWindow(engine=self._engine, ahk_id=ahk_id) for ahk_id in window_ids if ahk_id]
return async_ret
elif isinstance(self._engine, AHK):
ret = [Window(engine=self._engine, ahk_id=ahk_id) for ahk_id in window_ids if ahk_id]
return ret
else:
raise ValueError(f'Invalid engine: {self._engine!r}')
class NoValueResponseMessage(ResponseMessage):
def unpack(self) -> None:
assert self._raw_content == b'\xee\x80\x80', f'Unexpected or Malformed response: {self._raw_content!r}'
return None
class AHKExecutionException(Exception):
pass
class ExceptionResponseMessage(ResponseMessage):
_exception_type: Type[Exception] = AHKExecutionException
def unpack(self) -> NoReturn:
s = self._raw_content.decode(encoding='utf-8')
raise self._exception_type(s)
class WindowControlListResponseMessage(ResponseMessage):
def unpack(self) -> Union[List[AsyncControl], List[Control]]:
from ._async.engine import AsyncAHK
from ._async.window import AsyncWindow, AsyncControl
from ._sync.window import Window, Control
from ._sync.engine import AHK
s = self._raw_content.decode(encoding='utf-8')
val = ast.literal_eval(s)
assert is_window_control_list_response(val)
assert self._engine is not None
assert val is not None
ahkid, controls = val
if isinstance(self._engine, AsyncAHK):
ret_async: List[AsyncControl] = []
async_window = AsyncWindow(engine=self._engine, ahk_id=ahkid)
for control in controls:
hwnd, classname = control
async_ctrl = AsyncControl(window=async_window, hwnd=hwnd, control_class=classname)
ret_async.append(async_ctrl)
return ret_async
elif isinstance(self._engine, AHK):
ret_sync: List[Control] = []
window = Window(engine=self._engine, ahk_id=ahkid)
for control in controls:
hwnd, classname = control
ctrl = Control(window=window, hwnd=hwnd, control_class=classname)
ret_sync.append(ctrl)
return ret_sync
else:
raise ValueError(f'Invalid engine: {self._engine!r}')
class WindowResponseMessage(ResponseMessage):
def unpack(self) -> Union[Window, AsyncWindow]:
from ._async.engine import AsyncAHK
from ._async.window import AsyncWindow
from ._sync.window import Window
from ._sync.engine import AHK
s = self._raw_content.decode(encoding='utf-8')
ahk_id = s.strip()
if isinstance(self._engine, AsyncAHK):
async_ret = AsyncWindow(engine=self._engine, ahk_id=ahk_id)
return async_ret
elif isinstance(self._engine, AHK):
ret = Window(engine=self._engine, ahk_id=ahk_id)
return ret
else:
raise ValueError(f'Invalid engine: {self._engine!r}')
class PositionResponseMessage(TupleResponseMessage):
def unpack(self) -> Position:
resp = super().unpack()
if not len(resp) == 4:
raise ValueError(f'Unexpected response. Expected tuple of length 4, got tuple of length {len(resp)}')
pos = Position(*resp)
return pos
class FloatResponseMessage(ResponseMessage):
def unpack(self) -> float:
s = self._raw_content.decode(encoding='utf-8')
val = ast.literal_eval(s)
assert isinstance(val, float)
return val
class TimeoutResponseMessage(ExceptionResponseMessage):
_exception_type = TimeoutError
class B64BinaryResponseMessage(ResponseMessage):
def unpack(self) -> bytes:
b64_content = self._raw_content
b = base64.b64decode(b64_content)
return b
T_RequestMessageType = TypeVar('T_RequestMessageType', bound='RequestMessage')
class RequestMessage:
def __init__(self, function_name: str, args: Optional[List[str]] = None):
self.function_name: str = function_name
self.args: List[str] = args or []
def format(self) -> bytes:
arg_binary = b'|'.join(b64encode(bytes(arg, 'UTF-8')) for arg in self.args)
ret = bytes(self.function_name, 'UTF-8') + b'|' + arg_binary + b'\n'
return ret
ResponseMessageTypes = Union[
ResponseMessage,
TupleResponseMessage,
CoordinateResponseMessage,
IntegerResponseMessage,
BooleanResponseMessage,
StringResponseMessage,
WindowListResponseMessage,
NoValueResponseMessage,
WindowControlListResponseMessage,
ExceptionResponseMessage,
PositionResponseMessage,
]
ResponseMessageClassTypes = Union[
Type[PositionResponseMessage],
Type[TupleResponseMessage],
Type[CoordinateResponseMessage],
Type[IntegerResponseMessage],
Type[BooleanResponseMessage],
Type[StringResponseMessage],
Type[WindowListResponseMessage],
Type[NoValueResponseMessage],
Type[WindowControlListResponseMessage],
Type[ExceptionResponseMessage],
Type[ResponseMessage],
]
if TYPE_CHECKING:
from ._async.engine import AsyncAHK
from ._async.window import AsyncWindow, AsyncControl
from ._sync.window import Window, Control
from ._sync.engine import AHK