[kupfer] plugin.urlactions: Add DownloadTo



commit 8f968b544c23156a5800ec0bd2dbeb6aacce8170
Author: Ulrik Sverdrup <ulrik sverdrup gmail com>
Date:   Fri Aug 21 23:42:06 2009 +0200

    plugin.urlactions: Add DownloadTo

 kupfer/plugin/urlactions.py |   61 +++++++++++++++++++++++++++++++++++++++---
 1 files changed, 56 insertions(+), 5 deletions(-)
---
diff --git a/kupfer/plugin/urlactions.py b/kupfer/plugin/urlactions.py
index 4e669fa..564c3e3 100644
--- a/kupfer/plugin/urlactions.py
+++ b/kupfer/plugin/urlactions.py
@@ -1,15 +1,40 @@
-from kupfer.objects import Action, Source, UrlLeaf
-from kupfer import utils
+import os
+import urllib2
+
+from kupfer.objects import Action, Source, UrlLeaf, FileLeaf
+from kupfer import utils, pretty
 
 __kupfer_name__ = _("URL Actions")
 __kupfer_sources__ = ()
 __kupfer_text_sources__ = ()
 __kupfer_actions__ = (
+		"DownloadAndOpen",
+		"DownloadTo",
 	)
-__description__ = _("Actions on URLs")
+__description__ = _("URL Actions")
 __version__ = ""
 __author__ = "Ulrik Sverdrup <ulrik sverdrup gmail com>"
 
+def _download_uri(uri, destdir):
+	"""
+	Download @uri to directory @destdir
+	URI downloading may raise (IOError, EnvironmentError);
+	these are not handled here.
+	"""
+	import shutil
+
+	response = urllib2.urlopen(uri)
+	
+	header_basename = response.headers.get('Content-Disposition')
+	destname = header_basename or os.path.basename(response.url)
+	destpath = utils.get_destpath_in_directory(destdir, destname)
+	destfile = open(destpath, "wb")
+	try:
+		shutil.copyfileobj(response, destfile)
+	finally:
+		response.close()
+		destfile.close()
+
 class DownloadAndOpen (Action):
 	"""Asynchronous action to download file and open it"""
 	def __init__(self):
@@ -31,8 +56,34 @@ class DownloadAndOpen (Action):
 
 	def item_types(self):
 		yield UrlLeaf
-	def valid_for_item(self, item):
-		return True
 	def get_description(self):
 		return None
 
+class DownloadTo (Action):
+	def __init__(self):
+		Action.__init__(self, _("Download To..."))
+
+	def is_async(self):
+		return True
+	def activate(self, leaf, obj):
+		return self._start_action, self._finish_action
+
+	def _start_action(self, leaf, iobj=None):
+		uri = leaf.object
+		destdir = iobj.object
+		_download_uri(uri, destdir)
+
+	def _finish_action(self, ret):
+		pass
+
+	def item_types(self):
+		yield UrlLeaf
+	def requires_object(self):
+		return True
+	def object_types(self):
+		yield FileLeaf
+	def valid_object(self, obj, for_item=None):
+		return utils.is_directory_writable(obj.object)
+	def get_description(self):
+		return _("Download URL to a chosen location")
+



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