Function
zope.app.publisher.browser.metaconfigure.layer

Signature

layer(_context, name=None, interface=None, base=<InterfaceClass zope.publisher.interfaces.browser.IBrowserRequest>, bbb_aware=False)

Documentation String

Provides a new layer.

First, let's ignore the warnigns:

>>> showwarning = warnings.showwarning
>>> warnings.showwarning = lambda *a, **k: None
>>> class Info(object):
...     file = u'doctest'
...     line = 1
...
>>> class Context(object):
...     info = Info()
...     def __init__(self): self.actions = []
...     def action(self, **kw): self.actions.append(kw)

Possibility 1: The Old Way

>>> context = Context()
>>> layer(context, u'layer1')
>>> iface = context.actions[0]['args'][1]
>>> iface.getName()
'layer1'
>>> ILayer.providedBy(iface)
True
>>> import sys
>>> hasattr(sys.modules['zope.app.layers'], 'layer1')
True
>>> del sys.modules['zope.app.layers'].layer1

Possibility 2: Providing a custom base interface

>>> class BaseLayer(IBrowserRequest):
...     pass
>>> context = Context()
>>> layer(context, u'layer1', base=BaseLayer)
>>> iface = context.actions[0]['args'][1]
>>> iface.getName()
'layer1'
>>> iface.__bases__
(<InterfaceClass zope.app.publisher.browser.metaconfigure.BaseLayer>,)
>>> hasattr(sys.modules['zope.app.layers'], 'layer1')
True
>>> del sys.modules['zope.app.layers'].layer1

Possibility 3: Define a Layer just through an Interface

>>> class layer1(IBrowserRequest):
...     pass
>>> context = Context()
>>> layer(context, interface=layer1)
>>> context.actions[0]['args'][1] is layer1
True
>>> hasattr(sys.modules['zope.app.layers'], 'layer1')
False

Possibility 4: Use an Interface and a Name

>>> context = Context()
>>> layer(context, name='layer1', interface=layer1)
>>> context.actions[0]['args'][1] is layer1
True
>>> hasattr(sys.modules['zope.app.layers'], 'layer1')
True
>>> import pprint
>>> pprint.pprint([action['discriminator'] for action in context.actions])
[('interface', 'zope.app.publisher.browser.metaconfigure.layer1'),
 ('layer', 'layer1')]

Here are some disallowed configurations.

>>> context = Context()
>>> layer(context, 'foo,bar')
Traceback (most recent call last):
...
TypeError: Commas are not allowed in layer names.
>>> layer(context)
Traceback (most recent call last):
...
ConfigurationError: You must specify the 'name' or 'interface' attribute.
>>> layer(context, base=BaseLayer)
Traceback (most recent call last):
...
ConfigurationError: You must specify the 'name' or 'interface' attribute.
>>> layer(context, interface=layer1, base=BaseLayer)
Traceback (most recent call last):
...
ConfigurationError: You cannot specify the 'interface' and 'base' together.

Enabling the warnings again:

>>> warnings.showwarning = showwarning