[Zope3-checkins] CVS: Zope3/src/zope/component - adapter.py:1.5
Jim Fulton
cvs-admin at zope.org
Fri Nov 21 12:09:25 EST 2003
Update of /cvs-repository/Zope3/src/zope/component
In directory cvs.zope.org:/tmp/cvs-serv29503/src/zope/component
Modified Files:
adapter.py
Log Message:
Reimplemented the adapter service using surrogates.
Also simplified API. The service only provides query methods.
Also, queryAdapter no-longer checks whether an object already
implements an interface. That is done by outer apis.
=== Zope3/src/zope/component/adapter.py 1.4 => 1.5 ===
--- Zope3/src/zope/component/adapter.py:1.4 Tue Jun 3 10:18:45 2003
+++ Zope3/src/zope/component/adapter.py Fri Nov 21 12:09:25 2003
@@ -17,141 +17,49 @@
__metaclass__ = type
import sys
from zope.interface import implements
-from zope.interface.adapter import AdapterRegistry
+from zope.interface.surrogate import SurrogateRegistry
from zope.component.exceptions import ComponentLookupError
-from zope.component.interfaces import IGlobalAdapterService
+from zope.component.interfaces import IAdapterService
+from zope.component.service import GlobalService
import warnings
-class GlobalAdapterService:
+class IGlobalAdapterService(IAdapterService):
- implements(IGlobalAdapterService)
+ def provideAdapter(required, provided, factories, name='', with=()):
+ """Provide an adapter
+
+ An adapter provides an interface for objects that have another
+ interface.
+
+ Arguments:
+
+ forInterface -- The interface the adapter provides an interface for.
+
+ providedInterface -- The provided interface
+
+ maker -- a sequence of factories that are used to create the adapter.
+ The first factory is called with the object to be adapted, subsequent
+ factories are called with the results of the previous factory.
+ """
+
+ def getRegisteredMatching(required=None,
+ provided=None,
+ name=None,
+ with=None):
+ """Return information about registered data
- def __init__(self):
- self.__adapters = {}
+ A five-tuple is returned containing:
- def provideAdapter(self, forInterface, providedInterface, maker, name=''):
- """see IGlobalAdapterService interface"""
+ - registered name,
+
+ - registered for interface
+
+ - registered provided interface, and
+
+ - registered data
+ """
+
+class GlobalAdapterService(SurrogateRegistry, GlobalService):
+
+ implements(IGlobalAdapterService)
- if not isinstance(maker, (list, tuple)):
- maker = [maker]
- else:
- maker = list(maker)
-
- if not maker == filter(callable, maker):
- raise TypeError("The registered component callable is not "
- "callable")
-
- registry = self.__adapters.get(name)
- if registry is None:
- registry = AdapterRegistry()
- self.__adapters[name] = registry
-
- registry.register(forInterface, providedInterface, maker)
-
- def getAdapter(self, object, interface, name=''):
- """see IAdapterService interface"""
- result = self.queryAdapter(object, interface, name=name)
- if result is None:
- raise ComponentLookupError(object, interface)
-
- return result
-
- def getNamedAdapter(self, object, interface, name):
- """see IAdapterService interface"""
- result = self.queryNamedAdapter(object, interface, name)
- if result is None:
- raise ComponentLookupError(object, interface)
-
- return result
-
-
- def queryAdapter(self, object, interface, default=None, name=''):
- """see IAdapterService interface"""
- if name:
- warnings.warn("The name argument to queryAdapter is deprecated",
- DeprecationWarning, 2)
- return self.queryNamedAdapter(object, interface, name, default)
-
- conform = getattr(object, '__conform__', None)
- if conform is not None:
- try:
- adapter = conform(interface)
- except TypeError:
- # We got a TypeError. It might be an error raised by
- # the __conform__ implementation, or *we* may have
- # made the TypeError by calling an unbound method
- # (object is a class). In the later case, we behave
- # as though there is no __conform__ method. We can
- # detect this case by checking whether there is more
- # than one traceback object in the traceback chain:
- if sys.exc_info()[2].tb_next is not None:
- # There is more than one entry in the chain, so
- # reraise the error:
- raise
- # This clever trick is from Phillip Eby
- else:
- if adapter is not None:
- return adapter
-
- if interface.isImplementedBy(object):
- return object
-
- return self.queryNamedAdapter(object, interface, name, default)
-
- def queryNamedAdapter(self, object, interface, name, default=None):
- """see IAdapterService interface"""
- registry = self.__adapters.get(name)
- if registry is None:
- return default
-
- makers = registry.getForObject(object, interface)
-
- if makers is None:
- return default
-
- result = object
- for maker in makers:
- result = maker(result)
-
- return result
-
- def getRegisteredMatching(self,
- for_interfaces=None,
- provided_interfaces=None,
- name = None,
- ):
-
- if name is not None:
- registry = self.__adapters.get(name)
- if registry is None:
- return ()
- return [(name, for_, provided, data)
- for (for_, provided, data)
- in registry.getRegisteredMatching(for_interfaces,
- provided_interfaces)
- ]
-
- result = []
- for name in self.__adapters:
- r = self.getRegisteredMatching(
- for_interfaces, provided_interfaces, name)
- result.extend(r)
-
- return result
-
- _clear = __init__
-
-# the global adapter service instance (see component.zcml )
-adapterService = GlobalAdapterService()
-provideAdapter = adapterService.provideAdapter
-
-_clear = adapterService._clear
-
-# Register our cleanup with Testing.CleanUp to make writing unit tests simpler.
-try:
- from zope.testing.cleanup import addCleanUp
-except ImportError:
- pass
-else:
- addCleanUp(_clear)
- del addCleanUp
More information about the Zope3-Checkins
mailing list