[kupfer: 8/11] helplib: Add reverse_action to helplib



commit e067aeb3f4f367ad9f454abf69a39b9895a0da46
Author: Ulrik Sverdrup <ulrik sverdrup gmail com>
Date:   Sun Nov 1 18:59:14 2009 +0100

    helplib: Add reverse_action to helplib
    
    reverse_action is a convenience function to reverse a three-part
    action.
    
    This type of "reversal" is not done automatically in Kupfer and I
    don't think it can be done either; some three-part actions should
    never be reversed (like  Document -> Move To.. -> Folder becomes very
    confusing in the form Folder -> Move Something To(?).. -> Document).

 kupfer/helplib.py          |   38 ++++++++++++++++++++++++++++++++++++++
 kupfer/plugin/templates.py |   34 ++++------------------------------
 2 files changed, 42 insertions(+), 30 deletions(-)
---
diff --git a/kupfer/helplib.py b/kupfer/helplib.py
index a472bfa..a8dd12f 100644
--- a/kupfer/helplib.py
+++ b/kupfer/helplib.py
@@ -172,6 +172,44 @@ def gobject_connect_weakly(sender, signal, mcallback, *user_args):
 	"""
 	GobjectWeakCallback._connect(sender, signal, mcallback, *user_args)
 
+def reverse_action(action, rank=0):
+	"""Return a reversed version a three-part action
+
+	@action: the action class
+	@rank: the rank_adjust to give the reversed action
+
+	A three-part action requires a direct object (item) and an indirect
+	object (iobj).
+
+	In general, the item must be from the Catalog, while the iobj can be
+	from one, specified special Source. If this is used, and the action
+	will be reversed, the base action must be the one specifying a
+	source for the iobj. The reversed action will always take both item
+	and iobj from the Catalog, filtered by type.
+
+	If valid_object(iobj, for_leaf=None) is used, it will always be
+	called with only the new item as the first parameter when reversed.
+	"""
+	class ReverseAction (action):
+		rank_adjust = rank
+		def activate(self, leaf, iobj):
+			return action.activate(self, iobj, leaf)
+		def item_types(self):
+			return action.object_types(self)
+		def valid_for_item(self, leaf):
+			try:
+				return leaf.valid_object(leaf)
+			except AttributeError:
+				return True
+		def object_types(self):
+			return action.item_types(self)
+		def valid_object(self, obj, for_item=None):
+			return action.valid_for_item(self, obj)
+		def object_source(self, for_item=None):
+			return None
+	ReverseAction.__name__ = "Reverse" + action.__name__
+	return ReverseAction
+
 if __name__ == '__main__':
 	import doctest
 	doctest.testmod()
diff --git a/kupfer/plugin/templates.py b/kupfer/plugin/templates.py
index 13b0037..14776f9 100644
--- a/kupfer/plugin/templates.py
+++ b/kupfer/plugin/templates.py
@@ -4,8 +4,10 @@ import gio
 import glib
 
 from kupfer.objects import Leaf, Action, Source, FileLeaf
-from kupfer import icons, plugin_support, utils
+from kupfer import icons, utils
+from kupfer import helplib
 from kupfer.helplib import FilesystemWatchMixin, PicklingHelperMixin
+from kupfer import plugin_support
 
 __kupfer_name__ = _("Templates")
 __kupfer_sources__ = ("TemplatesSource", )
@@ -16,34 +18,6 @@ __author__ = "Ulrik Sverdrup <ulrik sverdrup gmail com>"
 
 DEFAULT_TMPL_DIR = "~/Templates"
 
-def _reversed_action(action, name=None, rank=0):
-	"""Return a reversed version a three-part action
-
-	@action: the action class
-	@name: use a different name
-	"""
-	class ReverseAction (action):
-		rank_adjust = rank
-		def __init__(self):
-			Action.__init__(self, name or unicode(action()))
-		def activate(self, leaf, iobj):
-			return action.activate(self, iobj, leaf)
-		def item_types(self):
-			return action.object_types(self)
-		def valid_for_item(self, leaf):
-			try:
-				return leaf.valid_object(leaf)
-			except AttributeError:
-				return True
-		def object_types(self):
-			return action.item_types(self)
-		def valid_object(self, obj, for_item=None):
-			return action.valid_for_item(self, obj)
-		def object_source(self, for_item=None):
-			return None
-	ReverseAction.__name__ = "Reverse" + action.__name__
-	return ReverseAction
-
 class Template (FileLeaf):
 	def __init__(self, path):
 		basename = glib.filename_display_basename(path)
@@ -104,7 +78,7 @@ class CreateNewDocument (Action):
 	def get_icon_name(self):
 		return "document-new"
 
-CreateDocumentIn = _reversed_action(CreateNewDocument, rank=10)
+CreateDocumentIn = helplib.reverse_action(CreateNewDocument, rank=10)
 
 class TemplatesSource (Source, PicklingHelperMixin, FilesystemWatchMixin):
 	def __init__(self):



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