about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--ranger/container/commandlist.py28
-rw-r--r--test/tc_commandlist.py54
2 files changed, 76 insertions, 6 deletions
diff --git a/ranger/container/commandlist.py b/ranger/container/commandlist.py
index bc8361ad..eca73f23 100644
--- a/ranger/container/commandlist.py
+++ b/ranger/container/commandlist.py
@@ -41,6 +41,8 @@ class CommandList(object):
 	
 	def __getitem__(self, key):
 		"""Returns the command with the given key combination"""
+		if isinstance(key, str):
+			key = self._str_to_tuple(key)
 		return self.paths[key]
 
 	def rebuild_paths(self):
@@ -53,7 +55,7 @@ class CommandList(object):
 		"""
 		if self.dummies_in_paths:
 			self.remove_dummies()
-		
+
 		for cmd in self.commandlist:
 			for key in cmd.keys:
 				for path in self._keypath(key):
@@ -95,7 +97,7 @@ class CommandList(object):
 		if isinstance(obj, tuple):
 			return obj
 		elif isinstance(obj, str):
-			return tuple(map(ord, list(obj)))
+			return tuple(map(ord, obj))
 		elif isinstance(obj, int):
 			return (obj, )
 		else:
@@ -110,7 +112,7 @@ class CommandList(object):
 		cmd = Command(fnc, keys)
 
 		self.commandlist.append(cmd)
-		for key in keys:
+		for key in cmd.keys:
 			self.paths[key] = cmd
 	
 	def hint(self, text, *keys):
@@ -122,9 +124,23 @@ class CommandList(object):
 		obj = Hint(text, keys)
 
 		self.commandlist.append(obj)
-		for key in keys:
+		for key in obj.keys:
 			self.paths[key] = obj
 
+	def unbind(self, *keys):
+		i = len(self.commandlist)
+		keys = set(map(self._str_to_tuple, keys))
+
+		while i > 0:
+			i -= 1
+			cmd = self.commandlist[i]
+			cmd.keys -= keys
+			if not cmd.keys:
+				del self.commandlist[i]
+
+		for k in keys:
+			del self.paths[k]
+
 	
 class Command(object):
 	"""Command objects store information about a command"""
@@ -132,7 +148,7 @@ class Command(object):
 	keys = []
 
 	def __init__(self, fnc, keys):
-		self.keys = keys
+		self.keys = set(keys)
 		self.execute = fnc
 	
 	def execute(self, *args):
@@ -151,7 +167,7 @@ class Hint(object):
 	text = ''
 
 	def __init__(self, text, keys):
-		self.keys = keys
+		self.keys = set(keys)
 		self.text = text
 
 #	def __str__(self):
diff --git a/test/tc_commandlist.py b/test/tc_commandlist.py
new file mode 100644
index 00000000..ed9b1bd7
--- /dev/null
+++ b/test/tc_commandlist.py
@@ -0,0 +1,54 @@
+if __name__ == '__main__': from __init__ import init; init()
+
+from unittest import TestCase, main
+from ranger.container.commandlist import CommandList as CL
+
+class Test(TestCase):
+	def assertKeyError(self, obj, key):
+		self.assertRaises(KeyError, obj.__getitem__, key)
+
+	def test_commandist(self):
+		cl = CL()
+		fnc = lambda arg: 1
+		dmy = cl.dummy_object
+
+		cl.bind(fnc, 'aaaa')
+		cl.rebuild_paths()
+
+		self.assertEqual(dmy, cl['a'])
+		self.assertEqual(dmy, cl['aa'])
+		self.assertEqual(dmy, cl['aaa'])
+		self.assertEqual(fnc, cl['aaaa'].execute)
+		self.assertKeyError(cl, 'aabb')
+		self.assertKeyError(cl, 'aaaaa')
+
+		cl.bind(fnc, 'aabb')
+		cl.rebuild_paths()
+
+		self.assertEqual(dmy, cl['a'])
+		self.assertEqual(dmy, cl['aa'])
+		self.assertEqual(dmy, cl['aab'])
+		self.assertEqual(fnc, cl['aabb'].execute)
+		self.assertEqual(dmy, cl['aaa'])
+		self.assertEqual(fnc, cl['aaaa'].execute)
+
+		cl.unbind('aabb')
+		cl.rebuild_paths()
+
+		self.assertEqual(dmy, cl['a'])
+		self.assertEqual(dmy, cl['aa'])
+		self.assertKeyError(cl, 'aabb')
+		self.assertKeyError(cl, 'aab')
+		self.assertEqual(dmy, cl['aaa'])
+		self.assertEqual(fnc, cl['aaaa'].execute)
+
+		hint_text = 'some tip blablablba'
+		cl.hint(hint_text, 'aa')
+		cl.rebuild_paths()
+
+		self.assertEqual(dmy, cl['a'])
+		self.assertEqual(hint_text, cl['aa'].text)
+		self.assertEqual(dmy, cl['aaa'])
+		self.assertEqual(fnc, cl['aaaa'].execute)
+
+if __name__ == '__main__': main()