=== modified file 'launchpadbugs/http_connection.py' --- launchpadbugs/http_connection.py 2008-03-11 20:17:57 +0000 +++ launchpadbugs/http_connection.py 2008-06-13 08:44:01 +0000 @@ -2,6 +2,7 @@ import multipartpost_handler import utils import cookielib +import cStringIO as StringIO import bughelper_error as Error from tempfile import mkstemp try: @@ -36,6 +37,7 @@ self.__attempts = attempts self.content_types = content_types self.__baseurl = None + self.__progress_hook = None def get_auth(self): return self.__cookie_handler @@ -155,9 +157,24 @@ raise Error.LPUrlError("login failed", url) for ct in self.content_types: if contenttype.startswith(ct): - text = sock.read() - geturl = sock.geturl() - sock.close() + if self.__progress_hook is None: + text = sock.read() + geturl = sock.geturl() + sock.close() + else: + tmp_text = StringIO.StringIO() + i = 0 + counter = 0 + size = int(sock.info()['Content-Length']) + while i < size: + tmp_text.write(sock.read(self.__block_size)) + i += self.__block_size + counter += 1 + self.__progress_hook(counter, self.__block_size, size) + text = tmp_text.getvalue() + geturl = sock.geturl() + sock.close() + tmp_text.close() return _result(contenttype=contenttype,text=text, url=geturl) sock.close() raise IOError, "unsupported contenttype (%s)" %contenttype @@ -169,4 +186,21 @@ count += 1 raise Error.LPUrlError(error, url) - + + def set_progress_hook(self, hook_func, blocksize=4096): + assert blocksize, "blocksize needs to be an integer greater than 0" + self.__block_size = blocksize + assert callable(hook_func), "hook_func needs to be callable with three arguments" + self.__progress_hook = hook_func + + +if __name__ == '__main__': + c = HTTPConnection() + + def example_hook(counter, block_size, size): + print (counter, block_size, size) + + c.set_progress_hook(example_hook) + x = c.get("https://bugs.edge.launchpad.net/ubuntu/+source/linux/+bug/200500/") + print x.text +