comparison urwid_satext/keys.py @ 143:144bdf877d21

python 3 port (using 2to3)
author Goffi <goffi@goffi.org>
date Tue, 13 Aug 2019 08:55:41 +0200
parents 2a1880d5d450
children
comparison
equal deleted inserted replaced
142:2855123621a0 143:144bdf877d21
16 # 16 #
17 # You should have received a copy of the GNU Lesser General Public License 17 # You should have received a copy of the GNU Lesser General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 """This module manage action <==> key mapping and can be extended to add new actions""" 20 """This module manage action <==> key mapping and can be extended to add new actions"""
21 from functools import reduce
21 22
22 23
23 class ConflictError(Exception): 24 class ConflictError(Exception):
24 pass 25 pass
25 26
66 67
67 @param action: name of an existing action 68 @param action: name of an existing action
68 @param shortcut: new shortcut to use 69 @param shortcut: new shortcut to use
69 @raise KeyError: action doesn't exists 70 @raise KeyError: action doesn't exists
70 """ 71 """
71 assert isinstance(action, basestring) 72 assert isinstance(action, str)
72 if action not in self: 73 if action not in self:
73 raise ValueError("Action [{}] doesn't exist".format(action)) 74 raise ValueError("Action [{}] doesn't exist".format(action))
74 super(ActionMap, self).__setitem__(action, shortcut) 75 super(ActionMap, self).__setitem__(action, shortcut)
75 76
76 def update(self, new_actions): 77 def update(self, new_actions):
80 @raise ValueError: something else than a dictionary is used 81 @raise ValueError: something else than a dictionary is used
81 @raise: ConflictError if at least one of the new actions already exists 82 @raise: ConflictError if at least one of the new actions already exists
82 """ 83 """
83 if not isinstance(new_actions, dict): 84 if not isinstance(new_actions, dict):
84 raise ValueError("only dictionary subclasses are accepted for update") 85 raise ValueError("only dictionary subclasses are accepted for update")
85 conflict = new_actions.viewkeys() & self.viewkeys() 86 conflict = new_actions.keys() & self.keys()
86 if conflict: 87 if conflict:
87 raise ConflictError("The actions [{}] already exists".format(','.join(conflict))) 88 raise ConflictError("The actions [{}] already exists".format(','.join(conflict)))
88 for action, shortcut in new_actions.iteritems(): 89 for action, shortcut in new_actions.items():
89 self[action] = shortcut 90 self[action] = shortcut
90 91
91 def replace(self, action_shortcuts_map): 92 def replace(self, action_shortcuts_map):
92 """Replace shortcuts with an other dictionary 93 """Replace shortcuts with an other dictionary
93 94
95 @raise ValueError: something else than a dictionary is used 96 @raise ValueError: something else than a dictionary is used
96 @raise KeyError: action doesn't exists 97 @raise KeyError: action doesn't exists
97 """ 98 """
98 if not isinstance(action_shortcuts_map, dict): 99 if not isinstance(action_shortcuts_map, dict):
99 raise ValueError("only dictionary subclasses are accepted for replacing shortcuts") 100 raise ValueError("only dictionary subclasses are accepted for replacing shortcuts")
100 for action, shortcut in action_shortcuts_map.iteritems(): 101 for action, shortcut in action_shortcuts_map.items():
101 self.replace_shortcut(action, shortcut) 102 self.replace_shortcut(action, shortcut)
102 103
103 def set_close_namespaces(self, close_namespaces, always_check=None): 104 def set_close_namespaces(self, close_namespaces, always_check=None):
104 """Set namespaces where conflicting shortcut should not happen 105 """Set namespaces where conflicting shortcut should not happen
105 106