[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/component/ Made the for attribute optional in a subscriber directive if the

Jim Fulton jim at zope.com
Fri Feb 18 11:28:10 EST 2005


Log message for revision 29208:
  Made the for attribute optional in a subscriber directive if the
  factory or handler has an adapter declaration.
  

Changed:
  U   Zope3/trunk/src/zope/app/component/metaconfigure.py
  U   Zope3/trunk/src/zope/app/component/metadirectives.py
  U   Zope3/trunk/src/zope/app/component/tests/test_directives.py

-=-
Modified: Zope3/trunk/src/zope/app/component/metaconfigure.py
===================================================================
--- Zope3/trunk/src/zope/app/component/metaconfigure.py	2005-02-18 16:05:39 UTC (rev 29207)
+++ Zope3/trunk/src/zope/app/component/metaconfigure.py	2005-02-18 16:28:10 UTC (rev 29208)
@@ -59,9 +59,10 @@
     return ob
 
 _handler=handler
-def subscriber(_context, for_, factory=None, handler=None, provides=None,
+def subscriber(_context, for_=None, factory=None, handler=None, provides=None,
                permission=None, trusted=False):
 
+
     if factory is None:
         if handler is None:
             raise TypeError("No factory or handler provides")
@@ -78,6 +79,16 @@
                 "is deprecated and will change it's meaning in Zope X3.3. "
                 "Use the handler attribute instead.",
                 DeprecationWarning)
+
+    if for_ is None:
+        try:
+            for_ = factory.__component_adapts__
+        except AttributeError:
+            pass
+
+        if for_ is None:
+            raise TypeError("No for attribute was provided and can't "
+                            "determine what the factory (or handler) adapts.")
     
     factory = [factory]
 

Modified: Zope3/trunk/src/zope/app/component/metadirectives.py
===================================================================
--- Zope3/trunk/src/zope/app/component/metadirectives.py	2005-02-18 16:05:39 UTC (rev 29207)
+++ Zope3/trunk/src/zope/app/component/metadirectives.py	2005-02-18 16:28:10 UTC (rev 29208)
@@ -246,7 +246,7 @@
     for_ = zope.configuration.fields.Tokens(
         title=_("Interfaces or classes that this subscriber depends on"),
         description=_("This should be a list of interfaces or classes"),
-        required=True,
+        required=False,
         value_type=zope.configuration.fields.GlobalObject(
           missing_value = object(),
           ),

Modified: Zope3/trunk/src/zope/app/component/tests/test_directives.py
===================================================================
--- Zope3/trunk/src/zope/app/component/tests/test_directives.py	2005-02-18 16:05:39 UTC (rev 29207)
+++ Zope3/trunk/src/zope/app/component/tests/test_directives.py	2005-02-18 16:28:10 UTC (rev 29208)
@@ -131,6 +131,46 @@
         self.assertEqual(a3.__class__, A3)
         self.assertEqual(a3.context, (content, a1))
 
+    def testSubscriber_wo_for(self):
+        xmlconfig(StringIO(template % (
+            '''
+            <subscriber
+              provides="zope.app.component.tests.adapter.IS"
+              factory="zope.app.component.tests.adapter.A3"
+              />
+            '''
+            )))
+
+        content = Content()
+        a1 = A1()
+        a2 = A2()
+        subscribers = zapi.subscribers((content, a1, a2), IS)
+
+        a3 = subscribers[0]
+
+        self.assertEqual(a3.__class__, A3)
+        self.assertEqual(a3.context, (content, a1, a2))
+
+    def testHandlerSubscriber_no_for(self):
+        xmlconfig(StringIO(template % (
+            '''
+            <subscriber
+              handler="zope.app.component.tests.adapter.A3"
+              />
+            '''
+            )))
+
+        content = Content()
+        a1 = A1()
+        a2 = A2()
+        subscribers = zapi.subscribers((content, a1, a2), None)
+
+        a3 = subscribers[0]
+
+        self.assertEqual(a3.__class__, A3)
+        self.assertEqual(a3.context, (content, a1, a2))
+        
+
     def testTrustedSubscriber(self):
         xmlconfig(StringIO(template % (
             '''



More information about the Zope3-Checkins mailing list