[Zope3-checkins] SVN: Zope3/trunk/src/zope/ Added a new configuration field type, GlobalInterface, and applied it

Shane Hathaway shane at zope.com
Sat Nov 13 16:05:19 EST 2004


Log message for revision 28450:
  Added a new configuration field type, GlobalInterface, and applied it 
  to GlobalObject fields that required an interface.
  
  This is designed to improve configuration documentation and catch 
  mistakes early.
  
  Note that it turns out there are many browser pages registered for a 
  class, not an interface, so I assume that the documentation for page 
  directives is wrong.  Apparently you can register pages for not 
  only interfaces but also classes.  In this case, I changed the 
  docstring to reflect that.
  
  

Changed:
  U   Zope3/trunk/src/zope/app/component/metadirectives.py
  U   Zope3/trunk/src/zope/app/container/browser/metaconfigure.py
  U   Zope3/trunk/src/zope/app/dav/metadirectives.py
  U   Zope3/trunk/src/zope/app/exception/browser/configure.zcml
  U   Zope3/trunk/src/zope/app/form/browser/metadirectives.py
  U   Zope3/trunk/src/zope/app/onlinehelp/interfaces.py
  U   Zope3/trunk/src/zope/app/onlinehelp/metadirectives.py
  U   Zope3/trunk/src/zope/app/pagelet/metadirectives.py
  U   Zope3/trunk/src/zope/app/publisher/browser/metadirectives.py
  U   Zope3/trunk/src/zope/app/publisher/xmlrpc/metadirectives.py
  U   Zope3/trunk/src/zope/app/renderer/metaconfigure.py
  U   Zope3/trunk/src/zope/app/security/metadirectives.py
  U   Zope3/trunk/src/zope/app/site/browser/metadirectives.py
  U   Zope3/trunk/src/zope/configuration/config.py
  U   Zope3/trunk/src/zope/configuration/fields.py
  U   Zope3/trunk/src/zope/schema/_field.py

-=-
Modified: Zope3/trunk/src/zope/app/component/metadirectives.py
===================================================================
--- Zope3/trunk/src/zope/app/component/metadirectives.py	2004-11-13 17:10:04 UTC (rev 28449)
+++ Zope3/trunk/src/zope/app/component/metadirectives.py	2004-11-13 21:05:19 UTC (rev 28450)
@@ -99,7 +99,7 @@
         Multiple interfaces can be provided, separated by
         whitespace."""),
         required=False,
-        value_type=zope.configuration.fields.GlobalObject(),
+        value_type=zope.configuration.fields.GlobalInterface(),
         )
 
     allowed_attributes = zope.configuration.fields.Tokens(
@@ -126,7 +126,7 @@
         default=u'',
         )
 
-    provides = zope.configuration.fields.GlobalObject(
+    provides = zope.configuration.fields.GlobalInterface(
         title=_("The interface this component provides."),
         description=_("""
         A view can provide an interface.  This would be used for
@@ -135,7 +135,7 @@
         default=zope.interface.Interface,
         )
 
