def open(self, uri, etag=None): p = parse.urlparse(uri) netloc = p.hostname if p.port: netloc += ":" + p.port uri = parse.urlunparse((p.scheme, netloc, p[2], p[3], p[4], p[5])) req = request.Request(uri) if p.username and p.password: header = base64.encodestring("%s:%s" % (p.username, p.password)) req.add_header("Authorization", "Basic " + header) if etag: req.add_header("If-None-Match", etag) try: fp = request.urlopen(req) except request.URLError as exc: raise NotFound("URL '%s' not found (URLError)" % uri) except request.HTTPError as exc: if exc.code == 304: raise NotModified("URL '%s' has not been modified" % uri) raise NotFound( "URL '%s' could not be found (HTTP response %s)" % (uri, exc.code)) class Resource(FpAdaptor): @property def etag(self): info = self.fp.info() if "etag" in info: return info["etag"] return None @property def len(self): return int(self.fp.info()['content-length']) @property def labels(self): return () uri = uri return Resource(fp)
def _scheme(self, uri): parsed = parse.urlparse(uri) return parsed.scheme