Package zope :: Package configuration :: Module config :: Class ConfigurationContext
[show private | hide private]
[frames | no frames]

Type ConfigurationContext

object --+
         |
        ConfigurationContext

Known Subclasses:
ConfigurationMachine, GroupingContextDecorator

Mix-in that implements IConfigurationContext

Subclasses provide a package attribute and a basepath attribute. If the base path is not None, relative paths are converted to absolute paths using the the base path. If the package is not none, relative imports are performed relative to the package.

In general, the basepath and package attributes should be consistent. When a package is provided, the base path should be set to the path of the package directory.

Subclasses also provide an actions attribute, which is a list of actions, an includepath attribute, and an info attribute.

The include path is appended to each action and is used when resolving conflicts among actions. Normally, only the a ConfigurationMachine provides the actions attribute. Decorators simply use the actions of the context they decorate. The includepath attribute is a tuple of names. Each name is typically the name of an included configuration file.

The info attribute contains descriptive information helpful when reporting errors. If not set, it defaults to an empty string.

The actions attribute is a sequence of tuples with items:

For brevity, trailing items after the callable in the tuples are ommitted if they are empty.


Method Summary
  __init__(self)
  action(self, discriminator, callable, args, kw, order)
Add an action with the given discriminator, callable and arguments
  checkDuplicate(self, filename)
Check for duplicate imports of the same file.
  hasFeature(self, feature)
Check whether a named feature has been provided.
  path(self, filename)
Examples:
  processFile(self, filename)
Check whether a file needs to be processed
  provideFeature(self, feature)
Declare thata named feature has been provided.
  resolve(self, dottedname)
Resolve a dotted name to an object
    Inherited from object
  __delattr__(...)
x.__delattr__('name') <==> del x.name
  __getattribute__(...)
x.__getattribute__('name') <==> x.name
  __hash__(x)
x.__hash__() <==> hash(x)
  __new__(T, S, ...)
T.__new__(S, ...) -> a new object with type S, a subtype of T
  __reduce__(...)
helper for pickle
  __reduce_ex__(...)
helper for pickle
  __repr__(x)
x.__repr__() <==> repr(x)
  __setattr__(...)
x.__setattr__('name', value) <==> x.name = value
  __str__(x)
x.__str__() <==> str(x)

Class Variable Summary
Implements __implemented__ = <implementedBy zope.configuration.conf...
ClassProvides __providedBy__ = <zope.interface.declarations.ClassProvi...
ClassProvides __provides__ = <zope.interface.declarations.ClassProvide...

Method Details

action(self, 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)

checkDuplicate(self, 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(self, 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(self, 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(self, 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(self, feature)

Declare thata named feature has been provided.

See hasFeature for examples.

resolve(self, 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'>

Class Variable Details

__implemented__

Type:
Implements
Value:
<implementedBy zope.configuration.config.ConfigurationContext>         

__providedBy__

Type:
ClassProvides
Value:
<zope.interface.declarations.ClassProvides object at 0xb64a852c>       

__provides__

Type:
ClassProvides
Value:
<zope.interface.declarations.ClassProvides object at 0xb64a852c>       

Generated by Epydoc 2.1 on Fri Jun 24 12:01:19 2005 http://epydoc.sf.net