[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/component/ Added a
new `LayerField`,
so that we can ensure that layer names are correclty
Stephan Richter
srichter at cosmos.phy.tufts.edu
Fri Sep 17 12:16:10 EDT 2004
Log message for revision 27586:
Added a new `LayerField`, so that we can ensure that layer names are correclty
converted to interfaces.
Changed:
A Zope3/trunk/src/zope/app/component/fields.py
A Zope3/trunk/src/zope/app/component/tests/test_fields.py
-=-
Added: Zope3/trunk/src/zope/app/component/fields.py
===================================================================
--- Zope3/trunk/src/zope/app/component/fields.py 2004-09-17 16:16:08 UTC (rev 27585)
+++ Zope3/trunk/src/zope/app/component/fields.py 2004-09-17 16:16:09 UTC (rev 27586)
@@ -0,0 +1,112 @@
+#############################################################################
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Browser configuration code
+
+This module defines the schemas for browser directives.
+
+$Id: metadirectives.py 26994 2004-08-11 10:07:39Z gintautasm $
+"""
+__docformat__ = 'restructuredtext'
+
+import zope.schema
+from zope.component.exceptions import ComponentLookupError
+from zope.configuration.exceptions import ConfigurationError
+from zope.configuration.fields import GlobalObject
+from zope.interface.interfaces import IInterface
+from zope.publisher.interfaces.browser import ILayer
+
+from zope.app import zapi
+
+
+class LayerField(GlobalObject):
+ r"""This fields represents a layer.
+
+ Besides being able to look up the layer by importing it, we also try
+ to look up the name in the utility service.
+
+ >>> from zope.interface import directlyProvides
+ >>> from zope.interface.interface import InterfaceClass
+
+ >>> layer1 = InterfaceClass('layer1', (),
+ ... __doc__='Layer: layer1',
+ ... __module__='zope.app.layers')
+ >>> directlyProvides(layer1, ILayer)
+
+ >>> layers = None
+ >>> class Resolver(object):
+ ... def resolve(self, path):
+ ... if path.startswith('zope.app.layers') and \
+ ... hasattr(layers, 'layer1') or \
+ ... path == 'zope.app.component.fields.layer1':
+ ... return layer1
+ ... raise ConfigurationError, 'layer1'
+
+ >>> field = LayerField()
+ >>> field = field.bind(Resolver())
+
+ Test 1: Import the layer
+ ------------------------
+
+ >>> field.fromUnicode('zope.app.component.fields.layer1') is layer1
+ True
+
+ Test 2: We have a shortcut name. Import the layer from `zope.app.layers`.
+ -------------------------------------------------------------------------
+
+ >>> from types import ModuleType as module
+ >>> import sys
+ >>> layers = module('layers')
+ >>> old = sys.modules.get('zope.app.layers', None)
+ >>> sys.modules['zope.app.layers'] = layers
+ >>> setattr(layers, 'layer1', layer1)
+
+ >>> field.fromUnicode('layer1') is layer1
+ True
+
+ >>> if old is not None:
+ ... sys.modules['zope.app.layers'] = old
+
+ Test 3: Get the layer from the utility service
+ ----------------------------------------------
+
+ >>> from zope.app.tests import ztapi
+ >>> ztapi.provideUtility(ILayer, layer1, 'layer1')
+
+ >>> field.fromUnicode('layer1') is layer1
+ True
+ """
+
+ def fromUnicode(self, u):
+ name = str(u.strip())
+
+ try:
+ value = zapi.queryUtility(ILayer, name)
+ except ComponentLookupError:
+ # The component architecture is not up and running.
+ pass
+ else:
+ if value is not None:
+ return value
+
+ try:
+ value = self.context.resolve(name)
+ except ConfigurationError, v:
+ name = 'zope.app.layers.'+name
+ try:
+ value = self.context.resolve(name)
+ except ConfigurationError, v:
+ raise zope.schema.ValidationError(v)
+
+ self.validate(value)
+ return value
Added: Zope3/trunk/src/zope/app/component/tests/test_fields.py
===================================================================
--- Zope3/trunk/src/zope/app/component/tests/test_fields.py 2004-09-17 16:16:08 UTC (rev 27585)
+++ Zope3/trunk/src/zope/app/component/tests/test_fields.py 2004-09-17 16:16:09 UTC (rev 27586)
@@ -0,0 +1,30 @@
+##############################################################################
+#
+# Copyright (c) 2004 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Test fields.
+
+$Id$
+"""
+import unittest
+from zope.testing.doctestunit import DocTestSuite
+from zope.app.tests import placelesssetup
+
+def test_suite():
+ return unittest.TestSuite((
+ DocTestSuite('zope.app.component.fields',
+ setUp=placelesssetup.setUp,
+ tearDown=placelesssetup.tearDown),
+ ))
+
+if __name__ == '__main__':
+ unittest.main(defaultTest='test_suite')
More information about the Zope3-Checkins
mailing list