def open_local_file(self, url): """ Override base urlopener method in order to read our custom metadata """ #scheme,netloc,path,parameters,query,fragment = urlparse.urlparse(url) host, file = urllib.splithost(url) localname = urllib.url2pathname(file) path = localname if os.path.isdir(path): if path[-1] != os.sep: url += '/' for index in ['index.html', 'index.htm', 'index_html']: indexpath = os.path.join(path, index) if os.path.exists(indexpath): return self.open_file(url + index) try: names = os.listdir(path) except os.error, msg: exc_type, exc_value, exc_tb = sys.exc_info() raise IOError, msg, exc_tb names.sort() s = MyStringIO("file:" + url, {'content-type': 'text/html'}) s.write('<BASE HREF="file:%s">\n' % urllib.quote(os.path.join(path, ""))) for name in names: q = urllib.quote(name) s.write('<A HREF="%s">%s</A>\n' % (q, q)) s.seek(0) return s
urlfile = file if file[:1] == '/': urlfile = 'file://' + file return urllib.addinfourl(OpenOnRead(localname, 'rb'), headers, urlfile) host, port = urllib.splitport(host) if not port \ and socket.gethostbyname(host) in (urllib.localhost(), urllib.thishost()): urlfile = file if file[:1] == '/': urlfile = 'file://' + file return urllib.addinfourl(OpenOnRead(localname, 'rb'), headers, urlfile) raise IOError, ('local file error', 'not on local host') def dirlisting(selfself, path): try: names = os.listdir(path) except os.error, msg: exc_type, exc_value, exc_tb = sys.exc_info() raise IOError, msg, exc_tb names.sort() s = MyStringIO("file:"+path, {'content-type': 'text/html'}) s.write('<BASE HREF="file:%s">\n' % urllib.quote(os.path.join(path, ""))) for name in names: q = urllib.quote(name) s.write('<A HREF="%s">%s</A>\n' % (q, q)) s.seek(0) return s