[Zope-CVS] SVN: book/trunk/itemvocabulary/ Copied from core and updated to latest API.

Stephan Richter srichter at cosmos.phy.tufts.edu
Mon Aug 23 11:11:14 EDT 2004


Log message for revision 27226:
  Copied from core and updated to latest API.
  


Changed:
  A   book/trunk/itemvocabulary/
  A   book/trunk/itemvocabulary/__init__.py
  A   book/trunk/itemvocabulary/browser.py
  A   book/trunk/itemvocabulary/configure.zcml
  A   book/trunk/itemvocabulary/tests.py


-=-
Added: book/trunk/itemvocabulary/__init__.py
===================================================================
--- book/trunk/itemvocabulary/__init__.py	2004-08-23 12:50:40 UTC (rev 27225)
+++ book/trunk/itemvocabulary/__init__.py	2004-08-23 15:11:13 UTC (rev 27226)
@@ -0,0 +1,97 @@
+##############################################################################
+#
+# Copyright (c) 2004 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (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.
+#
+##############################################################################
+"""Item Vocabulary
+
+Given a context object that implements `IEnumerableMapping`, the vocabulary
+displays all items of that context object. 
+
+$Id: __init__.py 26729 2004-07-23 21:20:21Z pruggera $
+"""
+__docformat__ = 'restructuredtext'
+
+from zope.interface import implements
+from zope.schema.interfaces import ITokenizedTerm
+from zope.schema.interfaces import IVocabulary, IVocabularyTokenized
+from zope.interface.common.mapping import IEnumerableMapping
+
+
+class ItemTerm(object):
+    """A simple term implementation for items."""
+    implements(ITokenizedTerm)
+    def __init__(self, value):
+        self.value = self.token = value
+
+
+class ItemVocabulary(object):
+    """A vocabulary that provides the keys of any `IEnumerableMapping` object.
+
+    Every dictionary will qualify for this vocabulary.
+
+    Example:
+
+    >>> data = {'a': 'Anton', 'b': 'Berta', 'c': 'Charlie'}
+    >>> vocab = ItemVocabulary(data)
+    >>> iterator = iter(vocab)
+    >>> iterator.next().token
+    'a'
+    >>> len(vocab)
+    3
+    >>> 'c' in vocab
+    True
+    >>> vocab.getQuery() is None
+    True
+    >>> vocab.getTerm('b').value
+    'b'
+    >>> vocab.getTerm('d')
+    Traceback (most recent call last):
+    ...
+    LookupError: d
+    >>> vocab.getTermByToken('b').token
+    'b'
+    >>> vocab.getTermByToken('d')
+    Traceback (most recent call last):
+    ...
+    LookupError: d
+    """
+    implements(IVocabulary, IVocabularyTokenized)
+    __used_for__ = IEnumerableMapping
+
+    def __init__(self, context):
+        self.context = context
+    
+    def __iter__(self):
+        """See zope.schema.interfaces.IIterableVocabulary"""
+        return iter([ItemTerm(key) for key in self.context.keys()])
+    
+    def __len__(self):
+        """See zope.schema.interfaces.IIterableVocabulary"""
+        return len(self.context)
+    
+    def __contains__(self, value):
+        """See zope.schema.interfaces.IBaseVocabulary"""
+        return value in self.context
+
+    def getQuery(self):
+        """See zope.schema.interfaces.IBaseVocabulary"""
+        return None
+    
+    def getTerm(self, value):
+        """See zope.schema.interfaces.IBaseVocabulary"""
+        if value not in self.context:
+            raise LookupError, value
+        return ItemTerm(value)
+
+    def getTermByToken(self, token):
+        """See zope.schema.interfaces.IVocabularyTokenized"""
+        return self.getTerm(token)

Added: book/trunk/itemvocabulary/browser.py
===================================================================
--- book/trunk/itemvocabulary/browser.py	2004-08-23 12:50:40 UTC (rev 27225)
+++ book/trunk/itemvocabulary/browser.py	2004-08-23 15:11:13 UTC (rev 27226)
@@ -0,0 +1,35 @@
+##############################################################################
+#
+# Copyright (c) 2004 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (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.
+#
+##############################################################################
+"""Item Vocabulary Views
+
+$Id: browser.py 26729 2004-07-23 21:20:21Z pruggera $
+"""
+__docformat__ = 'restructuredtext'
+
+from zope.interface import implements, Interface
+from zope.schema import Choice
+from zope.app.folder import Folder
+
+class IDefaultItem(Interface):
+
+    default = Choice(
+        title=u"Default Item Key",
+        description=u"Key of the default item in the folder.",
+        vocabulary="Items")
+
+class DefaultItemFolder(Folder):
+    implements(IDefaultItem)
+
+    default = None
+    

Added: book/trunk/itemvocabulary/configure.zcml
===================================================================
--- book/trunk/itemvocabulary/configure.zcml	2004-08-23 12:50:40 UTC (rev 27225)
+++ book/trunk/itemvocabulary/configure.zcml	2004-08-23 15:11:13 UTC (rev 27226)
@@ -0,0 +1,46 @@
+<configure
+    xmlns="http://namespaces.zope.org/zope"
+    xmlns:browser="http://namespaces.zope.org/browser"
+    i18n_domain="itemvocabulary">
+
+<vocabulary
+    name="Items"
+    factory=".ItemVocabulary" />
+
+<content class=".ItemVocabulary">
+  <allow interface="zope.schema.interfaces.IVocabulary"/>
+  <allow interface="zope.schema.interfaces.IVocabularyTokenized"/>
+</content>
+
+<content class=".ItemTerm">
+  <allow interface="zope.schema.interfaces.ITokenizedTerm"/>
+</content>
+
+<!-- Sample Content Component and Views -->
+
+<content class=".browser.DefaultItemFolder">
+  <require like_class="zope.app.folder.Folder"/>    
+
+  <require
+      permission="zope.View"
+      interface=".browser.IDefaultItem" />
+
+  <require
+      permission="zope.ManageContent"
+      set_schema=".browser.IDefaultItem" />
+</content>
+
+<browser:addMenuItem
+    class=".browser.DefaultItemFolder"
+    title="Default Item Folder"
+    permission="zope.ManageContent" />
+
+<browser:editform
+    schema=".browser.IDefaultItem"
+    for=".browser.IDefaultItem"
+    label="Change Default Item"
+    name="defaultItem.html"
+    permission="zope.ManageContent"
+    menu="zmi_views" title="Default Item" />
+
+</configure>

Added: book/trunk/itemvocabulary/tests.py
===================================================================
--- book/trunk/itemvocabulary/tests.py	2004-08-23 12:50:40 UTC (rev 27225)
+++ book/trunk/itemvocabulary/tests.py	2004-08-23 15:11:13 UTC (rev 27226)
@@ -0,0 +1,27 @@
+##############################################################################
+#
+# Copyright (c) 2004 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (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.
+#
+##############################################################################
+"""Tests for the Item Vocabulary
+
+$Id: tests.py 25177 2004-06-02 13:17:31Z jim $
+"""
+import unittest
+from zope.testing.doctestunit import DocTestSuite
+
+def test_suite():
+    return unittest.TestSuite((
+        DocTestSuite('book.itemvocabulary'),
+        ))
+
+if __name__ == '__main__':
+    unittest.main(defaultTest='test_suite')



More information about the Zope-CVS mailing list