Back to index

plone3  3.1.7
vocabulary.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2006 Zope Corporation and Contributors. All Rights Reserved.
00004 #
00005 # This software is subject to the provisions of the Zope Public License,
00006 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
00007 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00008 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00009 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00010 # FOR A PARTICULAR PURPOSE.
00011 #
00012 ##############################################################################
00013 """Formlib schema vocabulary base classes.
00014 
00015 $Id: vocabulary.py 72362 2007-02-04 15:31:51Z yuppie $
00016 """
00017 
00018 from zope.interface import implements
00019 from zope.schema.interfaces import IVocabularyFactory
00020 from zope.schema.vocabulary import SimpleVocabulary
00021 
00022 
00023 class SimpleVocabulary(SimpleVocabulary):
00024 
00025     def fromTitleItems(cls, items, *interfaces):
00026         """Construct a vocabulary from a list of (token, value, title) tuples.
00027         """
00028         terms = [ cls.createTerm(value, token, title)
00029                   for (token, value, title) in items ]
00030         return cls(terms, *interfaces)
00031 
00032     fromTitleItems = classmethod(fromTitleItems)
00033 
00034 
00035 class StaticVocabulary(object):
00036 
00037     """Vocabulary factory for static items.
00038     """
00039 
00040     implements(IVocabularyFactory)
00041 
00042     def __init__(self, items):
00043         self._vocabulary = SimpleVocabulary.fromTitleItems(items)
00044 
00045     def __call__(self, context):
00046         return self._vocabulary