[Zope3-checkins] CVS: Zope3/src/zope/app/utility - vocabulary.py:1.2

Stephan Richter srichter at cosmos.phy.tufts.edu
Mon Apr 26 19:58:41 EDT 2004


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

Modified Files:
	vocabulary.py 
Log Message:


Added optional 'nameOnly' argument to constuctor of utility vocabulary, so
that it is possible to have a vicab that only provides the names of the
utilities.


=== Zope3/src/zope/app/utility/vocabulary.py 1.1 => 1.2 ===
--- Zope3/src/zope/app/utility/vocabulary.py:1.1	Sat Apr 24 19:17:44 2004
+++ Zope3/src/zope/app/utility/vocabulary.py	Mon Apr 26 19:58:41 2004
@@ -180,15 +180,26 @@
     [(u'object1', <UtiltiyTerm object1, instance of Object>),
      (u'object2', <UtiltiyTerm object2, instance of Object>),
      (u'object3', <UtiltiyTerm object3, instance of Object>)]
+
+    Sometimes it is desirable to only select the name of a utility. For
+    this purpose a 'nameOnly' argument was added to the constructor, in which
+    case the UtilityTerm's value is not the utility itself but the name of the
+    utility.  
+
+    >>> vocab = UtilityVocabulary(None, IObject, nameOnly=True)
+    >>> pprint.pprint([term.value for term in vocab])
+    [u'object1', u'object2', u'object3']
     """
 
     implements(IVocabulary, IVocabularyTokenized)
 
-    def __init__(self, context, interface):
+    def __init__(self, context, interface, nameOnly=False):
+        if nameOnly is not False:
+            nameOnly = True
         if isinstance(interface, (str, unicode)): 
             interface = zapi.getUtility(context, IInterface, interface)
         utils = zapi.getUtilitiesFor(context, interface)
-        self._terms = dict([(name, UtilityTerm(util, name))
+        self._terms = dict([(name, UtilityTerm(nameOnly and name or util, name))
                             for name, util in utils])
       
     def __contains__(self, value):




More information about the Zope3-Checkins mailing list