[kupfer] action execution: move ActionExecutionContext to kupfer.commandexec



commit 9e8b1f7f6577acacd2905a4895bc4631e4dbd1f6
Author: Ulrik Sverdrup <ulrik sverdrup gmail com>
Date:   Sat Jan 2 15:20:47 2010 +0100

    action execution: move ActionExecutionContext to kupfer.commandexec

 kupfer/commandexec.py |  105 ++++++++++++++++++++++++++++++++++++++++++++
 kupfer/data.py        |  116 ++----------------------------------------------
 2 files changed, 110 insertions(+), 111 deletions(-)
---
diff --git a/kupfer/commandexec.py b/kupfer/commandexec.py
new file mode 100644
index 0000000..ca30355
--- /dev/null
+++ b/kupfer/commandexec.py
@@ -0,0 +1,105 @@
+from __future__ import with_statement
+
+import contextlib
+
+import gobject
+
+from kupfer import task
+
+RESULT_NONE, RESULT_OBJECT, RESULT_SOURCE, RESULT_ASYNC = (1, 2, 3, 4)
+
+_action_exec_context = None
+def DefaultActionExecutionContext():
+	global _action_exec_context
+	if _action_exec_context is None:
+		_action_exec_context = ActionExecutionContext()
+	return _action_exec_context
+
+class ActionExecutionError (Exception):
+	pass
+
+class ActionExecutionContext (gobject.GObject):
+	"""
+	command-result (result_type, result)
+		Emitted when a command is carried out, with its resulting value
+	"""
+	__gtype_name__ = "ActionExecutionContext"
+	def __init__(self):
+		gobject.GObject.__init__(self)
+		self.task_runner = task.TaskRunner(end_on_finish=False)
+		self._nest_level = 0
+		self._delegate = False
+
+	def check_valid(self, obj, action, iobj):
+		pass
+
+	@contextlib.contextmanager
+	def _nesting(self):
+		try:
+			self._nest_level += 1
+			self._delegate = False
+			yield
+		finally:
+			self._nest_level -= 1
+
+	def _is_nested(self):
+		return self._nest_level
+
+	def run(self, obj, action, iobj, delegate=False):
+		"""
+		Activate the command (obj, action, iobj), where @iobj may be None
+
+		Return a tuple (DESCRIPTION; RESULT)
+
+		If a command carries out another command as part of its execution,
+		and wishes to delegate to it, pass True for @delegate.
+		"""
+		if not action or not obj:
+			raise ActionExecutionError("Primary Object and Action required")
+		if iobj is None and action.requires_object():
+			raise ActionExecutionError("%s requires indirect object" % action)
+
+		with self._nesting():
+			if action.requires_object():
+				ret = action.activate(obj, iobj)
+			else:
+				ret = action.activate(obj)
+
+		# Delegated command execution was previously requested: we take
+		# the result of the nested execution context
+		if self._delegate:
+			res, ret = ret
+			return self._return_result(res, ret)
+
+		def valid_result(ret):
+			return ret and (not hasattr(ret, "is_valid") or ret.is_valid())
+
+		# handle actions returning "new contexts"
+		res = RESULT_NONE
+		if action.is_factory() and valid_result(ret):
+			res = RESULT_SOURCE
+		if action.has_result() and valid_result(ret):
+			res = RESULT_OBJECT
+		elif action.is_async():
+			self.task_runner.add_task(ret)
+			res = RESULT_ASYNC
+
+		# Delegated command execution was requested: we pass
+		# 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.emit("command-result", res, ret)
+		return res, ret
+
+# Action result type, action result
+gobject.signal_new("command-result", ActionExecutionContext,
+		gobject.SIGNAL_RUN_LAST,
+		gobject.TYPE_BOOLEAN, (gobject.TYPE_INT, gobject.TYPE_PYOBJECT))
+
+
diff --git a/kupfer/data.py b/kupfer/data.py
index 3376faa..c4b2fd5 100644
--- a/kupfer/data.py
+++ b/kupfer/data.py
@@ -1,7 +1,3 @@
-from __future__ import with_statement
-
-import contextlib
-
 import gzip
 import hashlib
 import itertools
@@ -17,6 +13,7 @@ gobject.threads_init()
 from . import objects
 from . import search, learn
 from . import config, pretty, scheduler, task
+from kupfer import commandexec
 
 from kupfer import qfurl
 
