ZConfig.loader — Resource loading support

This module provides some helper classes used by the primary APIs exported by the ZConfig package. These classes may be useful for some applications, especially applications that want to use a non-default data type registry.

class ZConfig.loader.Resource(file, url)

Object that allows an open file object and a URL to be bound together to ease handling.

Instances have the attributes file and url, which store the constructor arguments. These objects also have a close() method which will call close() on file, then set the file attribute to None and the closed attribute to True.

All other attributes are delegated to file.

class ZConfig.loader.ConfigLoader(schema)

Bases: ZConfig.loader.BaseLoader

Loader for configuration files.

Each configuration file must conform to the schema schema. The load*() methods return a tuple consisting of the configuration object and a composite handler.

class ZConfig.loader.SchemaLoader(registry=None)

Bases: ZConfig.loader.BaseLoader

Loader that loads schema instances.

All schema loaded by a SchemaLoader will use the same data type registry. If registry is provided and not None, it will be used, otherwise an instance of ZConfig.datatypes.Registry will be used.

Loader Objects

Loader objects provide a general public interface, an interface which subclasses must implement, and some utility methods.

class ZConfig.loader.BaseLoader

Base class for loader objects.

This should not be instantiated directly, as the loadResource() method must be overridden for the instance to be used via the public API.

The following methods provide the public interface:


Open and load a resource specified by the URL url.

This method uses the loadResource() method to perform the actual load, and returns whatever that method returns.

BaseLoader.loadFile(file, url=None)

Load from an open file object, file.

If given and not None, url should be the URL of the resource represented by file. If omitted or None, the name attribute of file is used to compute a file: URL, if present.

This method uses the loadResource() method to perform the actual load, and returns whatever that method returns.

The following method must be overridden by subclasses:


Abstract method.

Subclasses of BaseLoader must implement this method to actually load the resource and return the appropriate application-level object.

The following methods can be used as utilities:


Return true if s should be considered a filesystem path rather than a 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, ValueError is raised, and if it does have a fragment identifier, ConfigurationError is raised.

This uses isPath() to determine whether url is a URL of a filesystem path.


Returns a resource object that represents the URL url.

The URL is opened using the urllib2.urlopen() function, and the returned resource object is created using createResource(). If the URL cannot be opened, ConfigurationError is raised.

BaseLoader.createResource(file, url)

Returns a resource object for an open file and URL, given as file and url, respectively.

This may be overridden by a subclass if an alternate resource implementation is desired.