[Zope3-checkins] CVS: Zope3/src/zope/app/component - interface.py:1.1 globalinterfaceservice.py:NONE

Suresh Babu Eddala sbabu at zeomega.com
Fri Mar 5 10:52:58 EST 2004


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

Added Files:
	interface.py 
Removed Files:
	globalinterfaceservice.py 
Log Message:


renamed globalinterfaceservice.py  to interface.py




=== Added File Zope3/src/zope/app/component/interface.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 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.
#
##############################################################################
"""
$Id: interface.py,v 1.1 2004/03/05 15:52:57 eddala Exp $
"""
__metaclass__ = type

from zope.component.exceptions import ComponentLookupError
from zope.component import getService
from zope.interface import directlyProvides
from zope.interface.interfaces import IInterface
from zope.component.utility import utilityService
from types import ClassType
from zope.app import zapi

def provideInterface(id, interface, iface_type=None):
    """register Interface with utility service

    >>> from zope.app.tests.placelesssetup import setUp, tearDown
    >>> setUp()
    >>> utilities = zapi.getService(None, zapi.servicenames.Utilities)
    >>> from zope.interface import Interface
    >>> from zope.interface.interfaces import IInterface
    >>> from zope.app.content.interfaces import IContentType
    >>> class I(Interface):
    ...     pass
    >>> IInterface.isImplementedBy(I)
    True
    >>> IContentType.isImplementedBy(I)
    False
    >>> interfaces = utilities.getUtilitiesFor(IContentType)
    >>> interfaces
    []
    >>> provideInterface('', I, IContentType)
    >>> IContentType.isImplementedBy(I)
    True
    >>> interfaces = utilities.getUtilitiesFor(IContentType)
    >>> [name for (name, iface) in interfaces]
    ['zope.app.component.interface.I']
    >>> [iface.__name__ for (name, iface) in interfaces]
    ['I']
    >>> class I1(Interface):
    ...     pass
    >>> provideInterface('', I1)
    >>> IInterface.isImplementedBy(I1)
    True
    >>> IContentType.isImplementedBy(I1)
    False
    >>> interfaces1 = utilities.getUtilitiesFor(IContentType)
    >>> [name for (name, iface) in interfaces]
    ['zope.app.component.interface.I']
    >>> [iface.__name__ for (name, iface) in interfaces]
    ['I']
    >>> tearDown()
    
    """
    if not id:
        id = "%s.%s" % (interface.__module__, interface.__name__)

    if not IInterface.isImplementedBy(interface):
        if not isinstance(interface, (type, ClassType)):
            raise TypeError(id, "is not an interface or class")
        return

    if iface_type is not None:
        if not iface_type.extends(IInterface):
            raise TypeError(iface_type, "is not an interface type")
        directlyProvides(interface, iface_type)
    else:
        iface_type = IInterface
        
    utilityService = zapi.getService(None, zapi.servicenames.Utilities)
    utilityService.provideUtility(iface_type, interface, name=id)



def getInterface(context, id):
    """return interface or ComponentLookupError

    >>> from zope.app.tests.placelesssetup import setUp, tearDown
    >>> setUp()
    >>> utilities = zapi.getService(None, zapi.servicenames.Utilities)
    >>> from zope.interface import Interface
    >>> from zope.app.content.interfaces import IContentType
    >>> class I4(Interface):
    ...     pass
    >>> IInterface.isImplementedBy(I4)
    True
    >>> IContentType.isImplementedBy(I4)
    False
    >>> getInterface(None, 'zope.app.component.interface.I4')
    Traceback (most recent call last):
    ...
    ComponentLookupError: zope.app.component.interface.I4
    >>> provideInterface('', I4, IContentType)
    >>> IContentType.isImplementedBy(I4)
    True
    >>> iface = queryInterface( """\
                """ 'zope.app.component.interface.I4')
    >>> iface.__name__
    'I4'
    >>> tearDown()
    
    """
    iface = queryInterface(id, None)
    if iface is None:
        raise ComponentLookupError(id)
    return iface

