[kupfer: 29/38] Split out part of commandexec into actioncompat



commit 953f2f6b1b5769409703988126a226f0d15bb2c6
Author: Ulrik Sverdrup <ulrik sverdrup gmail com>
Date:   Sat Jan 16 02:51:12 2010 +0100

    Split out part of commandexec into actioncompat
    
    actioncompat -- action compatibility, the module that takes care of
    which objects and actions can work together. This is a core module
    that is split away from the shared module commandexec.

 kupfer/commandexec.py       |   73 ++++++++-----------------------------------
 kupfer/core/actioncompat.py |   58 ++++++++++++++++++++++++++++++++++
 kupfer/core/data.py         |    9 +++--
 kupfer/puid.py              |    4 +-
 4 files changed, 78 insertions(+), 66 deletions(-)
---
diff --git a/kupfer/commandexec.py b/kupfer/commandexec.py
index 9fb0fb4..dcf8520 100644
--- a/kupfer/commandexec.py
+++ b/kupfer/commandexec.py
@@ -19,6 +19,19 @@ def DefaultActionExecutionContext():
 class ActionExecutionError (Exception):
 	pass
 
+def _get_leaf_members(leaf):
+	"""
+	Return an iterator to members of @leaf, if it is a multiple leaf
+	"""
+	# NOTE : This function duplicates one in core/actionlogic.py
+	try:
+		return leaf.get_multiple_leaf_representation()
+	except AttributeError:
+		return (leaf, )
+
+def _is_multiple(leaf):
+	return hasattr(leaf, "get_multiple_leaf_representation")
+
 def activate_action(obj, action, iobj):
 	""" Activate @action in simplest manner """
 	if not _is_multiple(obj) and not _is_multiple(iobj):
@@ -141,63 +154,3 @@ gobject.signal_new("command-result", ActionExecutionContext,
 		gobject.SIGNAL_RUN_LAST,
 		gobject.TYPE_BOOLEAN, (gobject.TYPE_INT, gobject.TYPE_PYOBJECT))
 
-def _is_multiple(leaf):
-	return hasattr(leaf, "get_multiple_leaf_representation")
-
-def _get_leaf_members(leaf):
-	"""
-	Return an iterator to members of @leaf, if it is a multiple leaf
-	"""
-	try:
-		return leaf.get_multiple_leaf_representation()
-	except AttributeError:
-		return (leaf, )
-
-def action_valid_for_item(action, leaf):
-	return all(action.valid_for_item(L) for L in _get_leaf_members(leaf))
-
-def actions_for_item(leaf, sourcecontroller):
-	if leaf is None:
-		return []
-	actions = None
-	for L in _get_leaf_members(leaf):
-		l_actions = set(L.get_actions())
-		l_actions.update(sourcecontroller.get_actions_for_leaf(L))
-		if actions is None:
-			actions = l_actions
-		else:
-			actions.intersection_update(l_actions)
-	return actions
-
-def iobject_source_for_action(action, for_item):
-	for leaf in _get_leaf_members(for_item):
-		return action.object_source(leaf)
-
-def iobjects_valid_for_action(action, for_item):
-	"""
-	Return a filtering *function* that will let through
-	those leaves that are good iobjects for @action and @for_item.
-	"""
-	def valid_object(leaf, for_item):
-		_valid_object = action.valid_object
-		for L in _get_leaf_members(leaf):
-			for I in _get_leaf_members(for_item):
-				if not _valid_object(L, for_item=I):
-					return False
-		return True
-
-	types = tuple(action.object_types())
-	def type_obj_check(iobjs):
-		for i in iobjs:
-			if (isinstance(i, types) and valid_object(i, for_item=for_item)):
-				yield i
-	def type_check(itms):
-		for i in itms:
-			if isinstance(i, types):
-				yield i
-
-	if hasattr(action, "valid_object"):
-		return type_obj_check
-	else:
-		return type_check
-
diff --git a/kupfer/core/actioncompat.py b/kupfer/core/actioncompat.py
new file mode 100644
index 0000000..51fd7a3
--- /dev/null
+++ b/kupfer/core/actioncompat.py
@@ -0,0 +1,58 @@
+
+def _get_leaf_members(leaf):
+	"""
+	Return an iterator to members of @leaf, if it is a multiple leaf
+	"""
+	try:
+		return leaf.get_multiple_leaf_representation()
+	except AttributeError:
+		return (leaf, )
+
+def action_valid_for_item(action, leaf):
+	return all(action.valid_for_item(L) for L in _get_leaf_members(leaf))
+
+def actions_for_item(leaf, sourcecontroller):
+	if leaf is None:
+		return []
+	actions = None
+	for L in _get_leaf_members(leaf):
+		l_actions = set(L.get_actions())
+		l_actions.update(sourcecontroller.get_actions_for_leaf(L))
+		if actions is None:
+			actions = l_actions
+		else:
+			actions.intersection_update(l_actions)
+	return actions
+
+def iobject_source_for_action(action, for_item):
+	for leaf in _get_leaf_members(for_item):
+		return action.object_source(leaf)
+
+def iobjects_valid_for_action(action, for_item):
+	"""
+	Return a filtering *function* that will let through
+	those leaves that are good iobjects for @action and @for_item.
+	"""
+	def valid_object(leaf, for_item):
+		_valid_object = action.valid_object
+		for L in _get_leaf_members(leaf):
+			for I in _get_leaf_members(for_item):
+				if not _valid_object(L, for_item=I):
+					return False
+		return True
+
+	types = tuple(action.object_types())
+	def type_obj_check(iobjs):
+		for i in iobjs:
+			if (isinstance(i, types) and valid_object(i, for_item=for_item)):
+				yield i
+	def type_check(itms):
+		for i in itms:
+			if isinstance(i, types):
+				yield i
+
+	if hasattr(action, "valid_object"):
+		return type_obj_check
+	else:
+		return type_check
+
diff --git a/kupfer/core/data.py b/kupfer/core/data.py
index 7d884f9..90a4737 100644
--- a/kupfer/core/data.py
+++ b/kupfer/core/data.py
@@ -7,6 +7,7 @@ import gobject
 from kupfer.obj import base, sources, compose
 from kupfer import pretty, scheduler
 from kupfer import commandexec
