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