def queryInterface(id, default=None):
    """return interface or None

    >>> from zope.app.tests.placelesssetup import setUp, tearDown
    >>> tearDown()
    >>> setUp()
    >>> utilities = zapi.getService(None, zapi.servicenames.Utilities)
    >>> from zope.interface import Interface
    >>> from zope.interface.interfaces import IInterface
    >>> from zope.app.content.interfaces import IContentType
    >>> class I3(Interface):
    ...     pass
    >>> IInterface.isImplementedBy(I3)
    True
    >>> IContentType.isImplementedBy(I3)
    False
    >>> queryInterface('zope.app.component.interface.I3')
    
    >>> provideInterface('', I3, IContentType)
    >>> IContentType.isImplementedBy(I3)
    True
    >>> iface = queryInterface('zope.app.component.interface.I3')
    >>> iface.__name__
    'I3'
    >>> tearDown()
    
    """
    
    return zapi.queryUtility(None, IInterface, default, id)

def searchInterface(context, search_string=None, base=None):
    """Interfaces search

    >>> from zope.app.tests.placelesssetup import setUp, tearDown
    >>> setUp()
    >>> utilities = zapi.getService(None, zapi.servicenames.Utilities)
    >>> from zope.interface import Interface
    >>> from zope.interface.interfaces import IInterface
    >>> from zope.app.content.interfaces import IContentType
    >>> class I5(Interface):
    ...     pass
    >>> IInterface.isImplementedBy(I5)
    True
    >>> IContentType.isImplementedBy(I5)
    False
    >>> searchInterface(None, 'zope.app.component.interface.I5')
    []
    >>> provideInterface('', I5, IContentType)
    >>> IContentType.isImplementedBy(I5)
    True
    >>> iface = searchInterface(None,
    ...                        'zope.app.component.interface.I5')
    >>> iface[0].__name__
    'I5'
    >>> tearDown()

    """

    return [iface_util[1]
            for iface_util in
            searchInterfaceUtilities(context, search_string, base)]


def searchInterfaceIds(context, search_string=None, base=None):
    """Interfaces search

    >>> from zope.app.tests.placelesssetup import setUp, tearDown
    >>> setUp()
    >>> utilities = zapi.getService(None, zapi.servicenames.Utilities)
    >>> from zope.interface import Interface
    >>> from zope.interface.interfaces import IInterface
    >>> from zope.app.content.interfaces import IContentType
    >>> class I5(Interface):
    ...     pass
    >>> IInterface.isImplementedBy(I5)
    True
    >>> IContentType.isImplementedBy(I5)
    False
    >>> searchInterface(None, 'zope.app.component.interface.I5')
    []
    >>> provideInterface('', I5, IContentType)
    >>> IContentType.isImplementedBy(I5)
    True
    >>> iface = searchInterfaceIds(None,
    ...                        'zope.app.component.interface.I5')
    >>> iface
    ['zope.app.component.interface.I5']
    >>> tearDown()

    """

    return [iface_util[0]
            for iface_util in
            searchInterfaceUtilities(context, search_string, base)]


def searchInterfaceUtilities(context, search_string=None, base=None):
    utilityService = zapi.getService(None, zapi.servicenames.Utilities)   
    iface_utilities = utilityService.getUtilitiesFor(IInterface)

    if search_string:
        search_string = search_string.lower()
        iface_utilities = [iface_util for iface_util in iface_utilities
                           if (getInterfaceAllDocs(iface_util[1]).\
                               find(search_string) >= 0)]
    if base:
        res = [iface_util for iface_util in iface_utilities
               if iface_util[1].extends(base)]
    else:
        res = [iface_util for iface_util in iface_utilities]
    return res


def getInterfaceAllDocs(interface):
    iface_id = '%s.%s' %(interface.__module__, interface.__name__)
    docs = [str(iface_id).lower(),
            str(interface.__doc__).lower()]

    if IInterface.isImplementedBy(interface):
        for name in interface:
            docs.append(
                str(interface.getDescriptionFor(name).__doc__).lower())

    return '\n'.join(docs)


def nameToInterface(context, id):
    if id == 'None':
        return None
    iface = getInterface(context, id)
    return iface


=== Removed File Zope3/src/zope/app/component/globalinterfaceservice.py ===




More information about the Zope3-Checkins mailing list