B
    `&                 @   s   d dl mZmZ d dlmZmZmZmZ d dlm	Z	 d dl
mZmZmZmZmZmZmZmZmZmZmZ d dlmZmZ d dlmZ d dlmZmZ d dlmZ eeG d	d
 d
Z dd Z!dS )    )implementer
providedBy)BeforeTraversalContextFound
NewRequestNewResponse)HTTPNotFound)IDebugLoggerIExecutionPolicyIRequestIRequestExtensionsIRequestFactoryIRootFactoryIRouterIRouteRequestIRoutesMapper
ITraverserITweens)Requestapply_request_extensions)RequestContext)DefaultRootFactoryResourceTreeTraverser)
_call_viewc               @   sP   e Zd ZdZdZdd Zdd ZdddZdd	 ZdddZ	dd Z
dd ZdS )RouterFc             C   s   |j }|t| _|ttd| _|t| _|tt	d| _
|t| _|ttd| _| j| _|t}|d k	rt|| j|| _| j| _|| _|j}|d k	r|d | _|d | _d S )N)defaultdebug_notfounddebug_routematch)queryUtilityr	   loggerr   r   root_factoryr   routes_mapperr   r   request_factoryr   request_extensionsr
   default_execution_policyexecution_policyhandle_requestorig_handle_requestr   Zroot_policyregistrysettingsr   r   )selfr(   qZtweensr)    r,   V/home/kop/projects/devel/pgwui/test_venv/lib/python3.7/site-packages/pyramid/router.py__init__#   s$    



zRouter.__init__c          
   C   s  |j }|d }t|_d }| j}| j}|j}|j}|j}	| j}
|oJ|	t	| | j
}|d k	r||}|d |d  }}|d kr|rd|j }|
o|
| nr||d< ||d< |rd|j|j|j|j|dd	d
 |jD f }|
o|
| |jt|jtd|_|jp| j
}|o|	t| ||}||d< ||t}|d krNt|}||}|d |d |d |d |d |d f\}}}}}}|| |o|	t| t|}t|||||}|d kr| jr d|j|j|||||||f	 }|
o|
| n|j}t||S )Nr(   matchroutezno route matched for url %sZ	matchdictZmatched_routezcroute matched for url %s; route_name: %r, path_info: %r, pattern: %r, matchdict: %r, predicates: %rz, c             S   s   g | ]}|  qS r,   )text).0pr,   r,   r-   
<listcomp>a   s    z)Router.handle_request.<locals>.<listcomp>)namer   rootcontext	view_namesubpath	traversedZvirtual_rootZvirtual_root_pathzdebug_notfound of url %s; path_info: %r, context: %r, view_name: %r, subpath: %r, traversed: %r, root: %r, vroot: %r, vroot_path: %r)__dict__r   Zrequest_ifacer!   r   adaptershas_listenersnotifyr   r   r    urldebugr5   Z	path_infopatternjoinZ
predicatesr   r   factoryr   ZqueryAdapterr   r   updater   r   r   r   r   )r*   requestattrsr(   r7   r!   r   r<   r=   r>   r   r    infor/   r0   msgr6   Z	traverserZtdictr8   r9   r:   ZvrootZ
vroot_pathZcontext_ifaceresponser,   r,   r-   r&   8   s    




zRouter.handle_requestc          	   C   sL   | j |_ | j|_| j}|dk	r*t||d t| | j||dS Q R X dS )ay  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.
        N)
extensions)_use_tweens)r(   invoke_subrequestr#   r   r   invoke_request)r*   rE   Z
use_tweensrJ   r,   r,   r-   rL      s    
zRouter.invoke_subrequestc             C   s<   |  |}| j|_| j|_| j}|dk	r4t||d t|S )a}  
        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()

        N)rJ   )r"   r(   rL   r#   r   r   )r*   environrE   rJ   r,   r,   r-   request_context   s    
zRouter.request_contextTc          	   C   sd   | j }|j}|j}|r| j}n| j}z.||}|jr>|| |oN|t|| |S | | X dS )zw
        Execute a request through the request processing pipeline and
        return the generated response.

        N)	r(   r=   r>   r&   r'   Zresponse_callbacksZ_process_response_callbacksr   finish_request)r*   rE   rK   r(   r=   r>   r&   rI   r,   r,   r-   rM      s    
zRouter.invoke_requestc             C   s    |j r|  |jdd  d S )Nr7   )Zfinished_callbacksZ_process_finished_callbacksr;   pop)r*   rE   r,   r,   r-   rP     s    zRouter.finish_requestc             C   s   |  || }|||S )a*  
        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.
        )r%   )r*   rN   Zstart_responserI   r,   r,   r-   __call__  s    zRouter.__call__N)F)T)__name__
__module____qualname__r   r   r.   r&   rL   rO   rM   rP   rR   r,   r,   r,   r-   r      s   u
$
r   c          	   C   s    | | }||S Q R X d S )N)rO   rM   )rN   ZrouterrE   r,   r,   r-   r$     s    r$   N)"Zzope.interfacer   r   Zpyramid.eventsr   r   r   r   Zpyramid.httpexceptionsr   Zpyramid.interfacesr	   r
   r   r   r   r   r   r   r   r   r   Zpyramid.requestr   r   Zpyramid.threadlocalr   Zpyramid.traversalr   r   Zpyramid.viewr   r   r$   r,   r,   r,   r-   <module>   s   4 v