Michael Merickel
2018-10-18 e4c0570d5c67ddf0ad9502169b59475ba0784d82
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
from zope.interface import implementer, providedBy
 
from pyramid.interfaces import (
    IDebugLogger,
    IExecutionPolicy,
    IRequest,
    IRequestExtensions,
    IRootFactory,
    IRouteRequest,
    IRouter,
    IRequestFactory,
    IRoutesMapper,
    ITraverser,
    ITweens,
)
 
from pyramid.events import (
    ContextFound,
    NewRequest,
    NewResponse,
    BeforeTraversal,
)
 
from pyramid.httpexceptions import HTTPNotFound
from pyramid.request import Request
from pyramid.view import _call_view
from pyramid.request import apply_request_extensions
from pyramid.threadlocal import RequestContext
 
from pyramid.traversal import DefaultRootFactory, ResourceTreeTraverser
 
 
@implementer(IRouter)
class Router(object):
 
    debug_notfound = False
    debug_routematch = False
 
    def __init__(self, registry):
        q = registry.queryUtility
        self.logger = q(IDebugLogger)
        self.root_factory = q(IRootFactory, default=DefaultRootFactory)
        self.routes_mapper = q(IRoutesMapper)
        self.request_factory = q(IRequestFactory, default=Request)
        self.request_extensions = q(IRequestExtensions)
        self.execution_policy = q(
            IExecutionPolicy, default=default_execution_policy
        )
        self.orig_handle_request = self.handle_request
        tweens = q(ITweens)
        if tweens is not None:
            self.handle_request = tweens(self.handle_request, registry)
        self.root_policy = self.root_factory  # b/w compat
        self.registry = registry
        settings = registry.settings
        if settings is not None:
            self.debug_notfound = settings['debug_notfound']
            self.debug_routematch = settings['debug_routematch']
 
    def handle_request(self, request):
        attrs = request.__dict__
        registry = attrs['registry']
 
        request.request_iface = IRequest
        context = None
        routes_mapper = self.routes_mapper
        debug_routematch = self.debug_routematch
        adapters = registry.adapters
        has_listeners = registry.has_listeners
        notify = registry.notify
        logger = self.logger
 
        has_listeners and notify(NewRequest(request))
        # find the root object
        root_factory = self.root_factory
        if routes_mapper is not None:
            info = routes_mapper(request)
            match, route = info['match'], info['route']
            if route is None:
                if debug_routematch:
                    msg = 'no route matched for url %s' % request.url
                    logger and logger.debug(msg)
            else:
                attrs['matchdict'] = match
                attrs['matched_route'] = route
 
                if debug_routematch:
                    msg = (
                        'route matched for url %s; '
                        'route_name: %r, '
                        'path_info: %r, '
                        'pattern: %r, '
                        'matchdict: %r, '
                        'predicates: %r'
                        % (
                            request.url,
                            route.name,
                            request.path_info,
                            route.pattern,
                            match,
                            ', '.join([p.text() for p in route.predicates]),
                        )
                    )
                    logger and logger.debug(msg)
 
                request.request_iface = registry.queryUtility(
                    IRouteRequest, name=route.name, default=IRequest
                )
 
                root_factory = route.factory or self.root_factory
 
        # Notify anyone listening that we are about to start traversal
        #
        # Notify before creating root_factory in case we want to do something
        # special on a route we may have matched. See
        # https://github.com/Pylons/pyramid/pull/1876 for ideas of what is
        # possible.
        has_listeners and notify(BeforeTraversal(request))
 
        # Create the root factory
        root = root_factory(request)
        attrs['root'] = root
 
        # We are about to traverse and find a context
        traverser = adapters.queryAdapter(root, ITraverser)
        if traverser is None:
            traverser = ResourceTreeTraverser(root)
        tdict = traverser(request)
 
        context, view_name, subpath, traversed, vroot, vroot_path = (
            tdict['context'],
            tdict['view_name'],
            tdict['subpath'],
            tdict['traversed'],
            tdict['virtual_root'],
            tdict['virtual_root_path'],
        )
 
        attrs.update(tdict)
 
        # Notify anyone listening that we have a context and traversal is
        # complete
        has_listeners and notify(ContextFound(request))
 
        # find a view callable
        context_iface = providedBy(context)
        response = _call_view(
            registry, request, context, context_iface, view_name
        )
 
        if response is None:
            if self.debug_notfound:
                msg = (
                    'debug_notfound of url %s; path_info: %r, '
                    'context: %r, view_name: %r, subpath: %r, '
                    'traversed: %r, root: %r, vroot: %r, '
                    'vroot_path: %r'
                    % (
                        request.url,
                        request.path_info,
                        context,
                        view_name,
                        subpath,
                        traversed,
                        root,
                        vroot,
                        vroot_path,
                    )
                )
                logger and logger.debug(msg)
            else:
                msg = request.path_info
            raise HTTPNotFound(msg)
 
        return response
 
    def invoke_subrequest(self, request, use_tweens=False):
        """Obtain a response object from the Pyramid application based on
        information in the ``request`` object provided.  The ``request``
        object must be an object that implements the Pyramid request
        interface (such as a :class:`pyramid.request.Request` instance).  If
        ``use_tweens`` is ``True``, the request will be sent to the
        :term:`tween` in the tween stack closest to the request ingress.  If
        ``use_tweens`` is ``False``, the request will be sent to the main
        router handler, and no tweens will be invoked.
 
        See the API for pyramid.request for complete documentation.
        """
        request.registry = self.registry
        request.invoke_subrequest = self.invoke_subrequest
        extensions = self.request_extensions
        if extensions is not None:
            apply_request_extensions(request, extensions=extensions)
        with RequestContext(request):
            return self.invoke_request(request, _use_tweens=use_tweens)
 
    def request_context(self, environ):
        """
        Create a new request context from a WSGI environ.
 
        The request context is used to push/pop the threadlocals required
        when processing the request. It also contains an initialized
        :class:`pyramid.interfaces.IRequest` instance using the registered
        :class:`pyramid.interfaces.IRequestFactory`. The context may be
        used as a context manager to control the threadlocal lifecycle:
 
        .. code-block:: python
 
            with router.request_context(environ) as request:
                ...
 
        Alternatively, the context may be used without the ``with`` statement
        by manually invoking its ``begin()`` and ``end()`` methods.
 
        .. code-block:: python
 
            ctx = router.request_context(environ)
            request = ctx.begin()
            try:
                ...
            finally:
                ctx.end()
 
        """
        request = self.request_factory(environ)
        request.registry = self.registry
        request.invoke_subrequest = self.invoke_subrequest
        extensions = self.request_extensions
        if extensions is not None:
            apply_request_extensions(request, extensions=extensions)
        return RequestContext(request)
 
    def invoke_request(self, request, _use_tweens=True):
        """
        Execute a request through the request processing pipeline and
        return the generated response.
 
        """
        registry = self.registry
        has_listeners = registry.has_listeners
        notify = registry.notify
 
        if _use_tweens:
            handle_request = self.handle_request
        else:
            handle_request = self.orig_handle_request
 
        try:
            response = handle_request(request)
 
            if request.response_callbacks:
                request._process_response_callbacks(response)
 
            has_listeners and notify(NewResponse(request, response))
 
            return response
 
        finally:
            if request.finished_callbacks:
                request._process_finished_callbacks()
 
    def __call__(self, environ, start_response):
        """
        Accept ``environ`` and ``start_response``; create a
        :term:`request` and route the request to a :app:`Pyramid`
        view based on introspection of :term:`view configuration`
        within the application registry; call ``start_response`` and
        return an iterable.
        """
        response = self.execution_policy(environ, self)
        return response(environ, start_response)
 
 
def default_execution_policy(environ, router):
    with router.request_context(environ) as request:
        try:
            return router.invoke_request(request)
        except Exception:
            return request.invoke_exception_view(reraise=True)