ZConfig — Basic configuration support¶
ZConfig package exports these convenience functions:
loadConfig(schema, url, overrides=())¶
Load and return a configuration from a URL or pathname given by url.
The return value is a tuple containing the configuration object and a composite handler that, when called with a name-to-handler mapping, calls all the handlers for the configuration.
The optional overrides argument represents information derived from command-line arguments. If given, it must be either a sequence of value specifiers, or
None. A “value specifier” is a string of the form
optionpath=value, for example,
loadConfigFile(schema, file, url=None, overrides=())¶
Load and return a configuration from an opened file object.
If url is omitted, one will be computed based on the
nameattribute of file, if it exists. If no URL can be determined, all
%includestatements in the configuration must use absolute URLs. schema is a reference to a schema loaded by
The return value is a tuple containing the configuration object and a composite handler that, when called with a name-to-handler mapping, calls all the handlers for the configuration. The overrides argument is the same as for the
Load a schema definition from the URL url.
url may be a URL, absolute pathname, or relative pathname. Fragment identifiers are not supported.
Load a schema definition from the open file object file.
If url is given and not
None, it should be the URL of resource represented by file. If url is omitted or
None, a URL may be computed from the
nameattribute of file, if present. The resulting schema object can be passed to
loadConfigFile(). The schema object may be used as many times as needed.
The following exceptions are defined by this package:
Base class for exceptions specific to the
All instances provide a
messageattribute that describes the specific error, and a
urlattribute that gives the URL of the resource the error was located in, or
Exception raised when a configuration source does not conform to the allowed syntax.
In addition to the
urlattributes, exceptions of this type offer the
linenoattribute, which provides the line number at which the error was detected.
Raised when a data type conversion fails with
This exception is a subclass of both
str()of the exception provides the explanation from the original
ValueError, and the line number and URL of the value which provoked the error. The following additional attributes are provided:
- column number at which the value starts, or
- the original
- line number on which the value starts
str()returned by the original
- original value passed to the conversion function
- URL of the resource providing the value text
Raised when a schema contains an error.
This exception type provides the attributes
colno, which provide the source URL, the line number, and the column number at which the error was detected. These attributes may be
Nonein some cases.
Raised when there’s an error locating a resource required by the schema.
Instances of this exception class add the attributes
path, which hold the filename searched for within the package being loaded, the name of the package, and the
__path__attribute of the package itself (or
Noneif it isn’t a package or could not be imported).
Raised when the source text contains references to names which are not defined in mapping.
nameprovide the complete source text and the name (converted to lower case) for which no replacement is defined.
Raised when interpolation source text contains syntactical errors.
The simplest use of
ZConfig is to load a configuration
based on a schema stored in a file. This example loads a
configuration file specified on the command line using a schema in the
same directory as the script:
import os import sys import ZConfig try: myfile = __file__ except NameError: myfile = os.path.realpath(sys.argv) mydir = os.path.dirname(myfile) schema = ZConfig.loadSchema(os.path.join(mydir, 'schema.xml')) conf, handler = ZConfig.loadConfig(schema, sys.argv)
If the schema file contained this schema:
<schema> <key name='server' required='yes'/> <key name='attempts' datatype='integer' default='5'/> </schema>
and the file specified on the command line contained this text:
# sample configuration server www.example.com
then the configuration object
conf loaded above would have two
attributes, server with the value
attempts with the value