[kupfer] Implement Late command results



commit 8765a28303ba7704626c82f5762e58b7df3d9080
Author: Ulrik Sverdrup <ulrik sverdrup gmail com>
Date:   Mon Mar 29 17:05:20 2010 +0200

    Implement Late command results
    
    Show late results directly if there was no interaction between
    initiation of the result's action and the time when the result was
    received.
    
    Latest results (all) are showin in Command Results

 kupfer/commandexec.py          |   33 ++++++++++++++++++++++++++++++---
 kupfer/core/data.py            |    9 +++++++++
 kupfer/plugin/core/internal.py |   14 +++++++++++++-
 3 files changed, 52 insertions(+), 4 deletions(-)
---
diff --git a/kupfer/commandexec.py b/kupfer/commandexec.py
index ebed1da..ab9fca0 100644
--- a/kupfer/commandexec.py
+++ b/kupfer/commandexec.py
@@ -25,7 +25,9 @@ merge multiple return values.
 """
 from __future__ import with_statement
 
+import collections
 import contextlib
+import itertools
 import sys
 
 import gobject
@@ -41,6 +43,8 @@ from kupfer.obj.compose import MultipleLeaf
 RESULT_NONE, RESULT_OBJECT, RESULT_SOURCE, RESULT_ASYNC = (1, 2, 3, 4)
 RESULTS_SYNC = (RESULT_OBJECT, RESULT_SOURCE)
 
+_MAX_LAST_RESULTS = 10
+
 _action_exec_context = None
 def DefaultActionExecutionContext():
 	global _action_exec_context
@@ -136,6 +140,7 @@ class ActionExecutionContext (gobject.GObject, pretty.OutputMixin):
 		self.last_command_id = -1
 		self.last_command = None
 		self.last_executed_command = None
+		self.last_results = collections.deque([], _MAX_LAST_RESULTS)
 
 	def check_valid(self, obj, action, iobj):
 		pass
@@ -193,8 +198,25 @@ class ActionExecutionContext (gobject.GObject, pretty.OutputMixin):
 		self._do_error_conversion(cmdtuple, exc_info)
 
 	def register_late_result(self, token, result):
-		"Register a late result (as in result object, not factory or async)"
-		self.output_info("Late result", repr(result))
+		"Register a late result (as in result Leaf, not factory or async)"
+		self.output_info("Late result", repr(result), "for", token)
+		command_id, (_ign1, action, _ign2) = token
+		if result is None:
+			raise ActionExecutionError("Late result from %s was None" % action)
+		res_name = unicode(result)
+		res_desc = result.get_description()
+		if res_desc:
+			description = "%s (%s)" % (res_name, res_desc)
+		else:
+			description = res_name
+		uiutils.show_notification(_('"%s" produced a result') % action,
+				description)
+		self.emit("late-command-result", command_id, RESULT_OBJECT, result)
+		self._append_result(RESULT_OBJECT, result)
+
+	def _append_result(self, res_type, result):
+		if res_type == RESULT_OBJECT:
+			self.last_results.append(result)
 
 	def run(self, obj, action, iobj, delegate=False):
 		"""
@@ -238,12 +260,12 @@ class ActionExecutionContext (gobject.GObject, pretty.OutputMixin):
 		# through the result of the action to the parent execution context
 		if delegate and self._is_nested():
 			self._delegate = True
-			return (res, ret)
 
 		return self._return_result(res, ret)
 
 	def _return_result(self, res, ret):
 		if not self._is_nested():
+			self._append_result(res, ret)
 			self.emit("command-result", res, ret)
 		return res, ret
 
@@ -308,3 +330,8 @@ gobject.signal_new("command-result", ActionExecutionContext,
 		gobject.SIGNAL_RUN_LAST,
 		gobject.TYPE_BOOLEAN, (gobject.TYPE_INT, gobject.TYPE_PYOBJECT))
 
+# Command ID, Action result type, action result
+gobject.signal_new("late-command-result", ActionExecutionContext,
+		gobject.SIGNAL_RUN_LAST,
+		gobject.TYPE_BOOLEAN, (gobject.TYPE_INT, gobject.gobject.TYPE_INT,
+			gobject.TYPE_PYOBJECT))
diff --git a/kupfer/core/data.py b/kupfer/core/data.py
index 93b3bf7..963a369 100644
--- a/kupfer/core/data.py
+++ b/kupfer/core/data.py
@@ -426,9 +426,12 @@ class DataController (gobject.GObject, pretty.OutputMixin):
 			ctl.connect("search-result", self._pane_search_result, pane)
 		self.mode = None
 		self._search_ids = itertools.count(1)
+		self._latest_interaction = -1
 		self._execution_context = commandexec.DefaultActionExecutionContext()
 		self._execution_context.connect("command-result",
 				self._command_execution_result)
+		self._execution_context.connect("late-command-result",
+				self._late_command_execution_result)
 
 		sch = scheduler.GetScheduler()
 		sch.connect("load", self._load)
@@ -636,6 +639,7 @@ class DataController (gobject.GObject, pretty.OutputMixin):
 		"""
 
 		self.cancel_search(pane)
+		self._latest_interaction = self._execution_context.last_command_id
 		ctl = self._panectl_table[pane]
 		ctl.outstanding_search_id = self._search_ids.next()
 		wrapcontext = (ctl.outstanding_search_id, context)
@@ -790,6 +794,11 @@ class DataController (gobject.GObject, pretty.OutputMixin):
 			return
 		self.emit("command-result", result_type)
 
+	def _late_command_execution_result(self, ctx, id_, result_type, ret):
+		"Receive late command result"
+		if self._latest_interaction < id_:
+			self._command_execution_result(ctx, result_type, ret)
+
 	def find_object(self, url):
 		"""Find object with URI @url and select it in the first pane"""
 		sc = GetSourceController()
diff --git a/kupfer/plugin/core/internal.py b/kupfer/plugin/core/internal.py
index 8b9ef73..3d51e3b 100644
--- a/kupfer/plugin/core/internal.py
+++ b/kupfer/plugin/core/internal.py
@@ -3,7 +3,7 @@ from kupfer.objects import Source
 from kupfer.objects import RunnableLeaf
 from kupfer import commandexec
 
-__kupfer_sources__ = ("KupferInterals", )
+__kupfer_sources__ = ("KupferInterals", "CommandResults", )
 __author__ = "Ulrik Sverdrup <ulrik sverdrup gmail com>"
 
 class LastCommand (RunnableLeaf):
@@ -29,3 +29,15 @@ class KupferInterals (Source):
 		yield LastCommand(ctx.last_command)
 	def provides(self):
 		yield LastCommand
+
+class CommandResults (Source):
+	def __init__(self):
+		Source.__init__(self, _("Command Results"))
+	def is_dynamic(self):
+		return True
+	def get_items(self):
+		ctx = commandexec.DefaultActionExecutionContext()
+		for x in reversed(ctx.last_results):
+			yield x
+	def provides(self):
+		return ()



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