[Zope3-checkins] CVS: Zope3/src/zope/app/interface - vocabulary.py:1.1 configure.zcml:1.2

Stephan Richter srichter at cosmos.phy.tufts.edu
Sat Apr 24 19:18:07 EDT 2004


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

Modified Files:
	configure.zcml 
Added Files:
	vocabulary.py 
Log Message:


Vocabulary to provide the context's provided interfaces.




=== Added File Zope3/src/zope/app/interface/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.
#
##############################################################################
"""Vocabulary that provides a list of all interfaces its context provides.

$Id: vocabulary.py,v 1.1 2004/04/24 23:17:35 srichter Exp $
"""
from zope.interface import providedBy
from zope.security.proxy import trustedRemoveSecurityProxy
from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm 
from zope.app.introspector import interfaceToName


class ObjectInterfacesVocabulary(SimpleVocabulary):
    """A vocabulary that provides a list of all interfaces that its context
    provides.

    Here a quick demonstration:

    >>> from zope.interface import Interface, implements
    >>> class I1(Interface):
    ...     pass
    >>> class I2(Interface):
    ...     pass
    >>> class I3(I2):
    ...     pass

    >>> class Object(object):
    ...     implements(I3, I1)

    >>> vocab = ObjectInterfacesVocabulary(Object())
    >>> import pprint
    >>> names = [term.token for term in vocab._terms]
    >>> names.sort()
    >>> pprint.pprint(names)
    ['zope.app.interface.vocabulary.I1',
     'zope.app.interface.vocabulary.I2',
     'zope.app.interface.vocabulary.I3',
     'zope.interface.Interface']
    """

    def __init__(self, context):
        component = trustedRemoveSecurityProxy(context)
        interfaces = providedBy(component).flattened()
        terms = [SimpleTerm(interface, interfaceToName(context, interface))
                 for interface in interfaces]
        super(ObjectInterfacesVocabulary, self).__init__(terms)


=== Zope3/src/zope/app/interface/configure.zcml 1.1 => 1.2 ===
--- Zope3/src/zope/app/interface/configure.zcml:1.1	Thu Mar 11 07:38:37 2004
+++ Zope3/src/zope/app/interface/configure.zcml	Sat Apr 24 19:17:35 2004
@@ -19,4 +19,8 @@
       alias="zope.app.interfaces.services.interface"
       />
 
+  <vocabulary
+      name="Object Interfaces"
+      factory=".vocabulary.ObjectInterfacesVocabulary" />
+
 </configure>




More information about the Zope3-Checkins mailing list