@@ -755,7 +752,7 @@ 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._execution_context = DefaultActionExecutionContext()
+		self._execution_context = commandexec.DefaultActionExecutionContext()
 		self._execution_context.connect("command-result",
 				self._command_execution_result)
 
@@ -1052,7 +1049,7 @@ class DataController (gobject.GObject, pretty.OutputMixin):
 		if not sobject and self.mode is SourceActionObjectMode:
 			self.output_info("There is no third object!")
 			return
-		ctx = DefaultActionExecutionContext()
+		ctx = self._execution_context
 		ctx.run(leaf, action, sobject)
 
 		# register search to learning database
@@ -1062,12 +1059,10 @@ class DataController (gobject.GObject, pretty.OutputMixin):
 			learn.record_search_hit(sobject, self.object_pane.get_latest_key())
 
 	def _command_execution_result(self, ctx, result_type, ret):
-		if result_type == RESULT_SOURCE:
+		if result_type == commandexec.RESULT_SOURCE:
 			self.source_pane.push_source(ret)
-			return
-		if result_type == RESULT_OBJECT:
+		if result_type == commandexec.RESULT_OBJECT:
 			self.emit("pane-reset", SourcePane, search.wrap_rankable(ret))
-			return
 		self.emit("launched-action")
 
 	def find_object(self, url):
@@ -1113,104 +1108,3 @@ gobject.signal_new("launched-action", DataController, gobject.SIGNAL_RUN_LAST,
 		gobject.TYPE_BOOLEAN, ())
 
 
-RESULT_NONE, RESULT_OBJECT, RESULT_SOURCE, RESULT_ASYNC = (1, 2, 3, 4)
-
-_action_exec_context = None
-def DefaultActionExecutionContext():
-	global _action_exec_context
-	if _action_exec_context is None:
-		_action_exec_context = ActionExecutionContext()
-	return _action_exec_context
-
-class ActionExecutionError (Exception):
-	pass
-
-class ActionExecutionContext (gobject.GObject):
-	"""
-	command-result (result_type, result)
-		Emitted when a command is carried out, with its resulting value
-	"""
-	__gtype_name__ = "ActionExecutionContext"
-	def __init__(self):
-		gobject.GObject.__init__(self)
-		self.task_runner = task.TaskRunner(end_on_finish=False)
-		self._nest_level = 0
-		self._delegate = False
-
-	def check_valid(self, obj, action, iobj):
-		pass
-
-	@contextlib.contextmanager
-	def _nesting(self):
-		try:
-			self._nest_level += 1
-			self._delegate = False
-			yield
-		finally:
-			self._nest_level -= 1
-
-	def _is_nested(self):
-		return self._nest_level
-
-	def run(self, obj, action, iobj, delegate=False):
-		"""
-		Activate the command (obj, action, iobj), where @iobj may be None
-
-		Return a tuple (DESCRIPTION; RESULT)
-
-		If a command carries out another command as part of its execution,
-		and wishes to delegate to it, pass True for @delegate.
-		"""
-		if action.requires_object():
-			mode = SourceActionObjectMode
-		else:
-			mode = SourceActionMode
-		if not action or not obj:
-			raise ActionExecutionError("Primary Object and Action required")
-		if iobj is None and mode == SourceActionObjectMode:
-			raise ActionExecutionError("%s requires indirect object" % action)
-
-		with self._nesting():
-			if mode is SourceActionMode:
-				ret = action.activate(obj)
-			elif mode is SourceActionObjectMode:
-				ret = action.activate(obj, iobj)
-
-		# Delegated command execution was previously requested: we take
-		# the result of the nested execution context
-		if self._delegate:
-			res, ret = ret
-			return self._return_result(res, ret)
-
-		def valid_result(ret):
-			return ret and (not hasattr(ret, "is_valid") or ret.is_valid())
-
-		# handle actions returning "new contexts"
-		res = RESULT_NONE
-		if action.is_factory() and valid_result(ret):
-			res = RESULT_SOURCE
-		if action.has_result() and valid_result(ret):
-			res = RESULT_OBJECT
-		elif action.is_async():
-			self.task_runner.add_task(ret)
-			res = RESULT_ASYNC
-
-		# Delegated command execution was requested: we pass
-		# 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.emit("command-result", res, ret)
-		return res, ret
-
-# Action result type, action result
-gobject.signal_new("command-result", ActionExecutionContext,
-		gobject.SIGNAL_RUN_LAST,
-		gobject.TYPE_BOOLEAN, (gobject.TYPE_INT, gobject.TYPE_PYOBJECT))
-
-



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