[Checkins] SVN: zope.globalrequest/trunk/src/zope/globalrequest/ provide a way to store request objects in a thread-local manner[*] and register event subscribers to set and clear the values
Andreas Zeidler
az at zitc.de
Thu Jan 15 10:47:52 EST 2009
Log message for revision 94753:
provide a way to store request objects in a thread-local manner[*] and register event subscribers to set and clear the values
[*] borrowing the [http://dev.plone.org/collective/browser/collective.solr/trunk/collective/solr/local.py?rev=78873 local.py] module
from [http://pypi.python.org/pypi/collective.solr/ collective.solr]
Changed:
U zope.globalrequest/trunk/src/zope/globalrequest/README.txt
U zope.globalrequest/trunk/src/zope/globalrequest/__init__.py
A zope.globalrequest/trunk/src/zope/globalrequest/configure.zcml
A zope.globalrequest/trunk/src/zope/globalrequest/local.py
A zope.globalrequest/trunk/src/zope/globalrequest/subscribers.py
-=-
Modified: zope.globalrequest/trunk/src/zope/globalrequest/README.txt
===================================================================
--- zope.globalrequest/trunk/src/zope/globalrequest/README.txt 2009-01-15 15:47:39 UTC (rev 94752)
+++ zope.globalrequest/trunk/src/zope/globalrequest/README.txt 2009-01-15 15:47:46 UTC (rev 94753)
@@ -24,10 +24,16 @@
>>> from zope.interface import implements
>>> from zope.globalrequest import ftests
+ >>> from zope.globalrequest import getRequest
>>> class Foo(object):
... implements(ftests.IFoo)
... def foo(self):
- ... return 'foo!'
+ ... request = getRequest()
+ ... if request:
+ ... name = request.get('name', 'n00b')
+ ... else:
+ ... name = 'foo'
+ ... return 'y0 %s!' % name
Unfortunately the utility class cannot be directly imported from here, i.e.
relatively, so we have to make it available from somewhere else to register
@@ -43,9 +49,33 @@
... </configure>
... """)
-Rendering the view again should now give us the value provided by the utility:
+Rendering the view again should now give us the default value provided by the
+utility:
>>> browser.reload()
>>> browser.contents
- 'foo!'
+ 'y0 foo!'
+Up to now the request hasn't been stored for us yet, so let's hook up the
+necessary event subscribers and try that again:
+
+ >>> zcml("""
+ ... <configure xmlns="http://namespaces.zope.org/zope">
+ ... <include package="zope.component" file="meta.zcml" />
+ ... <include package="zope.globalrequest" />
+ ... </configure>
+ ... """)
+
+Now we should get the request and therefore the fallback value from the form
+lookup:
+
+ >>> browser.reload()
+ >>> browser.contents
+ 'y0 n00b!'
+
+If we now provide a request value we should be greeted properly:
+
+ >>> browser.open('?name=d4wg!')
+ >>> browser.contents
+ 'y0 d4wg!!'
+
Modified: zope.globalrequest/trunk/src/zope/globalrequest/__init__.py
===================================================================
--- zope.globalrequest/trunk/src/zope/globalrequest/__init__.py 2009-01-15 15:47:39 UTC (rev 94752)
+++ zope.globalrequest/trunk/src/zope/globalrequest/__init__.py 2009-01-15 15:47:46 UTC (rev 94753)
@@ -3,3 +3,21 @@
moduleProvides(IGlobalRequest)
+
+from zope.globalrequest.local import getLocal, setLocal
+
+
+def getRequest():
+ """ return the currently active request object """
+ return getLocal('request')
+
+
+def setRequest(request):
+ """ set the request object to be returned by `getRequest` """
+ setLocal('request', request)
+
+
+def clearRequest():
+ """ clear the stored request object """
+ setRequest(None)
+
Added: zope.globalrequest/trunk/src/zope/globalrequest/configure.zcml
===================================================================
--- zope.globalrequest/trunk/src/zope/globalrequest/configure.zcml (rev 0)
+++ zope.globalrequest/trunk/src/zope/globalrequest/configure.zcml 2009-01-15 15:47:46 UTC (rev 94753)
@@ -0,0 +1,16 @@
+<configure
+ xmlns="http://namespaces.zope.org/zope"
+ i18n_domain="zope.globalrequest">
+
+ <subscriber
+ handler=".subscribers.set"
+ for="zope.app.component.interfaces.ISite
+ zope.app.publication.interfaces.IBeforeTraverseEvent"
+ trusted="y" />
+
+ <subscriber
+ handler=".subscribers.clear"
+ for="zope.app.publication.interfaces.IEndRequestEvent"
+ trusted="y" />
+
+</configure>
Added: zope.globalrequest/trunk/src/zope/globalrequest/local.py
===================================================================
--- zope.globalrequest/trunk/src/zope/globalrequest/local.py (rev 0)
+++ zope.globalrequest/trunk/src/zope/globalrequest/local.py 2009-01-15 15:47:46 UTC (rev 94753)
@@ -0,0 +1,18 @@
+from threading import local
+
+
+# a thread-local object holding arbitrary data
+localData = local()
+marker = []
+
+# helper functions to get/set local values or optionally initialize them
+def getLocal(name, factory=lambda: None):
+ value = getattr(localData, name, marker)
+ if value is marker:
+ value = factory()
+ setLocal(name, value)
+ return value
+
+def setLocal(name, value):
+ setattr(localData, name, value)
+
Added: zope.globalrequest/trunk/src/zope/globalrequest/subscribers.py
===================================================================
--- zope.globalrequest/trunk/src/zope/globalrequest/subscribers.py (rev 0)
+++ zope.globalrequest/trunk/src/zope/globalrequest/subscribers.py 2009-01-15 15:47:46 UTC (rev 94753)
@@ -0,0 +1,12 @@
+from zope.globalrequest import setRequest, clearRequest
+
+
+def set(context, event):
+ """ set the request object as provided by the event """
+ setRequest(event.request)
+
+
+def clear(event):
+ """ clear the stored request object """
+ clearRequest()
+
More information about the Checkins
mailing list