Zope configure directive
This file contains the implementation of the Zope configure directive. It is broken out in a separate file to provide an example of a grouping directive.
The zope configuration directive is a pure grouping directive. It doesn't compute any actions on it's own. Instead, it allows a package to be specified, affecting the interpretation of relative dotted names and file paths. It also allows an i18n domain to be specified. The information collected is used by subdirectives.
To define a grouping directive, we need to do three things:
The parameter schema is given by IZopeConfigure. It specifies a package parameter and an i18n_domain paramter. The package parameter is specified as a GlobalObject. This means it must be given as a dotted name that can be resolved through import. The i18n domain is just a plain (not unicode) string.
The handler class has a constructor that takes a context to be adapted and zero or more arguments (depending on the paramter schema). The handler class must implement zope.configuration.interfaces.IGroupingContext, which defines hooks before and after, that are called with no arguments before and after nested directives are processed. If a grouping directive handler creates any actions, or does any computation, this is normally done in either the before or after hooks. Grouping handlers are normally decorators.
The base class, zope.configuration.config.GroupingContextDecorator, is normally used to define grouping directive handlers. It provides:
The ZopeConfigure provides handling for the configure directive. It subclasses GroupingContextDecorator, and overrides the constructor to set the basepath attribute if a package argument is provided. Note that it delegates the job of assigning paramters to attribute to the GroupingContextDecorator constructor.
The last step is to register the directive using the meta configuration directive. If we wanted to register the Zope configure directive for the zope namespace, we'd use a meta-configuration directive like:
<meta:groupingDirective namespace="http://namespaces.zope.org/zope" name="configure" schema="zope.configuration.zopeconfigure.IZopeConfigure" handler="zope.configuration.zopeconfigure.ZopeConfigure" > Zope configure The ``configure`` node is normally used as the root node for a configuration file. It can also be used to specify a package or internationalization domain for a group of directives within a file by grouping those directives. </meta:groupingDirective>
We use the groupingDirective meta-directive to register a grouping directive. The parameters are self explanatory. The textual contents of the directive provide documentation text, excluding parameter documentation, which is provided by the schema.
(The Zope configuration directive is actually registered using a lower-level Python API because it is registered for all namespaces, which isn't supported using the meta-configuration directives.)
$Id: zopeconfigure.py 26937 2004-08-06 16:33:35Z fdrake $
There are no attributes in this class.
action(discriminator, callable=None, args=(), kw={}, order=0)
Add an action with the given discriminator, callable and arguments
For testing purposes, the callable and arguments may be omitted. In that case, a default noop callable is used.
The discriminator must be given, but it can be None, to indicate that the action never conflicts.
Let's look at some examples:
>>> c = ConfigurationContext()
Normally, the context gets actions from subclasses. We'll provide an actions attribute ourselves:
>>> c.actions = []
We'll use a test callable that has a convenient string representation
>>> from zope.configuration.tests.directives import f
>>> c.action(1, f, (1, ), {'x': 1}) >>> c.actions [(1, f, (1,), {'x': 1})]
>>> c.action(None) >>> c.actions [(1, f, (1,), {'x': 1}), (None, None)]
Now set the include path and info:
>>> c.includepath = ('foo.zcml',) >>> c.info = "?" >>> c.action(None) >>> c.actions[-1] (None, None, (), {}, ('foo.zcml',), '?')
Finally, we can add an order argument to crudely control the order of execution:
>>> c.action(None, order=99999) >>> c.actions[-1] (None, None, (), {}, ('foo.zcml',), '?', 99999)
after()
before()
checkDuplicate(filename)
Check for duplicate imports of the same file.
Raises an exception if this file had been processed before. This is better than an unlimited number of conflict errors.
>>> c = ConfigurationContext() >>> c.checkDuplicate('/foo.zcml') >>> try: ... c.checkDuplicate('/foo.zcml') ... except ConfigurationError, e: ... # On Linux the exact msg has /foo, on Windows oo. ... str(e).endswith("foo.zcml' included more than once") True
You may use different ways to refer to the same file:
>>> import zope.configuration >>> c.package = zope.configuration >>> import os >>> d = os.path.dirname(zope.configuration.__file__) >>> c.checkDuplicate('bar.zcml') >>> try: ... c.checkDuplicate(d + os.path.normpath('/bar.zcml')) ... except ConfigurationError, e: ... str(e).endswith("bar.zcml' included more than once") ... True
hasFeature(feature)
Check whether a named feature has been provided.
Initially no features are provided
>>> c = ConfigurationContext() >>> c.hasFeature('onlinehelp') False
You can declare that a feature is provided
>>> c.provideFeature('onlinehelp')
and it becomes available
>>> c.hasFeature('onlinehelp') True
path(filename)
Examples:
>>> c = ConfigurationContext() >>> c.path("/x/y/z") == os.path.normpath("/x/y/z") 1 >>> c.path("y/z") Traceback (most recent call last): ... AttributeError: 'ConfigurationContext' object has no attribute 'package' >>> import zope.configuration >>> c.package = zope.configuration >>> import os >>> d = os.path.dirname(zope.configuration.__file__) >>> c.path("y/z") == d + os.path.normpath("/y/z") 1 >>> c.path("y/./z") == d + os.path.normpath("/y/z") 1 >>> c.path("y/../z") == d + os.path.normpath("/z") 1
processFile(filename)
Check whether a file needs to be processed
Return True if processing is needed and False otherwise. If the file needs to be processed, it will be marked as processed, assuming that the caller will procces the file if it needs to be procssed.
>>> c = ConfigurationContext() >>> c.processFile('/foo.zcml') True >>> c.processFile('/foo.zcml') False
You may use different ways to refer to the same file:
>>> import zope.configuration >>> c.package = zope.configuration >>> import os >>> d = os.path.dirname(zope.configuration.__file__) >>> c.processFile('bar.zcml') True >>> c.processFile('bar.zcml') False
provideFeature(feature)
Declare thata named feature has been provided.
See hasFeature for examples.
resolve(dottedname)
Resolve a dotted name to an object
Examples:
>>> c = ConfigurationContext() >>> import zope, zope.interface >>> c.resolve('zope') is zope 1 >>> c.resolve('zope.interface') is zope.interface 1
>>> c.resolve('zope.configuration.eek') #doctest: +NORMALIZE_WHITESPACE Traceback (most recent call last): ... ConfigurationError: ImportError: Module zope.configuration has no global eek
>>> c.resolve('.config.ConfigurationContext') Traceback (most recent call last): ... AttributeError: 'ConfigurationContext' object has no attribute 'package' >>> import zope.configuration >>> c.package = zope.configuration >>> c.resolve('.') is zope.configuration 1 >>> c.resolve('.config.ConfigurationContext') is ConfigurationContext 1 >>> c.resolve('..interface') is zope.interface 1 >>> c.resolve('unicode') <type 'unicode'>
There are no known subclasses.