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