-    type = zope.configuration.fields.GlobalObject(
+    type = zope.configuration.fields.GlobalInterface(
         title=_("Request type"),
         required=True
         )
@@ -145,12 +145,12 @@
     Define an interface
     """
     
-    interface = zope.configuration.fields.GlobalObject(
+    interface = zope.configuration.fields.GlobalInterface(
         title=_("Interface"),
         required=True,
         )
 
-    type = zope.configuration.fields.GlobalObject(
+    type = zope.configuration.fields.GlobalInterface(
         title=_("Interface type"),
         required=False,
         )
@@ -168,7 +168,7 @@
         value_type=zope.configuration.fields.GlobalObject()
         )
 
-    provides = zope.configuration.fields.GlobalObject(
+    provides = zope.configuration.fields.GlobalInterface(
         title=_("Interface the component provides"),
         description=_("This attribute specifes the interface the adapter"
                       " instance must provide."),
@@ -224,7 +224,7 @@
         required=True
         )
 
-    provides = zope.configuration.fields.GlobalObject(
+    provides = zope.configuration.fields.GlobalInterface(
         title=_("Interface the component provides"),
         description=_("This attribute specifes the interface the adapter"
                       " instance must provide."),
@@ -264,7 +264,7 @@
 class IUtilityDirective(IBasicComponentInformation):
     """Register a utility."""
 
-    provides = zope.configuration.fields.GlobalObject(
+    provides = zope.configuration.fields.GlobalInterface(
         title=_("Provided interface"),
         description=_("Interface provided by the utility."),
         required=True
@@ -322,7 +322,7 @@
     explicitly).
     """
 
-    for_ = zope.configuration.fields.GlobalObject(
+    for_ = zope.configuration.fields.GlobalInterface(
         title=_("The interface this view is the default for."),
         description=_("""
         Specifies the interface for which the default view is declared. All
@@ -346,7 +346,7 @@
     allowed_interface = zope.configuration.fields.Tokens(
         title=_("Interface that is also allowed if user has permission."),
         required=False,
-        value_type=zope.configuration.fields.GlobalObject(),
+        value_type=zope.configuration.fields.GlobalInterface(),
         )
 
     allowed_attributes = zope.configuration.fields.Tokens(
@@ -364,7 +364,7 @@
         required=True
         )
 
-    interface = zope.configuration.fields.GlobalObject(
+    interface = zope.configuration.fields.GlobalInterface(
         title=_("Interface of the service type"),
         required=True
         )
@@ -393,7 +393,7 @@
     interface = zope.configuration.fields.Tokens(
         title=_("One or more interfaces"),
         required=True,
-        value_type=zope.configuration.fields.GlobalObject()
+        value_type=zope.configuration.fields.GlobalInterface()
         )
 
 class IRequireSubdirective(zope.interface.Interface):
@@ -430,7 +430,7 @@
         description=_("The listed interfaces' methods and attributes"
                       " can be accessed."),
         required=False,
-        value_type=zope.configuration.fields.GlobalObject(),
+        value_type=zope.configuration.fields.GlobalInterface(),
         )
 
     set_schema = zope.configuration.fields.Tokens(
@@ -438,7 +438,7 @@
         description=_("The listed schemas' properties can be"
                       " modified/mutated."),
         required=False,
-        value_type=zope.configuration.fields.GlobalObject(),
+        value_type=zope.configuration.fields.GlobalInterface(),
         )
 
     like_class = zope.configuration.fields.GlobalObject(
@@ -466,7 +466,7 @@
     interface = zope.configuration.fields.Tokens(
         title=_("Interface"),
         required=False,
-        value_type=zope.configuration.fields.GlobalObject(),
+        value_type=zope.configuration.fields.GlobalInterface(),
         )
 
 class IFactorySubdirective(zope.interface.Interface):
@@ -498,7 +498,7 @@
 class IDefaultLayerDirective(zope.interface.Interface):
     """Associate a default layer with a request type."""
 
-    type = zope.configuration.fields.GlobalObject(
+    type = zope.configuration.fields.GlobalInterface(
         title=_("Request type"),
         required=True
         )

Modified: Zope3/trunk/src/zope/app/container/browser/metaconfigure.py
===================================================================
--- Zope3/trunk/src/zope/app/container/browser/metaconfigure.py	2004-11-13 17:10:04 UTC (rev 28449)
+++ Zope3/trunk/src/zope/app/container/browser/metaconfigure.py	2004-11-13 21:05:19 UTC (rev 28450)
@@ -18,7 +18,7 @@
 __docformat__ = 'restructuredtext'
 
 from zope.interface import Interface
-from zope.configuration.fields import GlobalObject
+from zope.configuration.fields import GlobalInterface
 from zope.schema import Id
 from zope.app.publisher.browser.viewmeta import page, view
 from zope.app.container.browser.contents import Contents
@@ -29,7 +29,7 @@
 class IContainerViews(Interface):
     """Define several container views for an `IContainer` implementation."""
 
-    for_ = GlobalObject(
+    for_ = GlobalInterface(
         title=u"The interface this containerViews are for.",
         description=u"""
         The containerViews will be for all objects that implement this

Modified: Zope3/trunk/src/zope/app/dav/metadirectives.py
===================================================================
--- Zope3/trunk/src/zope/app/dav/metadirectives.py	2004-11-13 17:10:04 UTC (rev 28449)
+++ Zope3/trunk/src/zope/app/dav/metadirectives.py	2004-11-13 21:05:19 UTC (rev 28450)
@@ -17,7 +17,7 @@
 """
 __docformat__ = 'restructuredtext'
 
-from zope.configuration.fields import GlobalObject
+from zope.configuration.fields import GlobalInterface
 from zope.interface import Interface
 from zope.schema import URI
 
@@ -28,10 +28,10 @@
     for_ = URI(
         title=u"Namespace",
         description=u"Namespace under which this interface will be available"\
-                    u"via DAV.",
+                    u" via DAV.",
         required=True)
 
-    interface = GlobalObject(
+    interface = GlobalInterface(
         title=u"Interface",
         description=u"Specifies an interface/schema for DAV.",
         required=True)

Modified: Zope3/trunk/src/zope/app/exception/browser/configure.zcml
===================================================================
--- Zope3/trunk/src/zope/app/exception/browser/configure.zcml	2004-11-13 17:10:04 UTC (rev 28449)
+++ Zope3/trunk/src/zope/app/exception/browser/configure.zcml	2004-11-13 21:05:19 UTC (rev 28450)
@@ -27,7 +27,7 @@
       />
 
   <page
-      for="zope.publisher.interfaces.NotFound"
+      for="zope.publisher.interfaces.INotFound"
       name="index.html"
       permission="zope.Public"
       template="notfound.pt"

Modified: Zope3/trunk/src/zope/app/form/browser/metadirectives.py
===================================================================
--- Zope3/trunk/src/zope/app/form/browser/metadirectives.py	2004-11-13 17:10:04 UTC (rev 28449)
+++ Zope3/trunk/src/zope/app/form/browser/metadirectives.py	2004-11-13 21:05:19 UTC (rev 28450)
@@ -18,8 +18,9 @@
 __docformat__ = 'restructuredtext'
 
 from zope.interface import Interface
-from zope.configuration.fields import GlobalObject, Tokens, Path, \
-     Bool, PythonIdentifier, MessageID
+from zope.configuration.fields import GlobalObject, GlobalInterface
+from zope.configuration.fields import Tokens, Path, Bool, PythonIdentifier
+from zope.configuration.fields import MessageID
 from zope.schema import Text, TextLine, Id
 from zope.app.component.fields import LayerField
 from zope.app.security.fields import Permission
@@ -36,13 +37,13 @@
         required=True
         )
 
-    schema = GlobalObject(
+    schema = GlobalInterface(
         title=u"Schema",
         description=u"The schema from which the form is generated.",
         required=True
         )
 
-    for_ = GlobalObject(
+    for_ = GlobalInterface(
         title=u"Interface",
         description=u"""
         The interface this page (view) applies to.

Modified: Zope3/trunk/src/zope/app/onlinehelp/interfaces.py
===================================================================
--- Zope3/trunk/src/zope/app/onlinehelp/interfaces.py	2004-11-13 17:10:04 UTC (rev 28449)
+++ Zope3/trunk/src/zope/app/onlinehelp/interfaces.py	2004-11-13 21:05:19 UTC (rev 28450)
@@ -20,7 +20,7 @@
 __docformat__ = 'restructuredtext'
 
 from zope.schema import TextLine, SourceText, Choice
-from zope.configuration.fields import GlobalObject
+from zope.configuration.fields import GlobalInterface
 from zope.app.container.interfaces import IContainer
 from zope.app.file.interfaces import IFile, IFileContent
 from zope.app.i18n import ZopeMessageIDFactory as _ 
@@ -82,7 +82,7 @@
         required = True,
         vocabulary = "SourceTypes")
 
-    interface = GlobalObject(
+    interface = GlobalInterface(
         title=_(u"Object Interface"),
         description=_(u"Interface for which this Help Topic is registered."),
         default=None,

Modified: Zope3/trunk/src/zope/app/onlinehelp/metadirectives.py
===================================================================
--- Zope3/trunk/src/zope/app/onlinehelp/metadirectives.py	2004-11-13 17:10:04 UTC (rev 28449)
+++ Zope3/trunk/src/zope/app/onlinehelp/metadirectives.py	2004-11-13 21:05:19 UTC (rev 28450)
@@ -17,7 +17,7 @@
 """
 __docformat__ = 'restructuredtext'
 
-from zope.configuration.fields import GlobalObject, Path, MessageID, Tokens
+from zope.configuration.fields import GlobalInterface, Path, MessageID, Tokens
 from zope.interface import Interface
 from zope.schema import BytesLine, TextLine
 
@@ -43,7 +43,7 @@
         default="",
         required=False)
 
-    for_ = GlobalObject(
+    for_ = GlobalInterface(
         title=u"Object Interface",
         description=u"Interface for which this Help Topic is registered.",
         default=None,

Modified: Zope3/trunk/src/zope/app/pagelet/metadirectives.py
===================================================================
--- Zope3/trunk/src/zope/app/pagelet/metadirectives.py	2004-11-13 17:10:04 UTC (rev 28449)
+++ Zope3/trunk/src/zope/app/pagelet/metadirectives.py	2004-11-13 21:05:19 UTC (rev 28450)
@@ -25,7 +25,7 @@
 from zope.app.security.fields import Permission
 from zope.app.component.fields import LayerField
 
-from zope.configuration.fields import GlobalObject
+from zope.configuration.fields import GlobalObject, GlobalInterface
 
 
 
@@ -38,7 +38,7 @@
         required=True
         )
 
-    slot = GlobalObject(
+    slot = GlobalInterface(
         title=u"slot",
         description=u"The slot interface this pagelet is for.",
         required=True
@@ -50,7 +50,7 @@
         required=True
         )
 
-    for_ = GlobalObject(
+    for_ = GlobalInterface(
         title=u"for",
         description=u"The interface this pagelet is for (default IInterface)",
         required=False
@@ -64,7 +64,7 @@
         required=False
         )
 
-    view = GlobalObject(
+    view = GlobalInterface(
         title=u"view",
         description=u"""
             The interface of the view this pagelet is for. (default IView)""",

Modified: Zope3/trunk/src/zope/app/publisher/browser/metadirectives.py
===================================================================
--- Zope3/trunk/src/zope/app/publisher/browser/metadirectives.py	2004-11-13 17:10:04 UTC (rev 28449)
+++ Zope3/trunk/src/zope/app/publisher/browser/metadirectives.py	2004-11-13 21:05:19 UTC (rev 28450)
@@ -18,8 +18,8 @@
 $Id$
 """
 from zope.interface import Interface
-from zope.configuration.fields import GlobalObject, Tokens, Path, \
-     PythonIdentifier, MessageID
+from zope.configuration.fields import GlobalObject, GlobalInterface
+from zope.configuration.fields import Tokens, Path, PythonIdentifier, MessageID
 from zope.schema import TextLine, Text, Id, Int
 
 from zope.app.component.metadirectives import IBasicViewInformation
@@ -41,7 +41,7 @@
     """
 
     for_ = GlobalObject(
-        title=u"The interface this view is for.",
+        title=u"The interface or class this view is for.",
         required=False
         )
 
@@ -59,7 +59,7 @@
     traversing to the view name and then traversing to the page name.
     """
 
-    for_ = GlobalObject(
+    for_ = GlobalInterface(
         title=u"The interface this view is for.",
         required=False
         )
@@ -91,7 +91,7 @@
         required=False
         )
 
-    provides = GlobalObject(
+    provides = GlobalInterface(
         title=u"The interface this view provides.",
         description=u"""
         A view can provide an interface.  This would be used for
@@ -165,7 +165,7 @@
         required=True
         )
 
-    for_ = GlobalObject(
+    for_ = GlobalInterface(
         title=u"The interface this view is the default for.",
         description=u"""Specifies the interface for which the view is
         registered. All objects implementing this interface can make use of
@@ -370,7 +370,7 @@
         required=False
         )
 
-    interface = GlobalObject(
+    interface = GlobalInterface(
         title=u"The menu's interface.",
         required=False
         )
@@ -390,7 +390,7 @@
         required=True,
         )
 
-    for_ = GlobalObject(
+    for_ = GlobalInterface(
         title=u"Interface",
         description=u"The interface the menu items are defined for",
         required=False
@@ -517,7 +517,7 @@
         required=False
         )
 
-    interface = GlobalObject(
+    interface = GlobalInterface(
         title=u"The layer's interface.",
         required=False
         )
@@ -546,7 +546,7 @@
         required=False
         )
 
-    interface = GlobalObject(
+    interface = GlobalInterface(
         title=u"The skin's interface.",
         required=False
         )
@@ -584,7 +584,7 @@
         required=True
         )
 
-    for_ = GlobalObject(
+    for_ = GlobalInterface(
         title=u"The interface this icon is for.",
         description=u"""
         The icon will be for all objects that implement this

Modified: Zope3/trunk/src/zope/app/publisher/xmlrpc/metadirectives.py
===================================================================
--- Zope3/trunk/src/zope/app/publisher/xmlrpc/metadirectives.py	2004-11-13 17:10:04 UTC (rev 28449)
+++ Zope3/trunk/src/zope/app/publisher/xmlrpc/metadirectives.py	2004-11-13 21:05:19 UTC (rev 28450)
@@ -36,7 +36,7 @@
     interface = zope.configuration.fields.Tokens(
         title=u"Interface to be published.",
         required=False,
-        value_type=zope.configuration.fields.GlobalObject()
+        value_type=zope.configuration.fields.GlobalInterface()
         )
 
     methods = zope.configuration.fields.Tokens(

Modified: Zope3/trunk/src/zope/app/renderer/metaconfigure.py
===================================================================
--- Zope3/trunk/src/zope/app/renderer/metaconfigure.py	2004-11-13 17:10:04 UTC (rev 28449)
+++ Zope3/trunk/src/zope/app/renderer/metaconfigure.py	2004-11-13 21:05:19 UTC (rev 28450)
@@ -17,25 +17,25 @@
 """
 from zope.app import zapi
 from zope.app.component.metaconfigure import handler
-from zope.configuration.fields import GlobalObject
+from zope.configuration.fields import GlobalInterface
 from zope.interface import Interface
 
 class IRendererDirective(Interface):
     """Register a renderer for a paricular output interface, such as
     IBrowserView."""
 
-    sourceType = GlobalObject(
+    sourceType = GlobalInterface(
         title=u"Source Type Interface",
         description=u"Specifies an interface for of a particular source type.",
         required=True)
 
-    for_ = GlobalObject(
+    for_ = GlobalInterface(
         title=u"Interface of the output type",
         description=u"Specifies the interface of the output type (i.e. "
                     u"browser) for which this view is being registered.",
         required=True)
 
-    factory = GlobalObject(
+    factory = GlobalInterface(
         title=u"Factory",
         description=u"Specifies the factory that is used to create the "
                     u"view on the source.",

Modified: Zope3/trunk/src/zope/app/security/metadirectives.py
===================================================================
--- Zope3/trunk/src/zope/app/security/metadirectives.py	2004-11-13 17:10:04 UTC (rev 28449)
+++ Zope3/trunk/src/zope/app/security/metadirectives.py	2004-11-13 21:05:19 UTC (rev 28450)
@@ -16,8 +16,8 @@
 $Id$
 """
 from zope.interface import Interface
-from zope.configuration.fields import GlobalObject, Tokens, PythonIdentifier
-from zope.configuration.fields import MessageID
+from zope.configuration.fields import GlobalObject, GlobalInterface
+from zope.configuration.fields import Tokens, PythonIdentifier, MessageID
 from zope.schema import InterfaceField, Id, TextLine
 from fields import Permission
 
@@ -58,7 +58,7 @@
         description=u"Interfaces whos names to provide access to. Access "
                     u"will be provided to all of the names defined by the "
                     u"interface(s). Multiple interfaces can be supplied.",
-        value_type = GlobalObject(value_type=InterfaceField()),
+        value_type = GlobalInterface(),
         required=False)
 
 

Modified: Zope3/trunk/src/zope/app/site/browser/metadirectives.py
===================================================================
--- Zope3/trunk/src/zope/app/site/browser/metadirectives.py	2004-11-13 17:10:04 UTC (rev 28449)
+++ Zope3/trunk/src/zope/app/site/browser/metadirectives.py	2004-11-13 21:05:19 UTC (rev 28450)
@@ -15,7 +15,7 @@
 
 $Id$
 """
-from zope.configuration.fields import GlobalObject, PythonIdentifier, MessageID
+from zope.configuration.fields import GlobalInterface, PythonIdentifier, MessageID
 from zope.interface import Interface
 
 class IToolDirective(Interface):
@@ -42,7 +42,7 @@
 class IUtilityToolDirective(IToolDirective):
     """Directive for creating new utility-based tools."""
 
-    interface = GlobalObject(
+    interface = GlobalInterface(
         title=u"Interface",
         description=u"Interface used to filter out the available entries in a \
                       tool",

Modified: Zope3/trunk/src/zope/configuration/config.py
===================================================================
--- Zope3/trunk/src/zope/configuration/config.py	2004-11-13 17:10:04 UTC (rev 28449)
+++ Zope3/trunk/src/zope/configuration/config.py	2004-11-13 21:05:19 UTC (rev 28450)
@@ -999,16 +999,10 @@
 ##############################################################################
 # Directive-definition
 
-class DirectiveSchema(fields.GlobalObject):
+class DirectiveSchema(fields.GlobalInterface):
     """A field that contains a global variable value that must be a schema
     """
 
-    def _validate(self, value):
-        super(fields.GlobalObject, self)._validate(value)
-        if not IInterface.providedBy(value):
-            raise WrongType(value)
-
-
 class IDirectivesInfo(Interface):
     """Schema for the ``directives`` directive
     """
@@ -1054,13 +1048,12 @@
         description = u"The dotted name of the directive handler",
         )
 
-    usedIn = fields.GlobalObject(
+    usedIn = fields.GlobalInterface(
         title = u"The directive types the directive can be used in",
         description = (u"The interface of the directives that can contain "
                        u"the directive"
                        ),
         default = IConfigurationContext,
-        value_type = zope.schema.InterfaceField(),
         )
 
 class IStandaloneDirectiveInfo(IDirectivesInfo, IFullInfo):

Modified: Zope3/trunk/src/zope/configuration/fields.py
===================================================================
--- Zope3/trunk/src/zope/configuration/fields.py	2004-11-13 17:10:04 UTC (rev 28449)
+++ Zope3/trunk/src/zope/configuration/fields.py	2004-11-13 21:05:19 UTC (rev 28450)
@@ -143,6 +143,40 @@
         self.validate(value)
         return value
 
+class GlobalInterface(GlobalObject):
+    """An interface that can be accessed from a module.
+
+    First, we need to set up a stub name resolver:
+
+    >>> class Foo(object): pass
+
+    >>> from zope.interface import Interface
+    >>> class IFoo(Interface): pass
+
+    >>> d = {'Foo': Foo, 'IFoo': IFoo}
+    >>> class fakeresolver(dict):
+    ...     def resolve(self, n):
+    ...         return self[n]
+
+    >>> fake = fakeresolver(d)
+
+    Now verify constraints are checked correctly.
+
+    >>> g = GlobalInterface()
+    >>> gg = g.bind(fake)
+    >>> gg.fromUnicode('IFoo')
+    <InterfaceClass zope.configuration.fields.IFoo>
+    >>> gg.fromUnicode('  IFoo  ')
+    <InterfaceClass zope.configuration.fields.IFoo>
+    >>> gg.fromUnicode('Foo')
+    Traceback (most recent call last):
+    ...
+    WrongType: An interface is required
+    """
+
+    def __init__(self, **kw):
+        super(GlobalInterface, self).__init__(schema.InterfaceField(), **kw)
+
 class Tokens(schema.List):
     """A list that can be read from a space-separated string
 

Modified: Zope3/trunk/src/zope/schema/_field.py
===================================================================
--- Zope3/trunk/src/zope/schema/_field.py	2004-11-13 17:10:04 UTC (rev 28449)
+++ Zope3/trunk/src/zope/schema/_field.py	2004-11-13 21:05:19 UTC (rev 28450)
@@ -259,7 +259,7 @@
     def _validate(self, value):
         super(InterfaceField, self)._validate(value)
         if not IInterface.providedBy(value):
-            raise WrongType
+            raise WrongType("An interface is required")
 
 def _validate_sequence(value_type, value, errors=None):
     """Validates a sequence value.



More information about the Zope3-Checkins mailing list