Skip to content

Commit fa8d7f5

Browse files
authored
Allow higher priority dictionaries to delete entries from others (#1160)
1 parent 65fc2b4 commit fa8d7f5

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

news.d/feature/1160.core.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Allow higher priority dictionaries to delete entries from others.

plover/steno_dictionary.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,13 @@ def longest_key(self):
183183
def set_dicts(self, dicts):
184184
self.dicts = dicts[:]
185185

186-
def _lookup(self, key, dicts=None, filters=()):
186+
def _lookup_keep_deleted(self, key, dicts=None, filters=()):
187+
"""
188+
Lookup a key in the given dicts.
189+
If key appears in none of the dicts, return None.
190+
If key appears in some dicts but with "{plover:deleted}" as value,
191+
return "{plover:deleted}".
192+
"""
187193
if dicts is None:
188194
dicts = self.dicts
189195
key_len = len(key)
@@ -195,14 +201,26 @@ def _lookup(self, key, dicts=None, filters=()):
195201
if key_len > d.longest_key:
196202
continue
197203
value = d.get(key)
198-
if value:
204+
if value is not None:
199205
if not any(f(key, value) for f in filters):
200206
return value
201207

208+
def _lookup(self, key, dicts=None, filters=()):
209+
"""
210+
Same as _lookup_keep_deleted, but if key appears in some dicts
211+
but with "{plover:deleted}" as value (case-insensitive),
212+
return None instead.
213+
"""
214+
result = self._lookup_keep_deleted(key, dicts, filters)
215+
if result is None or result.lower() == "{plover:deleted}":
216+
return None
217+
return result
218+
202219
def _lookup_from_all(self, key, dicts=None, filters=()):
203220
"""Key lookup from all dictionaries
204221
205-
Returns list of (value, dictionary) tuples
222+
Returns list of (value, dictionary) tuples.
223+
Entries with value "{plover:deleted}" are kept.
206224
"""
207225
if dicts is None:
208226
dicts = self.dicts
@@ -248,7 +266,7 @@ def reverse_lookup(self, value):
248266
keys.update(
249267
k
250268
for k in d.reverse_lookup(value)
251-
if self._lookup(k, dicts=self.dicts[:n]) is None
269+
if self._lookup_keep_deleted(k, dicts=self.dicts[:n]) is None
252270
)
253271
return keys
254272

test/test_steno_dictionary.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,26 @@ def test_dictionary_collection():
1414
d1 = StenoDictionary()
1515
d1[("S",)] = "a"
1616
d1[("T",)] = "b"
17+
d1[("H",)] = "{PLOVER:deleted}"
18+
d1[("R",)] = "e"
1719
d1.path = "d1"
1820
d2 = StenoDictionary()
1921
d2[("S",)] = "c"
2022
d2[("W",)] = "d"
23+
d2[("H",)] = "f"
24+
d2[("R",)] = "{plover:deleted}"
2125
d2.path = "d2"
2226
dc = StenoDictionaryCollection([d2, d1])
2327
assert dc.lookup(("S",)) == "c"
2428
assert dc.lookup(("W",)) == "d"
2529
assert dc.lookup(("T",)) == "b"
30+
assert dc.lookup(("H",)) == "f"
31+
assert dc.lookup(("R",)) == None
2632
assert dc.lookup_from_all(("S",)) == [("c", d2), ("a", d1)]
2733
assert dc.lookup_from_all(("W",)) == [("d", d2)]
2834
assert dc.lookup_from_all(("T",)) == [("b", d1)]
35+
assert dc.lookup_from_all(("H",)) == [("f", d2), ("{PLOVER:deleted}", d1)]
36+
assert dc.lookup_from_all(("R",)) == [("{plover:deleted}", d2), ("e", d1)]
2937
f = lambda k, v: v == "c"
3038
dc.add_filter(f)
3139
assert dc.lookup(("S",)) == "a"
@@ -50,9 +58,13 @@ def test_dictionary_collection():
5058
assert dc.lookup(("S",)) == "c"
5159
assert dc.lookup(("W",)) == "d"
5260
assert dc.lookup(("T",)) == "b"
61+
assert dc.lookup(("H",)) == "f"
62+
assert dc.lookup(("R",)) == None
5363
assert dc.lookup_from_all(("S",)) == [("c", d2), ("a", d1)]
5464
assert dc.lookup_from_all(("W",)) == [("d", d2)]
5565
assert dc.lookup_from_all(("T",)) == [("b", d1)]
66+
assert dc.lookup_from_all(("H",)) == [("f", d2), ("{PLOVER:deleted}", d1)]
67+
assert dc.lookup_from_all(("R",)) == [("{plover:deleted}", d2), ("e", d1)]
5668

5769
assert dc.reverse_lookup("c") == {("S",)}
5870

0 commit comments

Comments
 (0)