[Zodb-checkins] CVS: Zope3/lib/python/Interface/Registry - AdapterRegistry.py:1.3 IAdapterRegistry.py:1.3
Jim Fulton
jim@zope.com
Mon, 18 Nov 2002 15:40:18 -0500
Update of /cvs-repository/Zope3/lib/python/Interface/Registry
In directory cvs.zope.org:/tmp/cvs-serv3952
Modified Files:
AdapterRegistry.py IAdapterRegistry.py
Log Message:
Modified the constructor to take a mapping object to store internal
data. This was needed so that persistent subclasses could pass
persistent dictionaries.
Added a filter option to get and getForObject. Search will continue
until data that matches the filter is found.
Added getRegistered and getRegisteredMatching. (See IAdapterRegistry.)
=== Zope3/lib/python/Interface/Registry/AdapterRegistry.py 1.2 => 1.3 ===
--- Zope3/lib/python/Interface/Registry/AdapterRegistry.py:1.2 Thu Aug 1 12:06:41 2002
+++ Zope3/lib/python/Interface/Registry/AdapterRegistry.py Mon Nov 18 15:40:16 2002
@@ -38,8 +38,10 @@
# Where the registered provides is what was registered and
# provided may be some base interface
- def __init__(self):
- self._reg = {}
+ def __init__(self, data=None):
+ if data is None:
+ data = {}
+ self._reg = data
def _registerAllProvided(self, require, primary_provide, object, provide):
# Registers a component using (require, provide) as a key.
@@ -75,21 +77,82 @@
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 = self._reg.get((None, provide), default)
- if c:
- return c[1]
-
- return c
-
- def getForObject(self, object, interface):
- return self.get((getattr(object, '__implements__', None), interface))
+ c = c[1]
+ if filter is None:
+ return c
+ if filter(c):
+ return c
+
+ return default
+
+ 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))
+ if data:
+ registered_provide, object = data
+ if registered_provide == provide:
+ return object
+ return None
+
+ def getRegisteredMatching(self,
+ required_interfaces=None,
+ provided_interfaces=None):
+
+
+ if provided_interfaces:
+ r = {}
+
+ if required_interfaces:
+ # Both specified
+ for required in _flatten(required_interfaces, 1):
+ for provided in provided_interfaces:
+ v = self._reg.get((required, provided))
+ if v:
+ rprovided, o = v
+ r[required, rprovided] = o
+
+
+ else:
+ # Only provided specified
+ for (required, provided), (rprovided, o) in self._reg.items():
+ for p in provided_interfaces:
+ if provided.extends(p, 0):
+ r[required, rprovided] = o
+ break
+
+ return [(required, provided, o)
+ for ((required, provided), o) in r.items()]
+
+
+ elif required_interfaces:
+ # Just required specified
+ required_interfaces = _flatten(required_interfaces, 1)
+ return [(required, provided, o)
+ for (required, provided), (rprovided, o)
+ in self._reg.items()
+ if ((required in required_interfaces)
+ and
+ provided == rprovided
+ )
+ ]
+
+ else:
+ # Nothing specified
+ return [(required, provided, o)
+ for (required, provided), (rprovided, o)
+ in self._reg.items()
+ if provided == rprovided
+ ]
+
+
=== Zope3/lib/python/Interface/Registry/IAdapterRegistry.py 1.2 => 1.3 ===
--- Zope3/lib/python/Interface/Registry/IAdapterRegistry.py:1.2 Thu Aug 1 11:37:08 2002
+++ Zope3/lib/python/Interface/Registry/IAdapterRegistry.py Mon Nov 18 15:40:16 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,9 +65,49 @@
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):
+ """return data registred specificly for the given interfaces
"""
+ def getRegisteredMatching(required_interfaces=None,
+ provided_interfaces=None):
+ """Return information about registered data
+
+ Zero or more required and provided interfaces may be
+ specified. Registration information matching any of the
+ specified interfaces is returned. The returned value is a
+ sequence of three-element tuples:
+
+ - required interface
+
+ - provided interface
+
+ - the object registered specifically for the required and
+ provided interfaces.
+
+ To understand hopw the matching works, imagine that we have
+ interfaces R1, R2, P1, and P2. R2 extends R1. P2 extends P1.
+ We've registered C to require R1 and provide P2. Given this,
+ if we call getRegisteredMatching:
+
+ registery.getRegisteredMatching([R2], [P1])
+
+ the returned value will include:
+
+ (R1, P2, C)
+ """