[kupfer: 7/53] obj: Port RunnableLeaf and Composed objects to wants_context



commit c3b3ea3d7fa3866f8ce7fe37affeec65eb674d90
Author: Ulrik Sverdrup <ulrik sverdrup gmail com>
Date:   Thu Mar 24 17:22:33 2011 +0100

    obj: Port RunnableLeaf and Composed objects to wants_context
    
    Introduce wants_context on RunnableLeaves as well, so that they get
    the argument ``.run(ctx)``.

 kupfer/obj/compose.py |   14 ++++++++------
 kupfer/obj/objects.py |   16 +++++++++++++---
 2 files changed, 21 insertions(+), 9 deletions(-)
---
diff --git a/kupfer/obj/compose.py b/kupfer/obj/compose.py
index 50db552..9e20cd5 100644
--- a/kupfer/obj/compose.py
+++ b/kupfer/obj/compose.py
@@ -14,13 +14,14 @@ class TimedPerform (Perform):
 	def __init__(self):
 		Action.__init__(self, _("Run after Delay..."))
 
-	def activate(self, leaf, iobj=None):
+	def activate(self, leaf, iobj, ctx):
 		from kupfer import scheduler
 		# make a timer that will fire when Kupfer exits
 		interval = utils.parse_time_interval(iobj.object)
 		pretty.print_debug(__name__, "Run %s in %s seconds" % (leaf, interval))
 		timer = scheduler.Timer(True)
-		timer.set(interval, leaf.run)
+		args = (ctx,) if leaf.wants_context() else ()
+		timer.set(interval, leaf.run, *args)
 
 	def requires_object(self):
 		return True
@@ -65,11 +66,12 @@ class ComposedLeaf (RunnableLeaf):
 	def repr_key(self):
 		return self
 
-	def run(self):
-		from kupfer import commandexec
-		ctx = commandexec.DefaultActionExecutionContext()
+	def wants_context(self):
+		return True
+
+	def run(self, ctx):
 		obj, action, iobj = self.object
-		return ctx.run(obj, action, iobj, delegate=True)
+		return ctx.delegated_run(obj, action, iobj)
 
 	def get_gicon(self):
 		obj, action, iobj = self.object
diff --git a/kupfer/obj/objects.py b/kupfer/obj/objects.py
index 8f3b66f..36ecda8 100644
--- a/kupfer/obj/objects.py
+++ b/kupfer/obj/objects.py
@@ -363,8 +363,13 @@ class RunnableLeaf (Leaf):
 		Leaf.__init__(self, obj, name)
 	def get_actions(self):
 		yield Perform()
-	def run(self):
+	def run(self, ctx=None):
 		raise NotImplementedError
+	def wants_context(self):
+		""" Return ``True`` if you want the actions' execution
+		context passed as ctx= in RunnableLeaf.run
+		"""
+		return False
 	def repr_key(self):
 		return ""
 	def get_gicon(self):
@@ -382,8 +387,13 @@ class Perform (Action):
 		# TRANS: 'Run' as in Perform a (saved) command
 		if not name: name = _("Run")
 		super(Perform, self).__init__(name=name)
-	def activate(self, leaf):
-		return leaf.run()
+	def wants_context(self):
+		return True
+	def activate(self, leaf, ctx):
+		if leaf.wants_context():
+			return leaf.run(ctx)
+		else:
+			return leaf.run()
 	def get_description(self):
 		return _("Perform command")
 



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