[Checkins] SVN: grokcore.component/trunk/ * Allow functions that have been marked with @grok.subscriber also be

Philipp von Weitershausen philikon at philikon.de
Sat Jul 19 10:28:16 EDT 2008


Log message for revision 88595:
  * Allow functions that have been marked with @grok.subscriber also be
    registered with ``zope.component.provideHandler()`` manually.  This
    is useful for unit tests where you may not want to grok a whole
    module.
  
  

Changed:
  U   grokcore.component/trunk/CHANGES.txt
  U   grokcore.component/trunk/src/grokcore/component/decorators.py
  A   grokcore.component/trunk/src/grokcore/component/tests/event/provideHandler.py

-=-
Modified: grokcore.component/trunk/CHANGES.txt
===================================================================
--- grokcore.component/trunk/CHANGES.txt	2008-07-19 14:28:07 UTC (rev 88594)
+++ grokcore.component/trunk/CHANGES.txt	2008-07-19 14:28:16 UTC (rev 88595)
@@ -9,6 +9,11 @@
   method in grokcore.component.testing anymore. Use z3c.testsetup
   instead.
 
+* Allow functions that have been marked with @grok.subscriber also be
+  registered with ``zope.component.provideHandler()`` manually.  This
+  is useful for unit tests where you may not want to grok a whole
+  module.
+
 1.4 (2008-06-11)
 ----------------
 

Modified: grokcore.component/trunk/src/grokcore/component/decorators.py
===================================================================
--- grokcore.component/trunk/src/grokcore/component/decorators.py	2008-07-19 14:28:07 UTC (rev 88594)
+++ grokcore.component/trunk/src/grokcore/component/decorators.py	2008-07-19 14:28:16 UTC (rev 88595)
@@ -39,8 +39,12 @@
         if subscribers is None:
             frame.f_locals['__grok_subscribers__'] = subscribers = []
         subscribers.append((function, self.subscribed))
-        return function
 
+        # Also add __grok_adapts__ attribute to the function so that
+        # you can manually register the subscriber with, say,
+        # provideHandler.
+        return zope.component.adapter(*self.subscribed)(function)
+
 class adapter(zope.component.adapter):
 
     # Override the z.c.adapter decorator to force sanity checking and

Added: grokcore.component/trunk/src/grokcore/component/tests/event/provideHandler.py
===================================================================
--- grokcore.component/trunk/src/grokcore/component/tests/event/provideHandler.py	                        (rev 0)
+++ grokcore.component/trunk/src/grokcore/component/tests/event/provideHandler.py	2008-07-19 14:28:16 UTC (rev 88595)
@@ -0,0 +1,28 @@
+"""
+When you use the @grokcore.component.subscribe decorator, you can also
+use zope.component.provideHandler to register the subscriber.  This
+can be useful for unittests where you may not want to grok everything
+in a module but just enable certain components.
+
+  >>> from zope.component import provideHandler
+  >>> provideHandler(mammothAdded)
+
+  >>> manfred = Mammoth('Manfred')
+  >>> import zope.event
+  >>> zope.event.notify(ObjectEvent(manfred))
+  >>> mammoths
+  ['Manfred']
+
+"""
+import grokcore.component as grok
+from zope.component.interfaces import IObjectEvent, ObjectEvent
+
+class Mammoth(object):
+    def __init__(self, name):
+        self.name = name
+
+mammoths = []
+
+ at grok.subscribe(Mammoth, IObjectEvent)
+def mammothAdded(mammoth, event):
+    mammoths.append(mammoth.name)


Property changes on: grokcore.component/trunk/src/grokcore/component/tests/event/provideHandler.py
___________________________________________________________________
Name: svn:eol-style
   + native



More information about the Checkins mailing list