Permalink
Browse files

adding simple request plugin

  • Loading branch information...
1 parent c9352c4 commit 94210dfdf40906d1b07d6fa24a1624a2ca15d448 @turtlebender committed Apr 27, 2012
Showing with 77 additions and 0 deletions.
  1. +28 −0 bottle_request.py
  2. +25 −0 setup.py
  3. +24 −0 test.py
View
@@ -0,0 +1,28 @@
+"""
+Exceedingly simple bottle plugin to add request as a "stateless" parameter to
+web calls.. Having everything accessed through the "magic" thread
+local is pretty ugly, and this lets us treat the request as a stateless object.
+Note, even though this looks stateless, it is still the same threadlocal, but
+it can be easily swapped.
+"""
+import inspect
+from bottle import request
+
+def inject_request(callback):
+ """
+ Wrap the function in a decorator which adds the request parameter if
+ necessary.
+ """
+ args = inspect.getargspec(callback)[0]
+ if 'request' not in args:
+ return callback
+ def wrapper(*args, **kwargs):
+ """
+ Pass request object as a function parameter if signature contains
+ "request" as a parameter.
+ """
+ kwargs['request'] = request
+ return callback(*args, **kwargs)
+ return wrapper
+
+Plugin = inject_request
View
@@ -0,0 +1,25 @@
+from setuptools import setup
+
+setup(
+ name="bottle-request",
+ version="0.1.0",
+ url="http://github.com/turtlebender/bottle-request/",
+ description="Plugin to give bottle a 'stateless' request object",
+ author="Tom Howe",
+ author_email = 'turtlebender@gmail.com',
+ license="MIT",
+ platforms="any",
+ py_modules=["bottle_request"],
+ install_requires= [
+ 'bottle>=0.9',
+ ],
+ classifiers = [
+ 'Environment :: Web Environment',
+ 'Intended Audience :: Developers',
+ 'License :: OSI Approved :: MIT License',
+ 'Operating System :: OS Independent',
+ 'Programming Language :: Python',
+ 'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
+ 'Topic :: Software Development :: Libraries :: Python Modules'
+ ],
+)
View
24 test.py
@@ -0,0 +1,24 @@
+import unittest
+import bottle
+import bottle_request
+
+class TestRequestPlugin(unittest.TestCase):
+
+ def setUp(self):
+ self.app = bottle.Bottle(catchall=False)
+
+ def test_with_keyword(self):
+ self.plugin = self.app.install(bottle_request.Plugin)
+
+ @self.app.get('/')
+ def test(request):
+ self.assertEqual(bottle.request, request)
+ self.app({'PATH_INFO':'/', 'REQUEST_METHOD':'GET'}, lambda x, y: None)
+
+ def test_without_keyword(self):
+ self.plugin = self.app.install(bottle_request.Plugin)
+
+ @self.app.get('/')
+ def test():
+ pass
+ self.app({'PATH_INFO':'/', 'REQUEST_METHOD':'GET'}, lambda x, y: None)

0 comments on commit 94210df

Please sign in to comment.