2 """This file defines a basic download-to-memory consumer, suitable for use in
3 a filenode's read() method. See download_to_data() for an example of its use.
6 from zope.interface import implements
7 from twisted.internet.interfaces import IConsumer
12 def __init__(self, progress=None):
15 self._progress = progress
17 def registerProducer(self, p, streaming):
20 # call resumeProducing once to start things off
26 def write(self, data):
27 self.chunks.append(data)
28 if self._progress is not None:
29 self._progress.set_progress(sum([len(c) for c in self.chunks]))
31 def unregisterProducer(self):
34 def download_to_data(n, offset=0, size=None, progress=None):
36 :param on_progress: if set, a single-arg callable that receives total bytes downloaded
38 d = n.read(MemoryConsumer(progress=progress), offset, size)
39 d.addCallback(lambda mc: "".join(mc.chunks))