[Zope3-checkins] CVS: Zope3/src/zope/products/demo/itemvocabulary -
MAINTAINER.txt:1.1 __init__.py:1.1 browser.py:1.1
configure.zcml:1.1 tests.py:1.1
Stephan Richter
srichter at cosmos.phy.tufts.edu
Sat Feb 14 19:00:06 EST 2004
Update of /cvs-repository/Zope3/src/zope/products/demo/itemvocabulary
In directory cvs.zope.org:/tmp/cvs-serv20614/itemvocabulary
Added Files:
MAINTAINER.txt __init__.py browser.py configure.zcml tests.py
Log Message:
Demo for implementing a really trivial vocabulary that points out the general
idea well. This demo was developed for the Cookbook's vocabularies recipe,
which will be online as soon as I have fixed my STX2Latex converter or Ken
created the cookbook repository.
=== Added File Zope3/src/zope/products/demo/itemvocabulary/MAINTAINER.txt ===
Stephan Richter
Email: stephan.richter at tufts.edu
IRC nick: srichter
=== Added File Zope3/src/zope/products/demo/itemvocabulary/__init__.py ===
##############################################################################
#
# Copyright (c) 2004 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.
#
##############################################################################
"""Item Vocabulary
Given a context object that implements 'IEnumerableMapping', the vocabulary
displays all items of that context object.
$Id: __init__.py,v 1.1 2004/02/15 00:00:05 srichter Exp $
"""
from zope.interface import implements
from zope.schema.interfaces import \
ITokenizedTerm, 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.keys()
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.keys():
raise LookupError, value
return ItemTerm(value)
def getTermByToken(self, token):
"""See zope.schema.interfaces.IVocabularyTokenized"""
return self.getTerm(token)
=== Added File Zope3/src/zope/products/demo/itemvocabulary/browser.py ===
##############################################################################
#
# Copyright (c) 2004 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.
#
##############################################################################
"""Item Vocabulary Views
$Id: browser.py,v 1.1 2004/02/15 00:00:05 srichter Exp $
"""
from zope.interface import implements, Interface
from zope.schema.vocabulary import VocabularyField
from zope.app.content.folder import Folder
class IDefaultItem(Interface):
default = VocabularyField(
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 File Zope3/src/zope/products/demo/itemvocabulary/configure.zcml ===
<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"/>
<allow attributes="title"/>
</content>
<!-- Sample Content Component and Views -->
<content class=".browser.DefaultItemFolder">
<require like_class="zope.app.content.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 File Zope3/src/zope/products/demo/itemvocabulary/tests.py ===
##############################################################################
#
# Copyright (c) 2004 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.
#
##############################################################################
"""Tests for the Item Vocabulary
$Id: tests.py,v 1.1 2004/02/15 00:00:05 srichter Exp $
"""
import unittest
from zope.testing.doctestunit import DocTestSuite
def test_suite():
return unittest.TestSuite((
DocTestSuite('zope.products.demo.itemvocabulary'),
))
if __name__ == '__main__':
unittest.main(defaultTest='test_suite')
More information about the Zope3-Checkins
mailing list