From f43a4894fba7d20aba04bf22ba200cc62b8418a0 Mon Sep 17 00:00:00 2001 From: woorst Date: Wed, 19 Jul 2017 18:24:04 -0500 Subject: [PATCH 1/2] set_bindings can now update or replace existing bindings --- rtv/objects.py | 16 +++++++++------- tests/test_config.py | 6 +++--- tests/test_objects.py | 9 ++++++--- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/rtv/objects.py b/rtv/objects.py index b6a42a1..5b76d14 100644 --- a/rtv/objects.py +++ b/rtv/objects.py @@ -642,15 +642,17 @@ class KeyMap(object): self._keymap = None self.set_bindings(bindings) - def set_bindings(self, bindings): - # Clear the keymap before applying the bindings to avoid confusion. - # If a user defines custom bindings in their config file, they must - # explicitly define ALL of the bindings. - self._keymap = {} + def set_bindings(self, bindings, mode='update'): + new_keymap = {} for command, keys in bindings.items(): if not isinstance(command, Command): command = Command(command) - self._keymap[command] = keys + new_keymap[command] = keys + + if not self._keymap or mode == 'replace': + self._keymap = new_keymap + elif mode == 'update': + self._keymap.update(new_keymap) def get(self, command): if not isinstance(command, Command): @@ -694,4 +696,4 @@ class KeyMap(object): except (AttributeError, ValueError, TypeError): raise exceptions.ConfigError('Invalid configuration! "%s" is not a ' - 'valid key' % key) \ No newline at end of file + 'valid key' % key) diff --git a/tests/test_config.py b/tests/test_config.py index 51c40b6..26e7ee5 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -136,7 +136,7 @@ def test_config_from_file(): fargs, fbindings = Config.get_file(filename=fp.name) config = Config(**fargs) - config.keymap.set_bindings(fbindings) + config.keymap.set_bindings(fbindings, 'replace') assert config.config == {} assert config.keymap._keymap == {} @@ -153,7 +153,7 @@ def test_config_from_file(): fp.flush() fargs, fbindings = Config.get_file(filename=fp.name) config.update(**fargs) - config.keymap.set_bindings(fbindings) + config.keymap.set_bindings(fbindings, 'replace') assert config.config == args assert config.keymap.get('REFRESH') == ['r', ''] assert config.keymap.get('UPVOTE') == [''] @@ -224,4 +224,4 @@ def test_config_history(): config.delete_history() assert len(config.history) == 0 - assert not os.path.exists(fp.name) \ No newline at end of file + assert not os.path.exists(fp.name) diff --git a/tests/test_objects.py b/tests/test_objects.py index 1ac11ca..6c2b915 100644 --- a/tests/test_objects.py +++ b/tests/test_objects.py @@ -370,13 +370,16 @@ def test_objects_keymap(): keymap.get('downvote') assert 'DOWNVOTE' in six.text_type(e) - # Updating the bindings wipes out the old ones bindings = {'refresh': ['a', 0x12, '', '']} - keymap.set_bindings(bindings) + bindings2 = {'upvote': ['b', 0x13, '']} + keymap.set_bindings(bindings, 'replace') assert keymap.get('refresh') with pytest.raises(exceptions.ConfigError) as e: keymap.get('upvote') assert 'UPVOTE' in six.text_type(e) + keymap.set_bindings(bindings2, 'update') + assert keymap.get('refresh') + assert keymap.get('upvote') # Strings should be parsed correctly into keys assert KeyMap.parse('a') == 97 @@ -589,4 +592,4 @@ def test_objects_navigator_flip(): nav.flip(3) assert nav.page_index == 2 assert nav.cursor_index == 3 - assert not nav.inverted \ No newline at end of file + assert not nav.inverted From eb837716d8cfd0667f65e86aa7d1d07946315c9a Mon Sep 17 00:00:00 2001 From: woorst Date: Sun, 23 Jul 2017 13:31:11 -0500 Subject: [PATCH 2/2] simplify code to always update keymap --- rtv/objects.py | 6 +++--- tests/test_config.py | 7 ++++--- tests/test_objects.py | 5 +++-- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/rtv/objects.py b/rtv/objects.py index 5b76d14..48f0074 100644 --- a/rtv/objects.py +++ b/rtv/objects.py @@ -642,16 +642,16 @@ class KeyMap(object): self._keymap = None self.set_bindings(bindings) - def set_bindings(self, bindings, mode='update'): + def set_bindings(self, bindings): new_keymap = {} for command, keys in bindings.items(): if not isinstance(command, Command): command = Command(command) new_keymap[command] = keys - if not self._keymap or mode == 'replace': + if not self._keymap: self._keymap = new_keymap - elif mode == 'update': + else: self._keymap.update(new_keymap) def get(self, command): diff --git a/tests/test_config.py b/tests/test_config.py index 26e7ee5..060a6e4 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -136,9 +136,10 @@ def test_config_from_file(): fargs, fbindings = Config.get_file(filename=fp.name) config = Config(**fargs) - config.keymap.set_bindings(fbindings, 'replace') + default_keymap = config.keymap._keymap.copy() + config.keymap.set_bindings(fbindings) assert config.config == {} - assert config.keymap._keymap == {} + assert config.keymap._keymap == default_keymap # [rtv] rows = ['{0}={1}'.format(key, val) for key, val in args.items()] @@ -153,7 +154,7 @@ def test_config_from_file(): fp.flush() fargs, fbindings = Config.get_file(filename=fp.name) config.update(**fargs) - config.keymap.set_bindings(fbindings, 'replace') + config.keymap.set_bindings(fbindings) assert config.config == args assert config.keymap.get('REFRESH') == ['r', ''] assert config.keymap.get('UPVOTE') == [''] diff --git a/tests/test_objects.py b/tests/test_objects.py index 6c2b915..e3d8213 100644 --- a/tests/test_objects.py +++ b/tests/test_objects.py @@ -372,12 +372,13 @@ def test_objects_keymap(): bindings = {'refresh': ['a', 0x12, '', '']} bindings2 = {'upvote': ['b', 0x13, '']} - keymap.set_bindings(bindings, 'replace') + keymap._keymap = {} + keymap.set_bindings(bindings) assert keymap.get('refresh') with pytest.raises(exceptions.ConfigError) as e: keymap.get('upvote') assert 'UPVOTE' in six.text_type(e) - keymap.set_bindings(bindings2, 'update') + keymap.set_bindings(bindings2) assert keymap.get('refresh') assert keymap.get('upvote')