Add option to GlibURLDownloader to download to specified file instead of tempfile

This commit is contained in:
Dan Williams 2007-07-23 01:17:21 -04:00
parent 98aef7d74f
commit 62e424628e

View File

@ -26,6 +26,7 @@ import sys
import httplib import httplib
import urllib import urllib
import fcntl import fcntl
import tempfile
import gobject import gobject
import SimpleXMLRPCServer import SimpleXMLRPCServer
@ -183,23 +184,29 @@ class GlibURLDownloader(gobject.GObject):
def __init__(self, url, destdir=None): def __init__(self, url, destdir=None):
self._url = url self._url = url
if not destdir: if not destdir:
destdir = "/tmp" destdir = tempfile.gettempdir()
self._destdir = destdir self._destdir = destdir
self._srcid = 0 self._srcid = 0
self._fname = None self._fname = None
self._outf = None self._outf = None
gobject.GObject.__init__(self) gobject.GObject.__init__(self)
def start(self): def start(self, destfile=None):
self._info = urllib.urlopen(self._url) self._info = urllib.urlopen(self._url)
self._suggested_fname = self._get_filename_from_headers(self._info.headers) self._outf = None
import tempfile self._fname = None
garbage, path = urllib.splittype(self._url) if destfile:
garbage, path = urllib.splithost(path or "") self._suggested_fname = os.path.basename(destfile)
path, garbage = urllib.splitquery(path or "") self._fname = os.path.abspath(os.path.expanduser(destfile))
path, garbage = urllib.splitattr(path or "") self._outf = os.open(self._fname, os.O_RDWR | os.O_TRUNC | os.O_CREAT, 0644)
suffix = os.path.splitext(path)[1] else:
(self._outf, self._fname) = tempfile.mkstemp(suffix=suffix, dir=self._destdir) self._suggested_fname = self._get_filename_from_headers(self._info.headers)
garbage, path = urllib.splittype(self._url)
garbage, path = urllib.splithost(path or "")
path, garbage = urllib.splitquery(path or "")
path, garbage = urllib.splitattr(path or "")
suffix = os.path.splitext(path)[1]
(self._outf, self._fname) = tempfile.mkstemp(suffix=suffix, dir=self._destdir)
fcntl.fcntl(self._info.fp.fileno(), fcntl.F_SETFD, os.O_NDELAY) fcntl.fcntl(self._info.fp.fileno(), fcntl.F_SETFD, os.O_NDELAY)
self._srcid = gobject.io_add_watch(self._info.fp.fileno(), self._srcid = gobject.io_add_watch(self._info.fp.fileno(),