diff options
author | Marcel Hellkamp <marc@gsites.de> | 2011-04-28 20:26:10 +0200 |
---|---|---|
committer | Marcel Hellkamp <marc@gsites.de> | 2011-04-28 20:30:13 +0200 |
commit | 37bf1a5fb0b07d882f347d6516ff4b424fb4a5ec (patch) | |
tree | eda20a6fcf88aefb479813992dcf1ee4f9cde77a | |
parent | 266127eeb75c5b8f48c04a3872ce18ac0d553eb7 (diff) | |
download | bottle-37bf1a5fb0b07d882f347d6516ff4b424fb4a5ec.tar.gz |
Added werkzeug plugin (untested).
(cherry picked from commit 4ffe685c22d89ecf909db370966443befbb4802e)
-rw-r--r-- | plugins/werkzeug/bottle_werkzeug.py | 77 | ||||
-rw-r--r-- | plugins/werkzeug/setup.py | 70 |
2 files changed, 147 insertions, 0 deletions
diff --git a/plugins/werkzeug/bottle_werkzeug.py b/plugins/werkzeug/bottle_werkzeug.py new file mode 100644 index 0000000..844bbfa --- /dev/null +++ b/plugins/werkzeug/bottle_werkzeug.py @@ -0,0 +1,77 @@ +# -*- coding: utf-8 -*- +""" +This plugin adds support for :class:`werkzeug.Response`, all kinds of +:exc:`werkzeug.HTTPException` and provides a thread-local instance of +:class:`werkzeug.Request`. It basically turns Bottle into Flask. + +The plugin instance doubles as a werkzeug module object, so you don't need to +import werkzeug in your application. + +For werkzeug library documentation, see: http://werkzeug.pocoo.org/ + +Example:: + + import bottle + from bottle.ext.werkzeug import WerkzeugPlugin + + app = bottle.Bottle() + werkzeug = app.install(WerkzeugPlugin()) + wrequest = werkzueg.request # For the lazy. + + @app.route('/hello/:name') + def say_hello(name): + greet = {'en':'Hello', 'de':'Hallo', 'fr':'Bonjour'} + language = wrequest.accept_language.best_match(greet.keys()) + if language: + return werkzeug.Response('%s %s!' % (greet[language], name)) + else: + raise werkzeug.exceptions.NotAcceptable() + +""" + + +__autor__ = "Marcel Hellkamp" +__version__ = '0.1' +__licence__ = 'MIT' + +import werkzeug +from werkzeug import * +import bottle + +class WerkzeugPlugin(object): + """ This plugin adds support for :class:`werkzeug.Response`, all kinds of + :module:`werkzeug.exceptions` and provides a thread-local instance of + :class:`werkzeug.Request`. It basically turns Bottle into Flask. """ + + name = 'werkzeug' + + def __init_(self, request_class=werkzeug.Request, **config): + self.request_factory = request_class + self.config = config + self.app = None + + def setup(self, app): + self.app = app + + def apply(self, callback, context): + def wrapper(*a, **ka): + environ = bottle.request.environ + bottle.local.werkzueg_request = self.request_factory(environ, **self.config) + try: + rv = callback(*a, **ka) + except werkzeug.exceptions.HTTPException, e: + rv = e.get_response(environ) + if isinstance(rv, werkzeug.BaseResponse): + rv = bottle.HTTPResponse(rv.iter_encoded(), rv.status_code, rv.header_list) + return rv + return wrapper + + @property + def request(self): + ''' Return a local proxy to the current :class:`werkzeug.Request` + instance.''' + return werkzeug.LocalProxy(lambda: bottle.local.werkzueg_request) + + def __getattr__(self, name): + ''' Convenient access to werkzeug module contents. ''' + return getattr(werkzeug, name) diff --git a/plugins/werkzeug/setup.py b/plugins/werkzeug/setup.py new file mode 100644 index 0000000..f689b39 --- /dev/null +++ b/plugins/werkzeug/setup.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python + +""" +This plugin adds support for :class:`werkzeug.Response`, all kinds of +:exc:`werkzeug.HTTPException` and provides a thread-local instance of +:class:`werkzeug.Request`. It basically turns Bottle into Flask. + +The plugin instance doubles as a werkzeug module object, so you don't need to +import werkzeug in your application. + +For werkzeug library documentation, see: http://werkzeug.pocoo.org/ + +Example:: + + import bottle + from bottle.ext.werkzeug import WerkzeugPlugin + + app = bottle.Bottle() + werkzeug = app.install(WerkzeugPlugin()) + wrequest = werkzueg.request # For the lazy. + + @app.route('/hello/:name') + def say_hello(name): + greet = {'en':'Hello', 'de':'Hallo', 'fr':'Bonjour'} + language = wrequest.accept_language.best_match(greet.keys()) + if language: + return werkzeug.Response('%s %s!' % (greet[language], name)) + else: + raise werkzeug.exceptions.NotAcceptable() + +""" + +import sys +import os +import re +from distutils.core import setup + +try: + from distutils.command.build_py import build_py_2to3 as build_py +except ImportError: + from distutils.command.build_py import build_py + +setup( + name = 'bottle-werkzeug', + version = '0.1', + url = 'http://bottlepy.org/docs/dev/plugin/werkzeug/', + description = 'Werkzeug integration for Bottle', + long_description = __doc__, + author = 'Marcel Hellkamp', + author_email = 'marc@gsites.de', + license = 'MIT', + platforms = 'any', + py_modules = [ + 'bottle_werkzeug' + ], + requires = [ + 'bottle (>=0.9)', + 'werkzeug' + ], + 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' + ], + cmdclass = {'build_py': build_py} +) |