def openResource(self, url): # ConfigurationError exceptions raised here should be # str()able to generate a message for an end user. # # XXX This should be replaced to use a local cache for remote # resources. The policy needs to support both re-retrieve on # change and provide the cached resource when the remote # resource is not accessible. url = str(url) if url.startswith("package:"): _, package, filename = url.split(":", 2) file = openPackageResource(package, filename) else: try: file = urllib2.urlopen(url) except urllib2.URLError as e: # urllib2.URLError has a particularly hostile str(), so we # generally don't want to pass it along to the user. self._raise_open_error(url, e.reason) except (IOError, OSError) as e: # Python 2.1 raises a different error from Python 2.2+, # so we catch both to make sure we detect the situation. self._raise_open_error(url, str(e)) if sys.version_info[0] >= 3: # Python 3 support: file.read() returns bytes, so we convert it # to an StringIO. (Can't use io.TextIOWrapper because of # http://bugs.python.org/issue16723 and probably other bugs) try: data = file.read().decode() finally: file.close() file = StringIO.StringIO(data) return self.createResource(file, url)
def openResource(self, url): """Returns a resource object that represents the URL *url*. The URL is opened using the :func:`urllib2.urlopen` function, and the returned resource object is created using :meth:`createResource`. If the URL cannot be opened, :exc:`~.ConfigurationError` is raised. """ # ConfigurationError exceptions raised here should be # str()able to generate a message for an end user. # # XXX This should be replaced to use a local cache for remote # resources. The policy needs to support both re-retrieve on # change and provide the cached resource when the remote # resource is not accessible. url = str(url) if url.startswith("package:"): _, package, filename = url.split(":", 2) file = openPackageResource(package, filename) else: try: file = urllib2.urlopen(url) except urllib2.URLError as e: # urllib2.URLError has a particularly hostile str(), so we # generally don't want to pass it along to the user. self._raise_open_error(url, e.reason) # pragma: no cover except (IOError, OSError) as e: # Python 2.1 raises a different error from Python 2.2+, # so we catch both to make sure we detect the situation. self._raise_open_error(url, str(e)) # Python 3 support: file.read() returns bytes, so we convert it # to an StringIO. (Can't use io.TextIOWrapper because of # http://bugs.python.org/issue16723 and probably other bugs). # Do this even on Python 2 to avoid keeping a network connection # open for an unbounded amount of time and to catch IOErrors here, # where they make sense. try: data = file.read() finally: file.close() if isinstance(data, bytes): # Be sure to specify an (useful) encoding so we don't get # the system default, typically ascii. data = data.decode('utf-8') file = StringIO(data) return self.createResource(file, url)
def openResource(self, url): # ConfigurationError exceptions raised here should be # str()able to generate a message for an end user. # # XXX This should be replaced to use a local cache for remote # resources. The policy needs to support both re-retrieve on # change and provide the cached resource when the remote # resource is not accessible. url = str(url) if url.startswith("package:"): _, package, filename = url.split(":", 2) file = openPackageResource(package, filename) else: try: file = urllib2.urlopen(url) except urllib2.URLError, e: # urllib2.URLError has a particularly hostile str(), so we # generally don't want to pass it along to the user. self._raise_open_error(url, e.reason) except (IOError, OSError), e: # Python 2.1 raises a different error from Python 2.2+, # so we catch both to make sure we detect the situation. self._raise_open_error(url, str(e))