[Zope3-checkins] SVN: Zope3/trunk/ Ensured that all adapter-related directives and registration calls accept

Stephan Richter srichter at cosmos.phy.tufts.edu
Mon Feb 28 05:49:30 EST 2005


Log message for revision 29339:
  Ensured that all adapter-related directives and registration calls accept 
  classes as their required interface. I had to implement that feature for 
  subscribers as it was not done yet.
  
  

Changed:
  U   Zope3/trunk/doc/CHANGES.txt
  U   Zope3/trunk/doc/TODO.txt
  U   Zope3/trunk/src/zope/app/component/tests/test_directives.py
  U   Zope3/trunk/src/zope/app/event/dispatching.py
  U   Zope3/trunk/src/zope/component/__init__.py
  U   Zope3/trunk/src/zope/component/site.py

-=-
Modified: Zope3/trunk/doc/CHANGES.txt
===================================================================
--- Zope3/trunk/doc/CHANGES.txt	2005-02-28 00:16:22 UTC (rev 29338)
+++ Zope3/trunk/doc/CHANGES.txt	2005-02-28 10:49:30 UTC (rev 29339)
@@ -86,7 +86,7 @@
         `zope.deprecation`. It allows one to deprecate methods and properties
         in classes as well as any name in a module.
 
-      - The browser:containerViews directive provides an optional layer
+      - The `browser:containerViews` directive provides an optional layer
         attribute.
 
       - Groups for unauthenticated users, authenticated users, and
@@ -97,7 +97,7 @@
         for most subscribers.  Using the factory attribute without a
         provides attribute to register a handler is deprecated.
 
-      - Added the function zope.component.adapts for declaring interfaces
+      - Added the function `zope.component.adapts` for declaring interfaces
         adapted by classes. This is similar to zope.interface.implements in
         that it puts a marker on the class that has a significant meaning in
         the component architecture machinery.
@@ -288,6 +288,11 @@
 
     Restructuring
 
+      - Ensured that all adapters can be registered for classes as well as
+        interfaces by writing tests for all adapter-based directives. It
+        turned out that subscribers did not accept classes yet, so I
+        implemented the feature for subscribers.
+
       - Addressed issue 295: Sort out defaultView.
 
         Deprecated `zope:defaultView` directive and removed unused default

Modified: Zope3/trunk/doc/TODO.txt
===================================================================
--- Zope3/trunk/doc/TODO.txt	2005-02-28 00:16:22 UTC (rev 29338)
+++ Zope3/trunk/doc/TODO.txt	2005-02-28 10:49:30 UTC (rev 29339)
@@ -11,12 +11,7 @@
 
 - Support for iterable sources
 
-- Allow adapters (including views) to be registered for classes
-  (really implementation specifications of classes) as well as
-  interfaces. This has been done for page directives but needs to be
-  done for other adapter directives.
 
-
 Miscellaneous
 -------------
 

Modified: Zope3/trunk/src/zope/app/component/tests/test_directives.py
===================================================================
--- Zope3/trunk/src/zope/app/component/tests/test_directives.py	2005-02-28 00:16:22 UTC (rev 29338)
+++ Zope3/trunk/src/zope/app/component/tests/test_directives.py	2005-02-28 10:49:30 UTC (rev 29339)
@@ -43,7 +43,8 @@
 from zope.app.component.interface import queryInterface
 from zope.app.component.metaconfigure import interface
 from zope.app.component.tests.adapter import A1, A2, A3, I1, I3, IS, Handler
-from zope.app.component.tests.components import Content, IApp, Comp, comp
+from zope.app.component.tests.components import IContent, Content, Comp, comp
+from zope.app.component.tests.components import IApp
 from zope.app.component.tests.views import IV, IC, V1, R1, IR
 from zope.app.content.interfaces import IContentType
 
@@ -225,6 +226,25 @@
 
         self.assertEqual(content.args, ((a1,),))
         
