changeset 85:e0c8274f9b1c

added ability to replace shortcuts
author Goffi <goffi@goffi.org>
date Thu, 04 Sep 2014 17:13:19 +0200
parents 9f683df69a4c
children ea5b9a211bbb
files urwid_satext/keys.py
diffstat 1 files changed, 30 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/urwid_satext/keys.py	Thu Sep 04 16:50:12 2014 +0200
+++ b/urwid_satext/keys.py	Thu Sep 04 17:13:19 2014 +0200
@@ -61,20 +61,45 @@
         # we don't want to delete actions
         raise NotImplementedError
 
-    def update(self, dict_like):
+    def replace_shortcut(self, action, shortcut):
+        """Replace an existing action
+
+        @param action: name of an existing action
+        @param shortcut: new shortcut to use
+        @raise KeyError: action doesn't exists
+        """
+        assert isinstance(action, basestring)
+        if action not in self:
+            raise ValueError("Action [{}] doesn't exist".format(action))
+        super(ActionMap, self).__setitem__(action, shortcut)
+
+    def update(self, new_actions):
         """Update actions with an other dictionary
 
-        @param dict_like: dictionary like object to update actions
+        @param new_actions: dictionary object to update actions
+        @raise ValueError: something else than a dictionary is used
         @raise: ConflictError if at least one of the new actions already exists
         """
-        if not isinstance(dict_like, dict):
+        if not isinstance(new_actions, dict):
             raise ValueError("only dictionary subclasses are accepted for update")
-        conflict = dict_like.viewkeys() & self.viewkeys()
+        conflict = new_actions.viewkeys() & self.viewkeys()
         if conflict:
             raise ConflictError("The actions [{}] already exists".format(','.join(conflict)))
-        for action, shortcut in dict_like.iteritems():
+        for action, shortcut in new_actions.iteritems():
             self[action] = shortcut
 
+    def replace(self, action_shortcuts_map):
+        """Replace shortcuts with an other dictionary
+
+        @param action_shortcuts_map: dictionary like object to update shortcuts
+        @raise ValueError: something else than a dictionary is used
+        @raise KeyError: action doesn't exists
+        """
+        if not isinstance(action_shortcuts_map, dict):
+            raise ValueError("only dictionary subclasses are accepted for replacing shortcuts")
+        for action, shortcut in action_shortcuts_map.iteritems():
+            self.replace_shortcut(action, shortcut)
+
     def set_close_namespaces(self, close_namespaces, always_check=None):
         """Set namespaces where conflicting shortcut should not happen