"""Integration with Python standard library module urllib2: OpenerDirector class. Copyright 2004 John J Lee This code is free software; you can redistribute it and/or modify it under the terms of the BSD License (see the file COPYING included with the distribution). """ try: True except NameError: True = 1 False = 0 import urllib2, string, bisect, urlparse from _Util import startswith, isstringlike from _Request import Request def methnames(obj): """Return method names of class instance. dir(obj) doesn't work across Python versions, this does. """ return methnames_of_instance_as_dict(obj).keys() def methnames_of_instance_as_dict(inst): names = {} names.update(methnames_of_class_as_dict(inst.__class__)) for methname in dir(inst): candidate = getattr(inst, methname) if callable(candidate): names[methname] = None return names def methnames_of_class_as_dict(klass): names = {} for methname in dir(klass): candidate = getattr(klass, methname) if callable(candidate): names[methname] = None for baseclass in klass.__bases__: names.update(methnames_of_class_as_dict(baseclass)) return names class OpenerMixin: def _request(self, url_or_req, data): if isstringlike(url_or_req): req = Request(url_or_req, data) else: # already a urllib2.Request or ClientCookie.Request instance req = url_or_req if data is not None: req.add_data(data) return req def retrieve(self, fullurl, filename=None, reporthook=None, data=None): """Returns (filename, headers). For remote objects, the default filename will refer to a temporary file. """ req = self._request(fullurl, data) type_ = req.get_type() fp = self.open(req) headers = fp.info() if filename is None and type == 'file': return url2pathname(req.get_selector()), headers if filename: tfp = open(filename, 'wb') else: path = urlparse(fullurl)[2] suffix = os.path.splitext(path)[1] tfp = tempfile.TemporaryFile("wb", suffix=suffix) result = filename, headers bs = 1024*8 size = -1 read = 0 blocknum = 1 if reporthook: if headers.has_key("content-length"): size = int(headers["Content-Length"]) reporthook(0, bs, size) while 1: block = fp.read(bs) read += len(block) if reporthook: reporthook(blocknum, bs, size) blocknum = blocknum + 1 if not block: break tfp.write(block) fp.close() tfp.close() del fp del tfp if size>=0 and read