From 14defd5d14e787fe06985138ef0ed648f07da932 Mon Sep 17 00:00:00 2001 From: user202729 <25191436+user202729@users.noreply.github.com> Date: Sat, 24 Oct 2020 11:29:04 +0700 Subject: [PATCH 1/5] Allow higher priority dictionaries to delete entries from others --- plover/steno_dictionary.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/plover/steno_dictionary.py b/plover/steno_dictionary.py index d18f47706..4912dfb6d 100644 --- a/plover/steno_dictionary.py +++ b/plover/steno_dictionary.py @@ -210,8 +210,10 @@ def _lookup(self, key, dicts=None, filters=()): if key_len > d.longest_key: continue value = d.get(key) - if value: + if value is not None: if not any(f(key, value) for f in filters): + if value.lower() == "{plover:deleted}": + return None return value def _lookup_from_all(self, key, dicts=None, filters=()): From c4c4049abf25140286b0a6521eb0d2c5fc87292c Mon Sep 17 00:00:00 2001 From: user202729 <25191436+user202729@users.noreply.github.com> Date: Sat, 24 Oct 2020 17:52:00 +0700 Subject: [PATCH 2/5] Make reverse_lookup handle deleted entries correctly --- plover/steno_dictionary.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/plover/steno_dictionary.py b/plover/steno_dictionary.py index 4912dfb6d..7c7fb7fde 100644 --- a/plover/steno_dictionary.py +++ b/plover/steno_dictionary.py @@ -198,7 +198,7 @@ def set_dicts(self, dicts): d.add_longest_key_listener(self._longest_key_listener) self._longest_key_listener() - def _lookup(self, key, dicts=None, filters=()): + def _lookup_keep_deleted(self, key, dicts=None, filters=()): if dicts is None: dicts = self.dicts key_len = len(key) @@ -212,10 +212,14 @@ def _lookup(self, key, dicts=None, filters=()): value = d.get(key) if value is not None: if not any(f(key, value) for f in filters): - if value.lower() == "{plover:deleted}": - return None return value + def _lookup(self, key, dicts=None, filters=()): + result = self._lookup_keep_deleted(key, dicts, filters) + if result is None or result.lower() == "{plover:deleted}": + return None + return result + def _lookup_from_all(self, key, dicts=None, filters=()): ''' Key lookup from all dictionaries @@ -263,7 +267,7 @@ def reverse_lookup(self, value): continue # Ignore key if it's overridden by a higher priority dictionary. keys.update(k for k in d.reverse_lookup(value) - if self._lookup(k, dicts=self.dicts[:n]) is None) + if self._lookup_keep_deleted(k, dicts=self.dicts[:n]) is None) return keys def casereverse_lookup(self, value): From 3b4d56d94fd32e99bf7b8a7edb9c1483a449434c Mon Sep 17 00:00:00 2001 From: user202729 <25191436+user202729@users.noreply.github.com> Date: Mon, 22 Jan 2024 04:54:22 +0700 Subject: [PATCH 3/5] Add some documentation for _lookup_keep_deleted --- plover/steno_dictionary.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/plover/steno_dictionary.py b/plover/steno_dictionary.py index 48ad3737a..a31b3c0c4 100644 --- a/plover/steno_dictionary.py +++ b/plover/steno_dictionary.py @@ -179,6 +179,12 @@ def set_dicts(self, dicts): self.dicts = dicts[:] def _lookup_keep_deleted(self, key, dicts=None, filters=()): + """ + Lookup a key in the given dicts. + If key appears in none of the dicts, return None. + If key appears in some dicts but with "{plover:deleted}" as value, + return "{plover:deleted}". + """ if dicts is None: dicts = self.dicts key_len = len(key) @@ -195,6 +201,11 @@ def _lookup_keep_deleted(self, key, dicts=None, filters=()): return value def _lookup(self, key, dicts=None, filters=()): + """ + Same as _lookup_keep_deleted, but if key appears in some dicts + but with "{plover:deleted}" as value (case-insensitive), + return None instead. + """ result = self._lookup_keep_deleted(key, dicts, filters) if result is None or result.lower() == "{plover:deleted}": return None @@ -203,7 +214,8 @@ def _lookup(self, key, dicts=None, filters=()): def _lookup_from_all(self, key, dicts=None, filters=()): ''' Key lookup from all dictionaries - Returns list of (value, dictionary) tuples + Returns list of (value, dictionary) tuples. + Entries with value "{plover:deleted}" are kept. ''' if dicts is None: dicts = self.dicts From 2618cbffd97dffb9b30442057cd9afdfd95abfae Mon Sep 17 00:00:00 2001 From: user202729 <25191436+user202729@users.noreply.github.com> Date: Mon, 22 Jan 2024 05:00:13 +0700 Subject: [PATCH 4/5] Add some tests for plover:deleted --- test/test_steno_dictionary.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/test_steno_dictionary.py b/test/test_steno_dictionary.py index 020680174..ccfa32aa0 100644 --- a/test/test_steno_dictionary.py +++ b/test/test_steno_dictionary.py @@ -14,18 +14,26 @@ def test_dictionary_collection(): d1 = StenoDictionary() d1[('S',)] = 'a' d1[('T',)] = 'b' + d1[('H',)] = '{PLOVER:deleted}' + d1[('R',)] = 'e' d1.path = 'd1' d2 = StenoDictionary() d2[('S',)] = 'c' d2[('W',)] = 'd' + d2[('H',)] = 'f' + d2[('R',)] = '{plover:deleted}' d2.path = 'd2' dc = StenoDictionaryCollection([d2, d1]) assert dc.lookup(('S',)) == 'c' assert dc.lookup(('W',)) == 'd' assert dc.lookup(('T',)) == 'b' + assert dc.lookup(('H',)) == 'f' + assert dc.lookup(('R',)) == None assert dc.lookup_from_all(('S',)) == [('c', d2), ('a', d1)] assert dc.lookup_from_all(('W',)) == [('d', d2)] assert dc.lookup_from_all(('T',)) == [('b', d1)] + assert dc.lookup_from_all(('H',)) == [('f', d2), ('{PLOVER:deleted}', d1)] + assert dc.lookup_from_all(('R',)) == [('{plover:deleted}', d2), ('e', d1)] f = lambda k, v: v == 'c' dc.add_filter(f) assert dc.lookup(('S',)) == 'a' From 2e92e94a9c668ee5c5fd756610cba4a03025cd65 Mon Sep 17 00:00:00 2001 From: user202729 <25191436+user202729@users.noreply.github.com> Date: Mon, 22 Jan 2024 05:03:26 +0700 Subject: [PATCH 5/5] Add news fragment --- news.d/feature/1160.core.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 news.d/feature/1160.core.md diff --git a/news.d/feature/1160.core.md b/news.d/feature/1160.core.md new file mode 100644 index 000000000..a111a011e --- /dev/null +++ b/news.d/feature/1160.core.md @@ -0,0 +1 @@ +Allow higher priority dictionaries to delete entries from others.