+from kupfer.core import actioncompat
 from kupfer import datatools
 from kupfer.core import search, learn
 from kupfer.core import settings
@@ -313,14 +314,14 @@ class PrimaryActionPane (Pane):
 
 		self.latest_key = key
 		leaf = self.current_item
-		actions = commandexec.actions_for_item(leaf, GetSourceController())
+		actions = actioncompat.actions_for_item(leaf, GetSourceController())
 
 		def is_valid_cached(action):
 			"""Check if @action is valid for current item"""
 			cache = self._action_valid_cache
 			valid = cache.get(action)
 			if valid is None:
-				valid = commandexec.action_valid_for_item(action, leaf)
+				valid = actioncompat.action_valid_for_item(action, leaf)
 				cache[action] = valid
 			return valid
 
@@ -349,7 +350,7 @@ class SecondaryObjectPane (LeafPane):
 		self.current_item = item
 		self.current_action = act
 		if item and act:
-			ownsrc = commandexec.iobject_source_for_action(act, item)
+			ownsrc = actioncompat.iobject_source_for_action(act, item)
 			if ownsrc:
 				self.source_rebase(ownsrc)
 			else:
@@ -381,7 +382,7 @@ class SecondaryObjectPane (LeafPane):
 			textsrcs = sc.get_text_sources()
 			sources.extend(textsrcs)
 
-		item_check = commandexec.iobjects_valid_for_action(self.current_action,
+		item_check = actioncompat.iobjects_valid_for_action(self.current_action,
 				self.current_item)
 		decorator = lambda seq: dress_leaves(seq, action=self.current_action)
 
diff --git a/kupfer/puid.py b/kupfer/puid.py
index db17b62..ffbefb3 100644
--- a/kupfer/puid.py
+++ b/kupfer/puid.py
@@ -13,7 +13,7 @@ except ImportError:
 
 from kupfer import pretty
 
-from kupfer import commandexec
+from kupfer.core import actioncompat
 from kupfer.core import qfurl
 from kupfer.core.sources import GetSourceController
 
@@ -115,7 +115,7 @@ def resolve_action_id(puid, for_item=None):
 	get_action_id = repr
 	sc = GetSourceController()
 	if for_item is not None:
-		for action in commandexec.actions_for_item(for_item, sc):
+		for action in actioncompat.actions_for_item(for_item, sc):
 			if get_unique_id(action) == puid:
 				return action
 	for item_type, actions in sc.action_decorators.iteritems():



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]