Repository URL to install this package:
|
Version:
3.4.36 ▾
|
ó
¨EYc @ s d Z d d l m Z m Z d d l m Z d d l m Z d g Z d e f d YZ
d e f d YZ d e f d
YZ d S( sÜ
Decorators to wrap functions to make them WSGI applications.
The main decorator :class:`wsgify` turns a function into a WSGI
application (while also allowing normal calling of the method with an
instantiated request).
iÿÿÿÿ( t bytes_t text_type( t Request( t
HTTPExceptiont wsgifyc B s¡ e Z d Z e Z d d d
d d d Z d Z d d Z d Z d Z
d d Z d Z d Z
d d Z e d
Z e d d d Z RS( sà Turns a request-taking, response-returning function into a WSGI
app
You can use this like::
@wsgify
def myfunc(req):
return webob.Response('hey there')
With that ``myfunc`` will be a WSGI application, callable like
``app_iter = myfunc(environ, start_response)``. You can also call
it like normal, e.g., ``resp = myfunc(req)``. (You can also wrap
methods, like ``def myfunc(self, req)``.)
If you raise exceptions from :mod:`webob.exc` they will be turned
into WSGI responses.
There are also several parameters you can use to customize the
decorator. Most notably, you can use a :class:`webob.Request`
subclass, like::
class MyRequest(webob.Request):
@property
def is_local(self):
return self.remote_addr == '127.0.0.1'
@wsgify(RequestClass=MyRequest)
def myfunc(req):
if req.is_local:
return Response('hi!')
else:
raise webob.exc.HTTPForbidden
Another customization you can add is to add `args` (positional
arguments) or `kwargs` (of course, keyword arguments). While
generally not that useful, you can use this to create multiple
WSGI apps from one function, like::
import simplejson
def serve_json(req, json_obj):
return Response(json.dumps(json_obj),
content_type='application/json')
serve_ob1 = wsgify(serve_json, args=(ob1,))
serve_ob2 = wsgify(serve_json, args=(ob2,))
You can return several things from a function:
* A :class:`webob.Response` object (or subclass)
* *Any* WSGI application
* None, and then ``req.response`` will be used (a pre-instantiated
Response object)
* A string, which will be written to ``req.response`` and then that
response will be used.
* Raise an exception from :mod:`webob.exc`
Also see :func:`wsgify.middleware` for a way to make middleware.
You can also subclass this decorator; the most useful things to do
in a subclass would be to change `RequestClass` or override
`call_func` (e.g., to add ``req.urlvars`` as keyword arguments to
the function).
c C sj | | _ | d k r0 | | j k r0 | | _ n t | | _ | d k rT i } n | | _ | | _ d S( N( t funct Nonet RequestClasst tuplet argst kwargst middleware_wraps( t selfR R R R
R ( ( s>