[kupfer: 21/53] Move execfile into kupfer.core and update for GUI context changes



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

    Move execfile into kupfer.core and update for GUI context changes

 kupfer/core/data.py            |   15 +++++++++++++++
 kupfer/{ => core}/execfile.py  |   15 +++++++++++----
 kupfer/plugin/core/commands.py |    2 +-
 kupfer/ui/browser.py           |   27 +++++++++++++++++----------
 4 files changed, 44 insertions(+), 15 deletions(-)
---
diff --git a/kupfer/core/data.py b/kupfer/core/data.py
index e345886..488ee62 100644
--- a/kupfer/core/data.py
+++ b/kupfer/core/data.py
@@ -3,6 +3,7 @@ from __future__ import with_statement
 import itertools
 import operator
 import os
+import sys
 
 import gobject
 gobject.threads_init()
@@ -16,6 +17,7 @@ from kupfer.core import search, learn
 from kupfer.core import settings
 from kupfer.core import qfurl
 from kupfer.core import pluginload
+from kupfer.core import execfile
 
 from kupfer.core.sources import GetSourceController
 
@@ -801,6 +803,19 @@ class DataController (gobject.GObject, pretty.OutputMixin):
 		if res not in commandexec.RESULTS_SYNC:
 			self.emit("launched-action")
 
+	def execute_file(self, filepath, ui_ctx, on_error):
+		try:
+			cmd_objs = execfile.parse_kfcom_file(filepath)
+			ctx = self._execution_context
+			ctx.run(*cmd_objs, ui_ctx=ui_ctx)
+			return True
+		except commandexec.ActionExecutionError:
+			self.output_exc()
+			return
+		except execfile.ExecutionError:
+			on_error(sys.exc_info())
+			return False
+
 	def _insert_object(self, pane, obj):
 		"Insert @obj in @pane: prepare the object, then emit pane-reset"
 		self._decorate_object(obj)
diff --git a/kupfer/execfile.py b/kupfer/core/execfile.py
similarity index 87%
rename from kupfer/execfile.py
rename to kupfer/core/execfile.py
index 1200e00..b0a9dc9 100644
--- a/kupfer/execfile.py
+++ b/kupfer/core/execfile.py
@@ -14,14 +14,16 @@ KUPFER_COMMAND_SHEBANG="#!/usr/bin/env kupfer-exec\n"
 class ExecutionError (Exception):
 	pass
 
-def execute_file(filepath):
-	"""Execute serialized command inside @filepath
+def parse_kfcom_file(filepath):
+	"""Extract the serialized command inside @filepath
 
 	The file must be executable (comparable to a shell script)
 	>>> execute_file(__file__)  # doctest: +ELLIPSIS
 	Traceback (most recent call last):
 	    ...
 	ExecutionError: ... (not executable)
+
+	Return commands triple
 	"""
 	fobj = open(filepath, "rb")
 	if not os.access(filepath, os.X_OK):
@@ -45,8 +47,13 @@ def execute_file(filepath):
 		raise ExecutionError(_('Command in "%s" is not available') %
 				glib.filename_display_basename(filepath))
 
-	command_object.run()
-	glib.idle_add(update_icon, command_object, filepath)
+	try:
+		return tuple(command_object.object)
+	except (AttributeError, TypeError):
+		raise ExecutionError('"%s" is not a saved command' %
+				os.path.basename(filepath))
+	finally:
+		glib.idle_add(update_icon, command_object, filepath)
 
 def save_to_file(command_leaf, filename):
 	fd = os.open(filename, os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0o777)
diff --git a/kupfer/plugin/core/commands.py b/kupfer/plugin/core/commands.py
index 8ad477d..5026fd7 100644
--- a/kupfer/plugin/core/commands.py
+++ b/kupfer/plugin/core/commands.py
@@ -4,7 +4,7 @@ import os
 
 from kupfer.objects import Action, FileLeaf, TextLeaf, TextSource
 from kupfer.obj.compose import ComposedLeaf
-from kupfer import execfile
+from kupfer.core import execfile
 
 
 class SaveToFile (Action):
diff --git a/kupfer/ui/browser.py b/kupfer/ui/browser.py
index 49bf663..569e112 100644
--- a/kupfer/ui/browser.py
+++ b/kupfer/ui/browser.py
@@ -1477,15 +1477,28 @@ class Interface (gobject.GObject):
 		pane = self._pane_for_widget(self.current)
 		self.data_controller.browse_down(pane, alternate=alternate)
 
-	def _activate(self, widget, current):
+	def _make_gui_ctx(self):
 		timestamp = uievents.current_event_time()
-		ctx = uievents.GUIEnvironmentContext(timestamp)
-		self.data_controller.activate(ui_ctx=ctx)
+		return uievents.GUIEnvironmentContext(timestamp)
+
+	def _activate(self, widget, current):
+		self.data_controller.activate(ui_ctx=self._make_gui_ctx())
 
 	def activate(self):
 		"""Activate current selection (Run action)"""
 		self._activate(None, None)
 
+	def execute_file(self, filepath):
+		"""Execute a .kfcom file"""
+		def _handle_error(exc_info):
+			from kupfer import uiutils
+			etype, exc, tb = exc_info
+			if not uiutils.show_notification(unicode(exc), icon_name="kupfer"):
+				raise
+		self.data_controller.execute_file(filepath, self._make_gui_ctx(),
+		                                  on_error=_handle_error)
+
+
 	def _search_result(self, sender, pane, matchrankable, matches, context):
 		# NOTE: "Always-matching" search.
 		# If we receive an empty match, we ignore it, to retain the previous
@@ -1994,13 +2007,7 @@ class WindowController (pretty.OutputMixin):
 		self.interface.put_files(fileuris)
 
 	def _execute_file_received(self, sender, filepath):
-		from kupfer import execfile
-		from kupfer import uiutils
-		try:
-			execfile.execute_file(filepath)
-		except execfile.ExecutionError, exc:
-			if not uiutils.show_notification(unicode(exc)):
-				raise
+		self.interface.execute_file(filepath)
 
 	def _close_window(self, window, event):
 		self.put_away()



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