[Zope3-checkins] SVN: Zope3/trunk/src/zope/ Added a new adaptedBy function to get the adapter declaration for an

Jim Fulton jim at zope.com
Fri Feb 18 12:00:37 EST 2005


Log message for revision 29209:
  Added a new adaptedBy function to get the adapter declaration for an
  object.  This allows us to clean up the configuration-directive
  handlers so that they don't depend on the attribute name used to store
  the declaration.
  

Changed:
  U   Zope3/trunk/src/zope/app/component/metaconfigure.py
  U   Zope3/trunk/src/zope/component/README.txt
  U   Zope3/trunk/src/zope/component/__init__.py

-=-
Modified: Zope3/trunk/src/zope/app/component/metaconfigure.py
===================================================================
--- Zope3/trunk/src/zope/app/component/metaconfigure.py	2005-02-18 16:28:10 UTC (rev 29208)
+++ Zope3/trunk/src/zope/app/component/metaconfigure.py	2005-02-18 17:00:36 UTC (rev 29209)
@@ -17,6 +17,7 @@
 """
 __docformat__ = 'restructuredtext'
 
+from zope import component
 from zope.component.interfaces import IDefaultViewName, IFactory
 from zope.configuration.exceptions import ConfigurationError
 import zope.interface
@@ -81,11 +82,7 @@
                 DeprecationWarning)
 
     if for_ is None:
-        try:
-            for_ = factory.__component_adapts__
-        except AttributeError:
-            pass
-
+        for_ = component.adaptedBy(factory)
         if for_ is None:
             raise TypeError("No for attribute was provided and can't "
                             "determine what the factory (or handler) adapts.")
@@ -145,11 +142,8 @@
 
     if for_ is None:
         if len(factory) == 1:
-            try:
-                for_ = factory[0].__component_adapts__
-            except AttributeError:
-                pass
-
+            for_ = component.adaptedBy(factory[0])
+       
         if for_ is None:
             raise TypeError("No for attribute was provided and can't "
                             "determine what the factory adapts.")

Modified: Zope3/trunk/src/zope/component/README.txt
===================================================================
--- Zope3/trunk/src/zope/component/README.txt	2005-02-18 16:28:10 UTC (rev 29208)
+++ Zope3/trunk/src/zope/component/README.txt	2005-02-18 17:00:36 UTC (rev 29209)
@@ -95,10 +95,22 @@
 The class defines a constructor that takes an argument for every
 object adapted.
 
-We use `zope.component.adapts` to declare what we adapt.  If we
-declare the interfaces adapted and if we provide only one interface,
-as in the example above, then we can provide the adapter very simply [1]_:
+We used `zope.component.adapts` to declare what we adapt.  We can find
+out if an object declares that it adapts anything using adaptedBy:
 
+    >>> list(zope.component.adaptedBy(PersonGreeter)) == [IPerson]
+    True
+
+If an object makes no declaration, then None is returned:
+
+    >>> zope.component.adaptedBy(Greeter()) is None
+    True
+
+
+If we declare the interfaces adapted and if we provide only one
+interface, as in the example above, then we can provide the adapter
+very simply [1]_:
+
     >>> zope.component.provideAdapter(PersonGreeter)
 
 For adapters that adapt a single interface to a single interface

Modified: Zope3/trunk/src/zope/component/__init__.py
===================================================================
--- Zope3/trunk/src/zope/component/__init__.py	2005-02-18 16:28:10 UTC (rev 29208)
+++ Zope3/trunk/src/zope/component/__init__.py	2005-02-18 17:00:36 UTC (rev 29209)
@@ -202,7 +202,6 @@
 
         return ob
 
-
 def adapts(*interfaces):
     frame = sys._getframe(1)
     locals = frame.f_locals
@@ -219,6 +218,9 @@
 
     locals['__component_adapts__'] = _adapts_descr(interfaces)
 
+def adaptedBy(ob):
+    return getattr(ob, '__component_adapts__', None)
+
 #############################################################################
 # Register the component architectures adapter hook, with the adapter hook
 # registry of the `zope.inteface` package. This way we will be able to call



More information about the Zope3-Checkins mailing list