[Zope3-checkins] CVS: Zope3/lib/python/Zope/App/OFS/Services - adapter.py:1.1.2.1 interfaces.py:1.1.2.1

Jim Fulton jim@zope.com
Tue, 10 Dec 2002 09:38:37 -0500


Update of /cvs-repository/Zope3/lib/python/Zope/App/OFS/Services
In directory cvs.zope.org:/tmp/cvs-serv16226

Added Files:
      Tag: AdapterAndView-branch
	adapter.py interfaces.py 
Log Message:
Added local adapter service Python code.

=== Added File Zope3/lib/python/Zope/App/OFS/Services/adapter.py ===
##############################################################################
#
# Copyright (c) 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.
# 
##############################################################################
"""Adapter Service


$Id: adapter.py,v 1.1.2.1 2002/12/10 14:38:36 jim Exp $
"""
__metaclass__ = type

from Interface.Registry.AdapterRegistry import AdapterRegistry
from Persistence import Persistent
from Persistence.PersistentDict import PersistentDict
from Zope.ComponentArchitecture.IAdapterService import IAdapterService
from Zope.ComponentArchitecture.Exceptions import ComponentLookupError
from Zope.App.OFS.Services.ConfigurationInterfaces import IConfigurable
from Zope.App.OFS.Services.Configuration import ConfigurationRegistry
from Zope.Proxy.ContextWrapper import ContextWrapper
from Zope.ContextWrapper import ContextMethod

class PersistentAdapterRegistry(Persistent, AdapterRegistry):
    
    def __init__(self):
        AdapterRegistry.__init__(self, PersistentDict())


class AdapterService(Persistent):

    __implements__ = IAdapterService, IConfigurable

    def __init__(self):
        self._registry = PersistentAdapterRegistry()

    def queryConfigurationsFor(self, configuration, default=None):
        "See Zope.App.OFS.Services.ConfigurationInterfaces.IConfigurable"
        return self.queryConfigurations(
            configuration.forInterface, configuration.providedInterface,
            default)

    queryConfigurationsFor = ContextMethod(queryConfigurationsFor)

    def queryConfigurations(self,
                            forInterface, providedInterface, default=None):
        registry = self._registry.getRegistered(
            forInterface, providedInterface)
        if registry is None:
            return default
        return ContextWrapper(registry, self)

    queryConfigurations = ContextMethod(queryConfigurations)
    
    def createConfigurationsFor(self, configuration):
        "See Zope.App.OFS.Services.ConfigurationInterfaces.IConfigurable"
        return self.createConfigurations(
            configuration.forInterface, configuration.providedInterface)

    createConfigurationsFor = ContextMethod(createConfigurationsFor)

    def createConfigurations(self, forInterface, providedInterface):
        registry = self._registry.getRegistered(forInterface,
                                                providedInterface)
        if registry is None:
            registry = ConfigurationRegistry()
            self._registry.register(forInterface, providedInterface, registry)
        return ContextWrapper(registry, self)

    createConfigurations = ContextMethod(createConfigurations)

    def getAdapter(self, object, interface):
        "See Zope.ComponentArchitecture.IAdapterService.IAdapterService"
        adapter = self.queryAdapter(object, interface)
        if adapter is None:
            raise ComponentLookupError(object, interface)
        return adapter

    getAdapter = ContextMethod(getAdapter)

    def queryAdapter(self, object, interface, default=None):
        "See Zope.ComponentArchitecture.IAdapterService.IAdapterService"
        if interface.isImplementedBy(object):
            return object
        
        registry = self._registry.getForObject(
            object, interface,
            filter = lambda registry: ContextWrapper(registry, self).active(),
            )

        if registry is not None:
            registry = ContextWrapper(registry, self)
            adapter = registry.active().getAdapter(object)
            return adapter

        return default

    queryAdapter = ContextMethod(queryAdapter)


=== Added File Zope3/lib/python/Zope/App/OFS/Services/interfaces.py ===
##############################################################################
#
# Copyright (c) 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.
# 
##############################################################################
"""Service interfaces

$Id: interfaces.py,v 1.1.2.1 2002/12/10 14:38:36 jim Exp $
"""

from ConfigurationInterfaces import IConfiguration

class IAdapterConfiguration(IConfiguration):

    def getAdapter(object):
        """Return an adapter for the object

        The adapter is computed by passing the object to the
        registered factory.
        """