comparison urwid_satext/keys.py @ 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
comparison
equal deleted inserted replaced
84:9f683df69a4c 85:e0c8274f9b1c
59 59
60 def __delitem__(self, action): 60 def __delitem__(self, action):
61 # we don't want to delete actions 61 # we don't want to delete actions
62 raise NotImplementedError 62 raise NotImplementedError
63 63
64 def update(self, dict_like): 64 def replace_shortcut(self, action, shortcut):
65 """Replace an existing action
66
67 @param action: name of an existing action
68 @param shortcut: new shortcut to use
69 @raise KeyError: action doesn't exists
70 """
71 assert isinstance(action, basestring)
72 if action not in self:
73 raise ValueError("Action [{}] doesn't exist".format(action))
74 super(ActionMap, self).__setitem__(action, shortcut)
75
76 def update(self, new_actions):
65 """Update actions with an other dictionary 77 """Update actions with an other dictionary
66 78
67 @param dict_like: dictionary like object to update actions 79 @param new_actions: dictionary object to update actions
80 @raise ValueError: something else than a dictionary is used
68 @raise: ConflictError if at least one of the new actions already exists 81 @raise: ConflictError if at least one of the new actions already exists
69 """ 82 """
70 if not isinstance(dict_like, dict): 83 if not isinstance(new_actions, dict):
71 raise ValueError("only dictionary subclasses are accepted for update") 84 raise ValueError("only dictionary subclasses are accepted for update")
72 conflict = dict_like.viewkeys() & self.viewkeys() 85 conflict = new_actions.viewkeys() & self.viewkeys()
73 if conflict: 86 if conflict:
74 raise ConflictError("The actions [{}] already exists".format(','.join(conflict))) 87 raise ConflictError("The actions [{}] already exists".format(','.join(conflict)))
75 for action, shortcut in dict_like.iteritems(): 88 for action, shortcut in new_actions.iteritems():
76 self[action] = shortcut 89 self[action] = shortcut
90
91 def replace(self, action_shortcuts_map):
92 """Replace shortcuts with an other dictionary
93
94 @param action_shortcuts_map: dictionary like object to update shortcuts
95 @raise ValueError: something else than a dictionary is used
96 @raise KeyError: action doesn't exists
97 """
98 if not isinstance(action_shortcuts_map, dict):
99 raise ValueError("only dictionary subclasses are accepted for replacing shortcuts")
100 for action, shortcut in action_shortcuts_map.iteritems():
101 self.replace_shortcut(action, shortcut)
77 102
78 def set_close_namespaces(self, close_namespaces, always_check=None): 103 def set_close_namespaces(self, close_namespaces, always_check=None):
79 """Set namespaces where conflicting shortcut should not happen 104 """Set namespaces where conflicting shortcut should not happen
80 105
81 used by check_namespaces to see if the same shortcut is not used in two close namespaces (e.g. 'tab' used in edit_bar and globally) 106 used by check_namespaces to see if the same shortcut is not used in two close namespaces (e.g. 'tab' used in edit_bar and globally)