def openPackageResource(package, path): __import__(package) pkg = sys.modules[package] try: loader = pkg.__loader__ except AttributeError: relpath = os.path.join(*path.split("/")) for dirname in pkg.__path__: filename = os.path.join(dirname, relpath) if os.path.exists(filename): break else: raise ZConfig.SchemaResourceError("schema component not found", filename=path, package=package, path=pkg.__path__) url = "file:" + pathname2url(filename) url = ZConfig.url.urlnormalize(url) return urllib2.urlopen(url) else: v, tb = (None, None) for dirname in pkg.__path__: loadpath = os.path.join(dirname, path) try: return StringIO( loader.get_data(loadpath).decode('utf-8')) except Exception as e: v = ZConfig.SchemaResourceError( "error opening schema component: " + repr(e), filename=path, package=package, path=pkg.__path__) tb = sys.exc_info()[2] if v is not None: try: reraise(type(v), v, tb) finally: del tb raise ZConfig.SchemaResourceError("schema component not found", filename=path, package=package, path=pkg.__path__)
def openPackageResource(package, path): __import__(package) pkg = sys.modules[package] try: loader = pkg.__loader__ except AttributeError: relpath = os.path.join(*path.split("/")) for dirname in pkg.__path__: filename = os.path.join(dirname, relpath) if os.path.exists(filename): break else: raise ZConfig.SchemaResourceError("schema component not found", filename=path, package=package, path=pkg.__path__) url = "file:" + pathname2url(filename) url = ZConfig.url.urlnormalize(url) return urllib2.urlopen(url) else: v, tb = (None, None) for dirname in pkg.__path__: loadpath = os.path.join(dirname, path) try: return StringIO(loader.get_data(loadpath).decode('utf-8')) except Exception as e: v = ZConfig.SchemaResourceError( "error opening schema component: " + repr(e), filename=path, package=package, path=pkg.__path__) tb = sys.exc_info()[2] if v is not None: try: reraise(type(v), v, tb) finally: del tb raise ZConfig.SchemaResourceError("schema component not found", filename=path, package=package, path=pkg.__path__)
def normalizeURL(self, url): """Return a URL for *url* If *url* refers to an existing file, the corresponding ``file:`` URL is returned. Otherwise *url* is checked for sanity: if it does not have a schema, :exc:`ValueError` is raised, and if it does have a fragment identifier, :exc:`~.ConfigurationError` is raised. This uses :meth:`isPath` to determine whether *url* is a URL of a filesystem path. """ if self.isPath(url): url = "file://" + pathname2url(os.path.abspath(url)) newurl, fragment = ZConfig.url.urldefrag(url) if fragment: raise ZConfig.ConfigurationError( "fragment identifiers are not supported", url) return newurl
def _url_from_file(file_or_path): name = getattr(file_or_path, "name", None) if name and name[0] != "<" and name[-1] != ">": return "file://" + pathname2url(os.path.abspath(name))
import contextlib import os import sys import unittest import ZConfig from ZConfig.loader import ConfigLoader from ZConfig.url import urljoin from ZConfig._compat import NStringIO as StringIO from ZConfig._compat import pathname2url INPUT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "input")) CONFIG_BASE = "file://%s/" % pathname2url(INPUT_DIR) def input_file(fname): return os.path.abspath(os.path.join(INPUT_DIR, fname)) @contextlib.contextmanager def _replaced_stream(name, buf=None): if buf is None: buf = StringIO() old_stream = getattr(sys, name) setattr(sys, name, buf) try: yield finally: