[kupfer: 31/53] Port Triggers to ExecutionToken and wants_context changes



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

    Port Triggers to ExecutionToken and wants_context changes
    
    The trigger callback creates its own ExecutionToken using
    ``commandexec``.

 kupfer/commandexec.py     |    8 +++++++-
 kupfer/plugin/triggers.py |   37 +++++++++++++++++++++++++++----------
 2 files changed, 34 insertions(+), 11 deletions(-)
---
diff --git a/kupfer/commandexec.py b/kupfer/commandexec.py
index 596fb3e..d492f65 100644
--- a/kupfer/commandexec.py
+++ b/kupfer/commandexec.py
@@ -220,6 +220,12 @@ class ActionExecutionContext (gobject.GObject, pretty.OutputMixin):
 		"""
 		return (self.last_command_id, self.last_executed_command)
 
+	def make_execution_token(self, ui_ctx):
+		"""
+		Return an ExecutionToken for @self and @ui_ctx
+		"""
+		return ExecutionToken(self, self.get_async_token(), ui_ctx)
+
 	def operation_error(self, exc_info, cmdtuple):
 		"Error when executing action. Return True when error was handled"
 		if self._is_nested():
@@ -289,7 +295,7 @@ class ActionExecutionContext (gobject.GObject, pretty.OutputMixin):
 			raise ActionExecutionError("%s requires indirect object" % action)
 
 		# The execution token object for the current invocation
-		execution_token = ExecutionToken(self, self.get_async_token(), ui_ctx)
+		execution_token = self.make_execution_token(ui_ctx)
 		with self._error_conversion(obj, action, iobj):
 			with self._nesting():
 				ret = activate_action(execution_token, obj, action, iobj)
diff --git a/kupfer/plugin/triggers.py b/kupfer/plugin/triggers.py
index f5931d3..cfae091 100644
--- a/kupfer/plugin/triggers.py
+++ b/kupfer/plugin/triggers.py
@@ -11,14 +11,17 @@ __author__ = "Ulrik Sverdrup <ulrik sverdrup gmail com>"
 import gtk
 import glib
 
-from kupfer.obj.base import Action, Source, TextSource
-from kupfer.obj.objects import TextLeaf, RunnableLeaf
+from kupfer.objects import Action, Source, TextSource
+from kupfer.objects import TextLeaf, RunnableLeaf
+from kupfer.objects import OperationError
 from kupfer.obj.compose import ComposedLeaf
 from kupfer import puid
 from kupfer import kupferstring
 from kupfer import task
 
 from kupfer.ui import keybindings
+from kupfer.ui import uievents
+from kupfer import commandexec
 from kupfer.ui import getkey_dialog
 
 
@@ -31,8 +34,12 @@ class Trigger (RunnableLeaf):
 		for act in RunnableLeaf.get_actions(self):
 			yield act
 		yield RemoveTrigger()
-	def run(self):
-		return Triggers.perform_trigger(self.object)
+	def wants_context(self):
+		return True
+	def is_valid(self):
+		return Triggers.has_trigger(self.object)
+	def run(self, ctx):
+		return Triggers.perform_trigger(ctx, self.object)
 	def repr_key(self):
 		return self.object
 
@@ -55,7 +62,8 @@ class Triggers (Source):
 	
 	def initialize(self):
 		Triggers.instance = self
-		keybindings.GetKeyboundObject().connect("keybinding", self._callback)
+		keybindings.GetKeyboundObject().connect("keybinding",
+		                                        self.keybinding_callback)
 		for target, (keystr, name, id_) in self.trigger_table.iteritems():
 			keybindings.bind_key(keystr, target)
 		self.output_debug("Loaded triggers, count:", len(self.trigger_table))
@@ -64,8 +72,13 @@ class Triggers (Source):
 		for target, (keystr, name, id_) in self.trigger_table.iteritems():
 			keybindings.bind_key(None, target)
 
-	def _callback(self, keyobj, target, event_time):
-		self.perform_trigger(target)
+	def keybinding_callback(self, keyobj, target, event_time):
+		if not self.has_trigger(target):
+			return
+		ui_ctx = uievents.GUIEnvironmentContext(event_time)
+		ctx = commandexec.DefaultActionExecutionContext()
+		exec_token = ctx.make_execution_token(ui_ctx)
+		self.perform_trigger(exec_token, target)
 
 	def get_items(self):
 		for target, (keystr, name, id_) in self.trigger_table.iteritems():
@@ -79,15 +92,19 @@ class Triggers (Source):
 		yield Trigger
 
 	@classmethod
-	def perform_trigger(cls, target):
+	def has_trigger(cls, target):
+		return target in cls.instance.trigger_table
+
+	@classmethod
+	def perform_trigger(cls, ctx, target):
 		try:
 			keystr, name, id_ = cls.instance.trigger_table[target]
 		except KeyError:
-			return
+			raise OperationError("Trigger '%s' does not exist" % (target, ))
 		obj = puid.resolve_unique_id(id_)
 		if obj is None:
 			return
-		return obj.run()
+		return obj.run(ctx)
 
 	@classmethod
 	def add_trigger(cls, leaf, keystr):



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