[Zope3-checkins] CVS: Zope3/lib/python/Interface/Registry - AdapterRegistry.py:1.2.8.2 IAdapterRegistry.py:1.2.8.2 _flatten.py:1.1.8.1
Jim Fulton
jim@zope.com
Wed, 16 Oct 2002 04:35:36 -0400
Update of /cvs-repository/Zope3/lib/python/Interface/Registry
In directory cvs.zope.org:/tmp/cvs-serv11955/lib/python/Interface/Registry
Modified Files:
Tag: Zope3-Bangalore-TTW-Branch
AdapterRegistry.py IAdapterRegistry.py _flatten.py
Log Message:
- Added a filter option to get and getForObject. This will be
needed for placeful adapter service implementations.
- Added tests for getForObject.
- Fixed a logic error in get.
=== Zope3/lib/python/Interface/Registry/AdapterRegistry.py 1.2.8.1 => 1.2.8.2 ===
--- Zope3/lib/python/Interface/Registry/AdapterRegistry.py:1.2.8.1 Tue Oct 15 10:41:30 2002
+++ Zope3/lib/python/Interface/Registry/AdapterRegistry.py Wed Oct 16 04:35:35 2002
@@ -77,24 +77,25 @@
self._registerAllProvided(require, provide, object, provide)
- def get(self, (ob_interface, provide), default=None):
+ def get(self, (ob_interface, provide), default=None, filter=None):
"""
Finds a registered component that provides the given interface.
Returns None if not found.
"""
- for interface in _flatten(ob_interface):
+ for interface in _flatten(ob_interface, 1):
c = self._reg.get((interface, provide))
if c:
- return c[1]
+ c = c[1]
+ if filter is None:
+ return c
+ if filter(c):
+ return c
- c = self._reg.get((None, provide), default)
- if c:
- return c[1]
+ return default
- return c
-
- def getForObject(self, object, interface):
- return self.get((getattr(object, '__implements__', None), interface))
+ def getForObject(self, object, interface, filter=None):
+ return self.get((getattr(object, '__implements__', None), interface),
+ filter=filter)
def getRegistered(self, require, provide):
data = self._reg.get((require, provide))
=== Zope3/lib/python/Interface/Registry/IAdapterRegistry.py 1.2.8.1 => 1.2.8.2 ===
--- Zope3/lib/python/Interface/Registry/IAdapterRegistry.py:1.2.8.1 Tue Oct 15 10:41:30 2002
+++ Zope3/lib/python/Interface/Registry/IAdapterRegistry.py Wed Oct 16 04:35:35 2002
@@ -50,7 +50,7 @@
"""
- def get((implements, provides), default=None):
+ def get((implements, provides), default=None, filter=None):
"""Return a registered object
The registered object is one that was registered to require an
@@ -65,10 +65,19 @@
Note that the implements may be None, it which case a
component will be returned only if it was registered with a
require of None.
+
+ An optional filter may be provided. If provided, the returned
+ object must pass the filter. Search will continue until a
+ suitable match can be found. The filter should take a single
+ argument and return a true value if the object passes the
+ filter, or false otherwise.
+
"""
- def getForObject(object, interface):
+ def getForObject(object, interface, filter=None):
"""Get an adapter for object that implements the specified interface
+
+ The filter option has the same meaning as in the get method.
"""
def getRegistered(require, provide):
=== Zope3/lib/python/Interface/Registry/_flatten.py 1.1 => 1.1.8.1 ===
--- Zope3/lib/python/Interface/Registry/_flatten.py:1.1 Thu Aug 1 11:33:43 2002
+++ Zope3/lib/python/Interface/Registry/_flatten.py Wed Oct 16 04:35:35 2002
@@ -21,7 +21,7 @@
from Interface import Interface
-def _flatten(implements):
+def _flatten(implements, include_None=0):
"""Flatten an implements spec to a list of interfaces
The list includes all base interfaces of the interface(s) in
@@ -39,6 +39,9 @@
seen[interface] = 1
flattened.append(interface)
flattened.reverse()
+
+ if include_None:
+ flattened.append(None)
return flattened