return self.data
class FileHandle:
+ """Use me to download data to a pre-defined filehandle-like object. I
+ will use the target's write() method. I will *not* close the filehandle:
+ I leave that up to the originator of the filehandle. The download process
+ will return the filehandle when it completes.
+ """
implements(IDownloadTarget)
def __init__(self, filehandle):
self._filehandle = filehandle
def register_canceller(self, cb):
pass
def finish(self):
- pass
+ return self._filehandle
class Downloader(service.MultiService):
"""I am a service that allows file downloading.
d.addCallback(_do_upload)
def _upload_done(uri):
log.msg("upload finished: uri is %s" % (uri,))
+ self.uri = uri
dl = self.clients[1].getServiceNamed("downloader")
+ self.downloader = dl
d1 = dl.download_to_data(uri)
return d1
d.addCallback(_upload_done)
log.msg("download finished")
self.failUnlessEqual(data, DATA)
d.addCallback(_download_done)
+
+ target_filename = os.path.join(self.basedir, "download.target")
+ def _download_to_filename(res):
+ return self.downloader.download_to_filename(self.uri,
+ target_filename)
+ d.addCallback(_download_to_filename)
+ def _download_to_filename_done(res):
+ newdata = open(target_filename, "rb").read()
+ self.failUnlessEqual(newdata, DATA)
+ d.addCallback(_download_to_filename_done)
+
+ target_filename2 = os.path.join(self.basedir, "download.target2")
+ def _download_to_filehandle(res):
+ fh = open(target_filename2, "wb")
+ return self.downloader.download_to_filehandle(self.uri, fh)
+ d.addCallback(_download_to_filehandle)
+ def _download_to_filehandle_done(fh):
+ fh.close()
+ newdata = open(target_filename2, "rb").read()
+ self.failUnlessEqual(newdata, DATA)
+ d.addCallback(_download_to_filehandle_done)
+
return d
test_upload_and_download.timeout = 300