forked from opensteno/plover_python_dictionary
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplover_stdio_dictionary.py
More file actions
400 lines (322 loc) · 12 KB
/
plover_stdio_dictionary.py
File metadata and controls
400 lines (322 loc) · 12 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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# vim: set fileencoding=utf-8 :
from typing import Generic, IO, Literal, ParamSpec, TypeVar
from typing import cast, overload, TYPE_CHECKING
from collections.abc import Callable
import subprocess
import json
import threading
import queue
import inspect
from enum import Enum
from dataclasses import dataclass
from plover import log # type: ignore
from plover.steno_dictionary import StenoDictionary # type: ignore
T = TypeVar('T')
P = ParamSpec('P')
R = TypeVar('R')
# type hack for sentinel values from
# https://stackoverflow.com/questions/57959664/handling-conditional-logic-sentinel-value-with-mypy
class Sentinels(Enum):
GIVEN_DEFAULT = 0 # used for `ReturnArg`
NO_DEFAULT = 1 # used in `StdioDictionary._extract`
GivenDefault = Literal[Sentinels.GIVEN_DEFAULT]
GIVEN_DEFAULT: GivenDefault = Sentinels.GIVEN_DEFAULT
NoDefault = Literal[Sentinels.NO_DEFAULT]
NO_DEFAULT: NoDefault = Sentinels.NO_DEFAULT
class NoDefaultNeeded(Generic[T]):
pass
def is_exception_type(obj: object) -> bool:
return isinstance(obj, type) and issubclass(obj, Exception)
@dataclass
class ReturnArg(Generic[T]):
name: str
or_default: object | GivenDefault = GIVEN_DEFAULT
@overload
def handle_error(
return_ty: type[R], method: Literal["error"],
default: NoDefaultNeeded[R] = NoDefaultNeeded(),
*, ignore_inactive: bool = False
) -> Callable[[Callable[P, R]], Callable[P, R]]: ...
@overload
def handle_error(
return_ty: type[R], method: Literal["log"],
default: ReturnArg[R] | Callable[[], R] | R | type[Exception],
*, ignore_inactive: bool = False
) -> Callable[[Callable[P, R]], Callable[P, R]]: ...
def handle_error(
return_ty: type[R], method: Literal["error", "log"],
default: ReturnArg[R] | Callable[[], R] | R | type[Exception]
| NoDefaultNeeded[R] = NoDefaultNeeded(),
*, ignore_inactive: bool = False
) -> Callable[[Callable[P, R]], Callable[P, R]]:
"""
Error handling when the question of when and how to throw
errors is complicated is going to be a mess either way.
This decorator is supposed to simplify it a bit on the other
methods to aid in their readability, though in doing so all
the ugly details are swept under a very inelegant rug - this
function.
`method` is either:
* "error" - the error is passed on as is
* "log" - the error is logged
`default` is either
* `ReturnArg(...)` - return a given argument. Note that
this uses `inspect` to try and get the
correct position and default if
`GIVEN_DEFAULT` is used (used for
`StenoDictionary.get`)
* a callable object - return the result of a call with empty
arguments (used for mutable defaults,
so `lambda: []` instead of `[]`)
* an exception type - raise that exception. This also causes
exceptions of that type to not be
logged and be rethrown without
modification (used for
`StenoDictionary.__getitem__`)
* any other object - returned as is
If `ignore_inactive` is `True` this function will not
immediately try and return the default and instead run the
function anyway (used for `StenoDictionary._load`).
"""
def accept_function(func: Callable[P, R]) -> Callable[P, R]:
if isinstance(default, ReturnArg):
parameters = inspect.signature(func).parameters
arg_default = (
cast(R, parameters[default.name].default)
if default.or_default is GIVEN_DEFAULT
else cast(R, default.or_default)
)
arg_pos = next(
idx
for idx, name in enumerate(parameters.keys())
if name == default.name
)
def wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
self: StdioDictionary = \
cast(StdioDictionary, args[0])
def return_value() -> R:
if isinstance(default, ReturnArg):
if default.name in kwargs:
return cast(R, kwargs["fallback"])
elif arg_pos < len(args):
return cast(R, args[arg_pos])
else:
return arg_default
elif is_exception_type(default):
raise cast(type[Exception], default)
elif hasattr(default, "__call__"):
return cast(Callable[[], R], default)()
else:
return cast(R, default)
if not self._active and not ignore_inactive:
return return_value()
try:
return func(*args, **kwargs)
except Exception as ex:
if is_exception_type(default):
if isinstance(ex, cast(type, default)):
raise ex # rethrow unchanged
self._active = False
if method == "log":
log.error(str(ex))
return return_value()
elif method == "error":
raise ex
else:
raise NotImplementedError from ex
return wrapper
return accept_function
class StdioDictionary(StenoDictionary): # type: ignore
readonly = True
def __init__(self) -> None:
super().__init__()
self._filename: str
self._process: subprocess.Popen[str] | None = None
self._stdout: queue.SimpleQueue[str | None]
self._longest_key: int
self._timeout: float | None = None
self._untranslate: bool
self._seq: int
self._active: bool = False
self.readonly = True
def _expect_stdout(self) -> object:
out_s = self._stdout.get(timeout=self._timeout)
if out_s is None:
raise ValueError(
f"Dictionary {self._filename} exited early"
)
try:
out = json.loads(out_s)
except json.decoder.JSONDecodeError as ex:
raise ValueError(
f"Dictionary {self._filename} pushed invalid "
f"JSON: {out_s}"
) from ex
return out
def _extract(
self, obj: object, name: str, ty: type[T],
*, default: T | NoDefault = NO_DEFAULT
) -> T:
if not isinstance(obj, dict) or name not in obj:
if default is NO_DEFAULT:
raise ValueError(
f"Dictionary {self._filename} pushed an "
f"invalid object, expected {name} to be "
f"present"
)
return default
out = obj[name]
if TYPE_CHECKING:
if isinstance(ty, tuple):
for i in ty:
if isinstance(out, i):
return out
raise Exception
else:
if isinstance(out, ty):
return out
if not isinstance(out, ty):
raise ValueError(
f"Expected {name} to be of type {ty!r}"
)
return out
def _setup_process(self) -> None:
# terminate earlier instances if they exist:
if self._process is not None:
self._process.terminate()
self._process = subprocess.Popen(
[self._filename],
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
def handle_errors(file: IO[str]) -> None:
for i in file:
log.error(i)
threading.Thread(
target=handle_errors, args=(self._process.stderr,),
daemon=True
).start()
def handle_file(
file: IO[str],
output: queue.SimpleQueue[str | None]
) -> None:
for i in file:
output.put(i)
output.put(None)
self._stdout = queue.SimpleQueue()
threading.Thread(
target=handle_file,
args=(self._process.stdout, self._stdout),
daemon=True
).start()
def _communicate(
self, request: dict[str, object]
) -> object:
self._seq += 1
request["seq"] = self._seq
assert self._process is not None
assert self._process.stdin is not None
self._process.stdin.write(json.dumps(request) + "\n")
self._process.stdin.flush()
seq = -1
while seq < self._seq:
response = self._expect_stdout()
seq = self._extract(response, "seq", int, default=-1)
if seq > self._seq:
raise ValueError("The dictionary is in the future")
return response
@handle_error(type(None), "error", ignore_inactive=True)
def _load(self, filename: str) -> None:
self._filename = filename
self._active = True
self._setup_process()
# Get the global configuration for the dictionary
self._timeout = None
config = self._expect_stdout()
# Extract the longest key
self._longest_key = \
self._extract(config, "longest-key", int)
if self._longest_key <= 0:
raise ValueError(
f"'longest-key' is not a valid value: "
f"{self._longest_key}"
)
# Extract the maximum accepted latency
latency_ms = self._extract(
config, "max-latency-ms",
cast(
type[int | float | None],
int | float | type(None)
),
default=None
)
if latency_ms is not None:
if latency_ms <= 0:
raise ValueError(
f"The maximum latency is not a valid "
f"value: {latency_ms}"
)
self._timeout = (
latency_ms / 1000.
if latency_ms is not None
else None
)
# Extract if it is untranslate-capable
self._untranslate = self._extract(
config, "untranslate", bool, default=False
)
# Restart the sequence numbers
self._seq = -1
def _lookup(self, key: tuple[str, ...]) -> str:
if len(key) > self._longest_key:
raise KeyError
response = self._communicate({"translate": key})
out = self._extract(
response,
"translation",
cast(type[str | None], str | type(None)),
default=None
)
if out is None:
raise KeyError
return out
@handle_error(bool, "log", False)
def __contains__(self, key: tuple[str, ...]) -> bool:
try:
self._lookup(key)
return True
except KeyError:
return False
@handle_error(str, "log", KeyError)
def __getitem__(self, key: tuple[str, ...]) -> str:
return self._lookup(key)
@overload
def get(self, key: tuple[str, ...]) -> str | None: ...
@overload
def get(self, key: tuple[str, ...], fallback: T) \
-> str | T: ...
@handle_error( # type: ignore
str | T | None, "log", ReturnArg("fallback")
)
def get(
self, key: tuple[str, ...], fallback: T | None = None
) -> str | T | None:
try:
return self._lookup(key)
except KeyError:
return fallback
@handle_error(set[tuple[str, ...]], "log", lambda: set())
def reverse_lookup(self, value: str) \
-> set[tuple[str, ...]]:
response = self._communicate({"untranslate": value})
outl: list[object] = self._extract(
response,
"reverse-translation",
list[object] if TYPE_CHECKING else list,
default=[]
)
assert all(isinstance(i, list) for i in outl)
outll = cast(list[list[object]], outl)
assert all(isinstance(j, str) for i in outll for j in i)
return {tuple(i) for i in cast(list[list[str]], outll)}