Back to index

plone3  3.1.7
Functions
kss.demo.configfeature.meta Namespace Reference

Functions

def configfeature

Function Documentation

def kss.demo.configfeature.meta.configfeature (   _context,
  feature,
  instance,
  attribute,
  negate = False 
)
Imports a python object and uses an attribute of it as a boolean value
to declare a feature or not.

Usage
-----

We can have an object with attributes,

>>> class dictlike(object):
...     pass
>>> conf1 = dictlike()
>>> conf1.this = True
>>> conf1.that = False

and its attributes can defined a feature if they evaluate to True.
The feature will not be defined if the value evaluates to False.

>>> from zope.configuration.config import ConfigurationContext
>>> c = ConfigurationContext()
>>> configfeature(c, 'f1', conf1, 'this')
>>> c.hasFeature('f1')
True
>>> configfeature(c, 'f2', conf1, 'that')
>>> c.hasFeature('f2')
False

The boolean value can be negated, this allows it
to overcome the shortage of negating possibilit of zcml:condition.

>>> configfeature(c, 'f3', conf1, 'this', True)
>>> c.hasFeature('f3')
False
>>> configfeature(c, 'f4', conf1, 'that', True)
>>> c.hasFeature('f4')
True

Instead of an object with attributes, a dictionary can also be used:

>>> dconf = {}
>>> dconf['this'] = True
>>> dconf['that'] = False

>>> configfeature(c, 'f5', dconf, 'this')
>>> c.hasFeature('f5')
True
>>> configfeature(c, 'f6', dconf, 'that')
>>> c.hasFeature('f6')
False


Error handling
--------------

If there is an unexistent attribute, an error is reported.

>>> configfeature(c, 'f7', conf1, 'nosuch')         #doctest: +ELLIPSIS
Traceback (most recent call last):
...
ConfigurationError: Object ... does not have attribute or key "nosuch"

>>> configfeature(c, 'f8', dconf, 'nosuch')         #doctest: +ELLIPSIS
Traceback (most recent call last):
...
ConfigurationError: Object ... does not have attribute or key "nosuch"


Finally, like with the "provides" directive normally:
Spaces are not allowed in feature names (this is reserved for providing
many features with a single directive in the futute).

>>> configfeature(c, 'two words', conf1, 'this')
Traceback (most recent call last):
...
ValueError: Only one feature name allowed

Definition at line 4 of file meta.py.

00004 
00005 def configfeature(_context, feature, instance, attribute, negate=False):
00006     """Imports a python object and uses an attribute of it as a boolean value
00007     to declare a feature or not.
00008 
00009     Usage
00010     -----
00011 
00012     We can have an object with attributes,
00013 
00014     >>> class dictlike(object):
00015     ...     pass
00016     >>> conf1 = dictlike()
00017     >>> conf1.this = True
00018     >>> conf1.that = False
00019 
00020     and its attributes can defined a feature if they evaluate to True.
00021     The feature will not be defined if the value evaluates to False.
00022 
00023     >>> from zope.configuration.config import ConfigurationContext
00024     >>> c = ConfigurationContext()
00025     >>> configfeature(c, 'f1', conf1, 'this')
00026     >>> c.hasFeature('f1')
00027     True
00028     >>> configfeature(c, 'f2', conf1, 'that')
00029     >>> c.hasFeature('f2')
00030     False
00031 
00032     The boolean value can be negated, this allows it
00033     to overcome the shortage of negating possibilit of zcml:condition.
00034 
00035     >>> configfeature(c, 'f3', conf1, 'this', True)
00036     >>> c.hasFeature('f3')
00037     False
00038     >>> configfeature(c, 'f4', conf1, 'that', True)
00039     >>> c.hasFeature('f4')
00040     True
00041 
00042     Instead of an object with attributes, a dictionary can also be used:
00043 
00044     >>> dconf = {}
00045     >>> dconf['this'] = True
00046     >>> dconf['that'] = False
00047 
00048     >>> configfeature(c, 'f5', dconf, 'this')
00049     >>> c.hasFeature('f5')
00050     True
00051     >>> configfeature(c, 'f6', dconf, 'that')
00052     >>> c.hasFeature('f6')
00053     False
00054 
00055 
00056     Error handling
00057     --------------
00058 
00059     If there is an unexistent attribute, an error is reported.
00060 
00061     >>> configfeature(c, 'f7', conf1, 'nosuch')         #doctest: +ELLIPSIS
00062     Traceback (most recent call last):
00063     ...
00064     ConfigurationError: Object ... does not have attribute or key "nosuch"
00065 
00066     >>> configfeature(c, 'f8', dconf, 'nosuch')         #doctest: +ELLIPSIS
00067     Traceback (most recent call last):
00068     ...
00069     ConfigurationError: Object ... does not have attribute or key "nosuch"
00070 
00071 
00072     Finally, like with the "provides" directive normally:
00073     Spaces are not allowed in feature names (this is reserved for providing
00074     many features with a single directive in the futute).
00075 
00076     >>> configfeature(c, 'two words', conf1, 'this')
00077     Traceback (most recent call last):
00078     ...
00079     ValueError: Only one feature name allowed
00080 
00081     """
00082 
00083     try:
00084         value = getattr(instance, attribute)
00085     except AttributeError:
00086         # also try as dictionary value
00087         try:
00088             value = instance[attribute]
00089         except (TypeError, ValueError, KeyError):
00090             raise ConfigurationError, 'Object %s does not have attribute or key "%s"' % (instance, attribute)
00091 
00092     value = bool(value)
00093     if negate:
00094         value = not value
00095 
00096     if len(feature.split()) > 1:
00097         raise ValueError("Only one feature name allowed")
00098 
00099     if value:
00100         _context.provideFeature(feature)