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

Fred L. Drake, Jr. fred@zope.com
Thu, 8 May 2003 10:46:01 -0400


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

Added Files:
      Tag: schema-vocabulary-branch
	configure.zcml meta.zcml vocabulary.py 
Log Message:
- implement a vocabulary registry that cooperates with the component
  architecture to all local vocabulary services
- add a ZCML directive in the main zope namespace that registers named
  vocabularies in the global registry


=== Added File Zope3/src/zope/app/schema/configure.zcml ===
<zopeConfigure xmlns='http://namespaces.zope.org/zope'>

<serviceType id='Vocabularies'
             interface='zope.schema.vocabulary.IVocabularyRegistry' />

<service serviceType='Vocabularies'
         permission='zope.Public'
         component='zope.app.schema.vocabulary.vocabularyService' />

</zopeConfigure>


=== Added File Zope3/src/zope/app/schema/meta.zcml ===
<zopeConfigure xmlns='http://namespaces.zope.org/zope'>
<directives namespace="http://namespaces.zope.org/browser">

  <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.
    </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>


=== Added File Zope3/src/zope/app/schema/vocabulary.py ===
##############################################################################
#
# 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


def register(_context, name, factory):
    factory = _context.resolve(factory)
    return [
        Action(discriminator=('defineVocabulary', name),
               callable=vocabularyService.register,
               args=(name, factory))
        ]


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)


# This can only be done once; more than that causes confusion!
try:
    vocabularyService
except NameError:
    vocabularyService = vocabulary.getVocabularyRegistry()
    vocabulary._clear()
    vocabulary.setVocabularyRegistry(ZopeVocabularyRegistry())