-
Notifications
You must be signed in to change notification settings - Fork 0
/
better_rtf.py
75 lines (58 loc) · 1.87 KB
/
better_rtf.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
from plover.steno import STROKE_DELIMITER
from plover.steno_dictionary import StenoDictionary
import rtfcre
class RtfDictionary(StenoDictionary):
def __init__(self):
self._rtf_dict = rtfcre.RtfDictionary()
StenoDictionary.__init__(self)
def _load(self, filename):
with open(filename, "rb") as file:
self._rtf_dict = rtfcre.load(file)
def _save(self, filename):
with open(filename, "wb") as file:
self._rtf_dict.dump(file)
def __len__(self):
return len(self._rtf_dict)
def __contains__(self, steno):
return STROKE_DELIMITER.join(steno) in self._rtf_dict
def __getitem__(self, steno):
return self._rtf_dict[STROKE_DELIMITER.join(steno)]
def get(self, steno, default=None):
try:
return self[steno]
except KeyError:
return default
def __setitem__(self, steno, translation):
self._rtf_dict[STROKE_DELIMITER.join(steno)] = translation
def __delitem__(self, steno):
del self._rtf_dict[STROKE_DELIMITER.join(steno)]
@property
def _longest_key(self):
return self._rtf_dict.longest_key
@property
def _dict(self):
return {
tuple(stroke.split(STROKE_DELIMITER)): translation
for (stroke, translation) in self._rtf_dict.stroke_to_translation.items()
}
@_dict.setter
def _dict(self, new_val):
pass
@property
def reverse(self):
return {
translation: [tuple(stroke.split(STROKE_DELIMITER)) for stroke in strokes]
for (translation, strokes) in self._rtf_dict.translation_to_strokes.items()
}
@reverse.setter
def reverse(self, new_val):
pass
def clear(self):
self._rtf_dict = rtfcre.RtfDictionary()
def lookup(self, steno):
return self._rtf_dict.lookup(STROKE_DELIMITER.join(steno))
def reverse_lookup(self, translation):
return {
tuple(stroke.split(STROKE_DELIMITER))
for stroke in self._rtf_dict.reverse_lookup(translation)
}