[Zope3-checkins] SVN: Zope3/trunk/src/zope/interface/adapter. Added an adapter_hook method to make it easier (and more efficient) to

Jim Fulton jim at zope.com
Sat Jul 10 08:44:33 EDT 2004


Log message for revision 26389:
Added an adapter_hook method to make it easier (and more efficient) to
into interface call.



-=-
Modified: Zope3/trunk/src/zope/interface/adapter.py
===================================================================
--- Zope3/trunk/src/zope/interface/adapter.py	2004-07-10 02:50:25 UTC (rev 26388)
+++ Zope3/trunk/src/zope/interface/adapter.py	2004-07-10 12:44:33 UTC (rev 26389)
@@ -378,13 +378,25 @@
 
         return value
 
-    def queryAdapter(self, object, interface, name='', default=None):
+    def adapter_hook(self, interface, object, name='', default=None):
+        """Hook function used when calling interfaces.
+
+        When called from Interface.__adapt__, only the interface and
+        object parameters will be passed.
+        
+        """
         factory = self.lookup1(providedBy(object), interface, name)
         if factory is not None:
             return factory(object)
 
         return default
 
+    def queryAdapter(self, object, interface, name='', default=None):
+        # Note that we rarely call queryAdapter directly
+        # We usually end up calling adapter_hook
+        return self.adapter_hook(interface, object, name, default)
+
+
     def subscriptions(self, required, provided):
         if provided is None:
             provided = Null
@@ -483,8 +495,10 @@
             except KeyError:
                 pass
         lookup = AdapterLookup(self, surrogates, _remove)
+        
         for name in ('lookup', 'lookup1', 'queryAdapter', 'get',
-                     'subscriptions', 'queryMultiAdapter', 'subscribers'
+                     'adapter_hook', 'subscriptions',
+                     'queryMultiAdapter', 'subscribers',
                      ):
             setattr(self, name, getattr(lookup, name))
 

Modified: Zope3/trunk/src/zope/interface/adapter.txt
===================================================================
--- Zope3/trunk/src/zope/interface/adapter.txt	2004-07-10 02:50:25 UTC (rev 26388)
+++ Zope3/trunk/src/zope/interface/adapter.txt	2004-07-10 12:44:33 UTC (rev 26389)
@@ -170,8 +170,25 @@
   >>> y.context is x
   True
 
+An alternate method that provides the same function as `queryAdapter` is
+`adapter_hook`:
 
+  >>> y = registry.adapter_hook(IP1, x)
+  >>> y.__class__.__name__
+  'Y'
+  >>> y.context is x
+  True
+  >>> y = registry.adapter_hook(IP1, x, 'bob')
+  >>> y.__class__.__name__
+  'Y2'
+  >>> y.context is x
+  True
 
+The `adapter_hook` simply switches the order of the object and
+interface arguments.  It is used to hook into the interface call
+mechanism.
+
+
 Default Adapters
 ----------------
   



More information about the Zope3-Checkins mailing list