[Zope3-checkins] CVS: Zope3/src/zope/app/schema - __init__.py:1.2 configure.zcml:1.2 meta.zcml:1.2 vocabulary.py:1.2

Fred L. Drake, Jr. fred@zope.com
Tue, 20 May 2003 12:10:59 -0400


Update of /cvs-repository/Zope3/src/zope/app/schema
In directory cvs.zope.org:/tmp/cvs-serv24680/src/zope/app/schema

Added Files:
	__init__.py configure.zcml meta.zcml vocabulary.py 
Log Message:
Merge schema-vocabulary-branch into the trunk.

Preliminary documentation on vocabularies and schema vocabulary fields
can be found at http://dev.zope.org/Zope3/VocabularyFields.


=== Zope3/src/zope/app/schema/__init__.py 1.1 => 1.2 ===
--- /dev/null	Tue May 20 12:10:59 2003
+++ Zope3/src/zope/app/schema/__init__.py	Tue May 20 12:10:29 2003
@@ -0,0 +1 @@
+# Make this a Python package.


=== Zope3/src/zope/app/schema/configure.zcml 1.1 => 1.2 ===
--- /dev/null	Tue May 20 12:10:59 2003
+++ Zope3/src/zope/app/schema/configure.zcml	Tue May 20 12:10:29 2003
@@ -0,0 +1,10 @@
+<zopeConfigure xmlns='http://namespaces.zope.org/zope'>
+
+<serviceType id='Vocabularies'
+             interface='zope.schema.interfaces.IVocabularyRegistry' />
+
+<service serviceType='Vocabularies'
+         permission='zope.Public'
+         component='zope.app.schema.vocabulary.vocabularyService' />
+
+</zopeConfigure>


=== Zope3/src/zope/app/schema/meta.zcml 1.1 => 1.2 ===
--- /dev/null	Tue May 20 12:10:59 2003
+++ Zope3/src/zope/app/schema/meta.zcml	Tue May 20 12:10:29 2003
@@ -0,0 +1,42 @@
+<zopeConfigure xmlns='http://namespaces.zope.org/zope'>
+<directives namespace="http://namespaces.zope.org/zope">
+
+  <directive name="vocabulary" handler=".vocabulary.register">
+    <!-- example:
+         <vocabulary name="garys-favorite-path-references"
+                     factory="zope.app.gary.paths.Favorites" />
+      -->
+
+    <description>
+      Define a named vocabulary.
+
+      This associates a vocabulary name in the global vocabulary
+      service with a factory.  Each name may only be defined once.
+
+      Additional keyword arguments may be passed to the factory by
+      adding additional attributes beyond those listed here.  This can
+      be useful when using vocabularies which implement various kinds
+      of filtering.
+    </description>
+
+    <attribute name="name" required="yes">
+      <description>
+        The name of the vocabulary; this can be used as the value for
+        the "vocabulary" argument to the VocabularyField and
+        VocabularyMultiField constructors to cause this vocabulary to
+        be used.
+      </description>
+    </attribute>
+
+    <attribute name="factory" required="yes">
+      <description>
+        Factory that returns an instance of the named vocabulary when
+        called with the context object as the only argument.  This
+        should be a dotted-name that refers to a Python object.
+      </description>
+    </attribute>
+
+  </directive>
+
+</directives>
+</zopeConfigure>


=== Zope3/src/zope/app/schema/vocabulary.py 1.1 => 1.2 ===
--- /dev/null	Tue May 20 12:10:59 2003
+++ Zope3/src/zope/app/schema/vocabulary.py	Tue May 20 12:10:29 2003
@@ -0,0 +1,69 @@
+##############################################################################
+#
+# Copyright (c) 2003 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (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.
+#
+##############################################################################
+
+"""Implementation of ZCML action to register vocabulary factories."""
+
+from zope.component import getService
+from zope.configuration.action import Action
+from zope.schema import vocabulary
+from zope.schema.interfaces import IVocabularyRegistry
+from zope.testing import cleanup
+
+
+def register(_context, name, factory, **kw):
+    factory = _context.resolve(factory.strip())
+    if kw:
+        factory = FactoryKeywordPasser(factory, kw)
+    return [
+        Action(discriminator=('defineVocabulary', name),
+               callable=vocabularyService.register,
+               args=(name, factory))
+        ]
+
+
+class FactoryKeywordPasser:
+    """Helper that passes additional keywords to the actual factory."""
+
+    def __init__(self, factory, kwargs):
+        self.factory = factory
+        self.kwargs = kwargs
+
+    def __call__(self, object):
+        return self.factory(object, **self.kwargs)
+
+
+class ZopeVocabularyRegistry(object):
+    """IVocabularyRegistry that supports local vocabulary services."""
+
+    __implements__ = IVocabularyRegistry
+    __slots__ = ()
+
+    def get(self, context, name):
+        vr = getService(context, "Vocabularies")
+        return vr.get(context, name)
+
+
+def _clear():
+    """Re-initialize the vocabulary service."""
+    # This should normally only be needed by the testing framework,
+    # but is also used for module initialization.
+    global vocabularyService
+    vocabulary._clear()
+    vocabularyService = vocabulary.getVocabularyRegistry()
+    vocabulary._clear()
+    vocabulary.setVocabularyRegistry(ZopeVocabularyRegistry())
+
+
+_clear()
+cleanup.addCleanUp(_clear)