commit | author | age
|
e7cb93
|
1 |
from functools import wraps |
8cf6dd
|
2 |
from pyramid.request import call_app_with_subpath_as_path_info |
d1209e
|
3 |
|
95b14b
|
4 |
def wsgiapp(wrapped): |
fd5ae9
|
5 |
""" Decorator to turn a WSGI application into a :app:`Pyramid` |
c6895b
|
6 |
:term:`view callable`. This decorator differs from the |
c81aad
|
7 |
:func:`pyramid.wsgi.wsgiapp2` decorator inasmuch as fixups of |
c6895b
|
8 |
``PATH_INFO`` and ``SCRIPT_NAME`` within the WSGI environment *are |
CM |
9 |
not* performed before the application is invoked. |
95b14b
|
10 |
|
8b1f6e
|
11 |
E.g., the following in a ``views.py`` module:: |
95b14b
|
12 |
|
CM |
13 |
@wsgiapp |
|
14 |
def hello_world(environ, start_response): |
|
15 |
body = 'Hello world' |
|
16 |
start_response('200 OK', [ ('Content-Type', 'text/plain'), |
|
17 |
('Content-Length', len(body)) ] ) |
|
18 |
return [body] |
|
19 |
|
c1eb0c
|
20 |
Allows the following call to |
aff443
|
21 |
:meth:`pyramid.config.Configurator.add_view`:: |
8b1f6e
|
22 |
|
CM |
23 |
from views import hello_world |
|
24 |
config.add_view(hello_world, name='hello_world.txt') |
95b14b
|
25 |
|
c6895b
|
26 |
The ``wsgiapp`` decorator will convert the result of the WSGI |
CM |
27 |
application to a :term:`Response` and return it to |
aaedf5
|
28 |
:app:`Pyramid` as if the WSGI app were a :app:`Pyramid` |
c6895b
|
29 |
view. |
bfc145
|
30 |
|
95b14b
|
31 |
""" |
d292b9
|
32 |
|
BJR |
33 |
if wrapped is None: |
|
34 |
raise ValueError('wrapped can not be None') |
|
35 |
|
9dd41c
|
36 |
def decorator(context, request): |
5a7f9a
|
37 |
return request.get_response(wrapped) |
dc474e
|
38 |
|
MK |
39 |
# Support case where wrapped is a callable object instance |
|
40 |
if getattr(wrapped, '__name__', None): |
|
41 |
return wraps(wrapped)(decorator) |
|
42 |
return wraps(wrapped, ('__module__', '__doc__'))(decorator) |
cfd4e5
|
43 |
|
d1209e
|
44 |
def wsgiapp2(wrapped): |
fd5ae9
|
45 |
""" Decorator to turn a WSGI application into a :app:`Pyramid` |
c6895b
|
46 |
view callable. This decorator differs from the |
c81aad
|
47 |
:func:`pyramid.wsgi.wsgiapp` decorator inasmuch as fixups of |
c6895b
|
48 |
``PATH_INFO`` and ``SCRIPT_NAME`` within the WSGI environment |
CM |
49 |
*are* performed before the application is invoked. |
d1209e
|
50 |
|
8b1f6e
|
51 |
E.g. the following in a ``views.py`` module:: |
d1209e
|
52 |
|
f348c6
|
53 |
@wsgiapp2 |
d1209e
|
54 |
def hello_world(environ, start_response): |
CM |
55 |
body = 'Hello world' |
|
56 |
start_response('200 OK', [ ('Content-Type', 'text/plain'), |
|
57 |
('Content-Length', len(body)) ] ) |
|
58 |
return [body] |
|
59 |
|
c1eb0c
|
60 |
Allows the following call to |
aff443
|
61 |
:meth:`pyramid.config.Configurator.add_view`:: |
8b1f6e
|
62 |
|
CM |
63 |
from views import hello_world |
|
64 |
config.add_view(hello_world, name='hello_world.txt') |
|
65 |
|
c6895b
|
66 |
The ``wsgiapp2`` decorator will convert the result of the WSGI |
ba0a5f
|
67 |
application to a Response and return it to :app:`Pyramid` as if the WSGI |
CM |
68 |
app were a :app:`Pyramid` view. The ``SCRIPT_NAME`` and ``PATH_INFO`` |
|
69 |
values present in the WSGI environment are fixed up before the |
|
70 |
application is invoked. In particular, a new WSGI environment is |
|
71 |
generated, and the :term:`subpath` of the request passed to ``wsgiapp2`` |
|
72 |
is used as the new request's ``PATH_INFO`` and everything preceding the |
|
73 |
subpath is used as the ``SCRIPT_NAME``. The new environment is passed to |
|
74 |
the downstream WSGI application.""" |
bfc145
|
75 |
|
d292b9
|
76 |
if wrapped is None: |
BJR |
77 |
raise ValueError('wrapped can not be None') |
|
78 |
|
d1209e
|
79 |
def decorator(context, request): |
8cf6dd
|
80 |
return call_app_with_subpath_as_path_info(request, wrapped) |
dc474e
|
81 |
|
MK |
82 |
# Support case where wrapped is a callable object instance |
|
83 |
if getattr(wrapped, '__name__', None): |
|
84 |
return wraps(wrapped)(decorator) |
|
85 |
return wraps(wrapped, ('__module__', '__doc__'))(decorator) |