+    def testSubscriberHavingARequiredClass(self):
+        xmlconfig(StringIO(template % (
+            '''
+            <subscriber
+              for="zope.app.component.tests.components.Content"
+              provides="zope.app.component.tests.adapter.I1"
+              factory="zope.app.component.tests.adapter.A1"
+              />
+            '''
+            )))
+
+        subs = zapi.subscribers((Content(),), I1)
+        self.assert_(isinstance(subs[0], A1))
+        
+        class MyContent:
+            implements(IContent)
+        
+        self.assertEqual(zapi.subscribers((MyContent(),), I1), [])
+
     def testMultiSubscriber(self):
         xmlconfig(StringIO(template % (
             '''
@@ -384,6 +404,28 @@
                              )),
                           )
 
+    def testAdapterHavingARequiredClass(self):
+        xmlconfig(StringIO(template % (
+            '''
+            <adapter
+              for="zope.app.component.tests.components.Content"
+              provides="zope.app.component.tests.adapter.I1"
+              factory="zope.app.component.tests.adapter.A1"
+              />
+            '''
+            )))
+
+        content = Content()
+        a1 = zapi.getAdapter(content, I1, '')
+        self.assert_(isinstance(a1, A1))
+
+        class MyContent:
+            implements(IContent)
+
+        self.assertRaises(ComponentLookupError, zapi.getAdapter,
+                          MyContent(), I1, '')
+
+
     def testMultiAdapter(self):
         xmlconfig(StringIO(template % (
             '''
@@ -858,6 +900,27 @@
         v = zapi.queryMultiAdapter((ob, Request(IR)), IV)
         self.assertEqual(v.__class__, V1)
 
+    def testViewHavingARequiredClass(self):
+        xmlconfig(StringIO(template % (
+            '''
+            <view
+              for="zope.app.component.tests.components.Content"
+              type="zope.app.component.tests.views.IR"
+              factory="zope.app.component.tests.adapter.A1"
+              />
+            '''
+            )))
+
+        content = Content()
+        a1 = zapi.getMultiAdapter((content, Request(IR)))
+        self.assert_(isinstance(a1, A1))
+
+        class MyContent:
+            implements(IContent)
+
+        self.assertRaises(ComponentLookupError, zapi.getMultiAdapter,
+                          (MyContent(), Request(IR)))
+
     def testInterfaceProtectedView(self):
         xmlconfig(StringIO(template %
             '''

Modified: Zope3/trunk/src/zope/app/event/dispatching.py
===================================================================
--- Zope3/trunk/src/zope/app/event/dispatching.py	2005-02-28 00:16:22 UTC (rev 29338)
+++ Zope3/trunk/src/zope/app/event/dispatching.py	2005-02-28 10:49:30 UTC (rev 29339)
@@ -36,8 +36,8 @@
 
   >>> from zope.app.testing import ztapi
   >>> from zope.interface import implementedBy
-  >>> ztapi.handle([implementedBy(E1)], handler1)
-  >>> ztapi.handle([implementedBy(E2)], handler2)
+  >>> ztapi.handle([implementedBy(E1)], handler1) # old way
+  >>> ztapi.handle((E2,), handler2) # new way
 
   >>> from zope.event import notify
 

Modified: Zope3/trunk/src/zope/component/__init__.py
===================================================================
--- Zope3/trunk/src/zope/component/__init__.py	2005-02-28 00:16:22 UTC (rev 29338)
+++ Zope3/trunk/src/zope/component/__init__.py	2005-02-28 10:49:30 UTC (rev 29339)
@@ -264,7 +264,7 @@
 # Factories
 
 def createObject(__factory_name, *args, **kwargs):
-    # BBB
+    # BBB: Goes away in 3.3 
     if not isinstance(__factory_name, basestring):
         import warnings
         warnings.warn(

Modified: Zope3/trunk/src/zope/component/site.py
===================================================================
--- Zope3/trunk/src/zope/component/site.py	2005-02-28 00:16:22 UTC (rev 29338)
+++ Zope3/trunk/src/zope/component/site.py	2005-02-28 10:49:30 UTC (rev 29339)
@@ -18,7 +18,7 @@
 __docformat__ = "reStructuredText"
 import types
 
-from zope.interface import implements, providedBy, implementedBy
+from zope.interface import implements, providedBy, implementedBy, declarations
 from zope.interface.adapter import AdapterRegistry
 from zope.interface.interfaces import IInterface
 
@@ -204,8 +204,18 @@
         SubscriptionRegistration(('R1',), 'P2', 'c1', 'd1')
         SubscriptionRegistration(('R1',), 'P2', 'c2', 'd2')
         """
-        required = tuple(required)
+        ifaces = []
+        for iface in required:
+            if not IInterface.providedBy(iface) and \
+                   not isinstance(iface, declarations.Implements) and \
+                   iface is not None:
+                if not isinstance(iface, (type, types.ClassType)):
+                    raise TypeError(iface, IInterface)
+                iface = implementedBy(iface)
 
+            ifaces.append(iface)
+        required = tuple(ifaces)
+
         registration = SubscriptionRegistration(
             required, provided, factory, info)
 



More information about the Zope3-Checkins mailing list