[kupfer: 17/53] Port runtimehelper and image, archivemanager to wants_context



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

    Port runtimehelper and image, archivemanager to wants_context

 kupfer/plugin/archivemanager.py |   16 ++++++++++------
 kupfer/plugin/image.py          |   14 ++++++++++----
 kupfer/runtimehelper.py         |   19 +++++++++----------
 3 files changed, 29 insertions(+), 20 deletions(-)
---
diff --git a/kupfer/plugin/archivemanager.py b/kupfer/plugin/archivemanager.py
index a87ebb3..b81e962 100644
--- a/kupfer/plugin/archivemanager.py
+++ b/kupfer/plugin/archivemanager.py
@@ -92,28 +92,32 @@ class CreateArchiveIn (Action):
 		Action.__init__(self, _("Create Archive In..."))
 
 	@classmethod
-	def _make_archive(cls, basename, dirpath, filepaths):
+	def _make_archive(cls, ctx, basename, dirpath, filepaths):
 		archive_type = __kupfer_settings__["archive_type"]
 		archive_path = \
 			utils.get_destpath_in_directory(dirpath, basename, archive_type)
 		cmd = ["file-roller", "--add-to=%s" % (archive_path, )]
 		cmd.extend(filepaths)
-		runtimehelper.register_async_file_result(archive_path)
+		runtimehelper.register_async_file_result(ctx, archive_path)
 		utils.spawn_async_notify_as("file-roller.desktop", cmd)
 		return archive_path
 
-	def activate(self, leaf, iobj):
+	def wants_context(self):
+		return True
+
+	def activate(self, leaf, iobj, ctx):
 		archive_type = __kupfer_settings__["archive_type"]
 		dirpath = iobj.object
 		basename = os_path.basename(leaf.object)
-		self._make_archive(basename, dirpath, (leaf.object, ))
+		self._make_archive(ctx, basename, dirpath, (leaf.object, ))
 
-	def activate_multiple(self, objs, iobjs):
+	def activate_multiple(self, objs, iobjs, ctx):
 		archive_type = __kupfer_settings__["archive_type"]
 		# TRANS: Default filename (no extension) for 'Create Archive In...'
 		basename = _("Archive")
 		for iobj in iobjs:
-			self._make_archive(basename, iobj.object, [L.object for L in objs])
+			self._make_archive(ctx, basename, iobj.object,
+			                   [L.object for L in objs])
 
 	def item_types(self):
 		yield FileLeaf
diff --git a/kupfer/plugin/image.py b/kupfer/plugin/image.py
index 731aeeb..a9c5650 100644
--- a/kupfer/plugin/image.py
+++ b/kupfer/plugin/image.py
@@ -28,7 +28,10 @@ class Scale (Action):
 	def has_result(self):
 		return True
 
-	def activate(self, leaf, obj):
+	def wants_context(self):
+		return True
+
+	def activate(self, leaf, obj, ctx):
 		size = self._make_size(obj.object)
 		fpath = leaf.object
 		dirname = os_path.dirname(fpath)
@@ -36,7 +39,7 @@ class Scale (Action):
 		filename = "%s_%s%s" % (head, size, ext)
 		dpath = utils.get_destpath_in_directory(dirname, filename)
 		argv = ["convert", "-scale", ('%s' % size),  fpath, dpath]
-		runtimehelper.register_async_file_result(dpath)
+		runtimehelper.register_async_file_result(ctx, dpath)
 		utils.spawn_async(argv)
 		return FileLeaf(dpath)
 
@@ -82,7 +85,10 @@ class RotateBase (Action):
 	def has_result(self):
 		return True
 
-	def activate(self, leaf, obj=None):
+	def wants_context(self):
+		return True
+
+	def activate(self, leaf, ctx):
 		fpath = leaf.object
 		dirname = os_path.dirname(fpath)
 		head, ext = os_path.splitext(os_path.basename(fpath))
@@ -90,7 +96,7 @@ class RotateBase (Action):
 		dpath = utils.get_destpath_in_directory(dirname, filename)
 		argv = ["jpegtran", "-copy", "all", "-rotate", self.rotation, "-outfile",
 		        dpath, fpath]
-		runtimehelper.register_async_file_result(dpath)
+		runtimehelper.register_async_file_result(ctx, dpath)
 		utils.spawn_async(argv)
 		return FileLeaf(dpath)
 
diff --git a/kupfer/runtimehelper.py b/kupfer/runtimehelper.py
index c51532e..f3997ca 100644
--- a/kupfer/runtimehelper.py
+++ b/kupfer/runtimehelper.py
@@ -1,28 +1,27 @@
 import gio
 
 from kupfer.objects import FileLeaf
-from kupfer import commandexec
 
-def register_async_file_result(filepath):
-	"This function may only be called inside command execution"
-	ctx = commandexec.DefaultActionExecutionContext()
-	return AsyncFileResult(ctx.get_async_token(), filepath)
+def register_async_file_result(ctx, filepath):
+	"""
+	Register that @filepath may appear soon
+	@ctx: The action's execution context token
+	"""
+	return AsyncFileResult(ctx, filepath)
 
 class AsyncFileResult (object):
 	"""Expect a given file path to be created, and when (probably) done,
 	post the file as a late result.
 	"""
-	def __init__(self, async_token, filepath):
-		self.async_token = async_token
+	def __init__(self, ctx, filepath):
+		self.ctx = ctx
 		gfile = gio.File(filepath)
 		self.monitor = gfile.monitor_file(gio.FILE_MONITOR_NONE)
 		self.callback_id = self.monitor.connect("changed", self.changed)
 
 	def changed(self, monitor, gfile1, gfile2, event):
 		if event == gio.FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
-			ctx = commandexec.DefaultActionExecutionContext()
-			ctx.register_late_result(self.async_token,
-					FileLeaf(gfile1.get_path()))
+			self.ctx.register_late_result(FileLeaf(gfile1.get_path()))
 			self.monitor.disconnect(self.callback_id)
 			self.monitor = None
 



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