B
    `>                 @   s<  d dl mZ d dlZd dlZd dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZmZmZmZ d d	lmZmZ d d
lmZmZ d dlmZ d dlmZmZmZmZm Z  d dl!m"Z" G dd dZ#G dd dZ$eeG dd deee$eeeee"
Z%dddZ&dd Z'dd Z(d ddZ)G dd dZ*dS )!    )dequeN)BaseRequest)implementer)InterfaceClass)reify)LocalizerRequestMixin)IRequestIRequestExtensions	IResponseISessionFactory)Response_get_response_factory)AuthenticationAPIMixinSecurityAPIMixin)URLMethodsMixin)InstancePropertyHelperInstancePropertyMixinSentinelbytes_text_)ViewMethodsMixinc               @   s   e Zd ZdS )TemplateContextN)__name__
__module____qualname__ r   r   W/home/kop/projects/devel/pgwui/test_venv/lib/python3.7/site-packages/pyramid/request.pyr      s   r   c               @   sD   e Zd Zedd Zedd Zdd Zdd Zd	d
 Zdd Z	dS )CallbackMethodsMixinc             C   s   t  S )N)r   )selfr   r   r   finished_callbacks"   s    z'CallbackMethodsMixin.finished_callbacksc             C   s   t  S )N)r   )r   r   r   r   response_callbacks&   s    z'CallbackMethodsMixin.response_callbacksc             C   s   | j | dS )a  
        Add a callback to the set of callbacks to be called by the
        :term:`router` at a point after a :term:`response` object is
        successfully created.  :app:`Pyramid` does not have a
        global response object: this functionality allows an
        application to register an action to be performed against the
        response once one is created.

        A 'callback' is a callable which accepts two positional
        parameters: ``request`` and ``response``.  For example:

        .. code-block:: python
           :linenos:

           def cache_callback(request, response):
               'Set the cache_control max_age for the response'
               response.cache_control.max_age = 360
           request.add_response_callback(cache_callback)

        Response callbacks are called in the order they're added
        (first-to-most-recently-added).  No response callback is
        called if an exception happens in application code, or if the
        response object returned by :term:`view` code is invalid.

        All response callbacks are called *after* the tweens and
        *before* the :class:`pyramid.events.NewResponse` event is sent.

        Errors raised by callbacks are not handled specially.  They
        will be propagated to the caller of the :app:`Pyramid`
        router application.

        .. seealso::

            See also :ref:`using_response_callbacks`.
        N)r    append)r   callbackr   r   r   add_response_callback*   s    %z*CallbackMethodsMixin.add_response_callbackc             C   s&   | j }x|r | }|| | qW d S )N)r    popleft)r   response	callbacksr"   r   r   r   _process_response_callbacksQ   s    z0CallbackMethodsMixin._process_response_callbacksc             C   s   | j | dS )av  
        Add a callback to the set of callbacks to be called
        unconditionally by the :term:`router` at the very end of
        request processing.

        ``callback`` is a callable which accepts a single positional
        parameter: ``request``.  For example:

        .. code-block:: python
           :linenos:

           import transaction

           def commit_callback(request):
               '''commit or abort the transaction associated with request'''
               if request.exception is not None:
                   transaction.abort()
               else:
                   transaction.commit()
           request.add_finished_callback(commit_callback)

        Finished callbacks are called in the order they're added (
        first- to most-recently- added).  Finished callbacks (unlike
        response callbacks) are *always* called, even if an exception
        happens in application code that prevents a response from
        being generated.

        The set of finished callbacks associated with a request are
        called *very late* in the processing of that request; they are
        essentially the last thing called by the :term:`router`. They
        are called after response processing has already occurred in a
        top-level ``finally:`` block within the router request
        processing code.  As a result, mutations performed to the
        ``request`` provided to a finished callback will have no
        meaningful effect, because response processing will have
        already occurred, and the request's scope will expire almost
        immediately after all finished callbacks have been processed.

        Errors raised by finished callbacks are not handled specially.
        They will be propagated to the caller of the :app:`Pyramid`
        router application.

        .. seealso::

            See also :ref:`using_finished_callbacks`.
        N)r   r!   )r   r"   r   r   r   add_finished_callbackW   s    /z*CallbackMethodsMixin.add_finished_callbackc             C   s$   | j }x|r| }||  qW d S )N)r   r$   )r   r&   r"   r   r   r   _process_finished_callbacks   s    z0CallbackMethodsMixin._process_finished_callbacksN)
r   r   r   r   r   r    r#   r'   r(   r)   r   r   r   r   r   !   s   '1r   c               @   sT   e Zd ZdZdZdZdZdZeZ	e
Zedd Zedd Zedd Zd	d
 ZdS )Requesta  
    A subclass of the :term:`WebOb` Request class.  An instance of
    this class is created by the :term:`router` and is provided to a
    view callable (and to other subsystems) as the ``request``
    argument.

    The documentation below (save for the ``add_response_callback`` and
    ``add_finished_callback`` methods, which are defined in this subclass
    itself, and the attributes ``context``, ``registry``, ``root``,
    ``subpath``, ``traversed``, ``view_name``, ``virtual_root`` , and
    ``virtual_root_path``, each of which is added to the request by the
    :term:`router` at request ingress time) are autogenerated from the WebOb
    source code used when this documentation was generated.

    Due to technical constraints, we can't yet display the WebOb
    version number from which this documentation is autogenerated, but
    it will be the 'prevailing WebOb version' at the time of the
    release of this :app:`Pyramid` version.  See
    https://webob.org/ for further information.
    Nc             C   s   t  S )N)r   )r   r   r   r   tmpl_context   s    zRequest.tmpl_contextc             C   s$   | j t}|dkrtd|| S )zObtain the :term:`session` object associated with this
        request.  If a :term:`session factory` has not been registered
        during application configuration, a
        :class:`pyramid.exceptions.ConfigurationError` will be raisedNzUNo session factory registered (see the Sessions chapter of the Pyramid documentation))registryqueryUtilityr   AttributeError)r   factoryr   r   r   session   s
    zRequest.sessionc             C   s   t | j}|| S )a,  This attribute is actually a "reified" property which returns an
        instance of the :class:`pyramid.response.Response`. class.  The
        response object returned does not exist until this attribute is
        accessed.  Subsequent accesses will return the same Response object.

        The ``request.response`` API is used by renderers.  A render obtains
        the response object it will return from a view that uses that renderer
        by accessing ``request.response``.  Therefore, it's possible to use the
        ``request.response`` API to set up a response object with "the
        right" attributes (e.g. by calling ``request.response.set_cookie()``)
        within a view that uses a renderer.  Mutations to this response object
        will be preserved in the response sent to the client.)r   r,   )r   Zresponse_factoryr   r   r   r%      s    
zRequest.responsec             C   s4   |j tkrdS | j}||t}|dkr,dS ||kS )zgReturn ``True`` if the object passed as ``ob`` is a valid
        response object, ``False`` otherwise.TNF)	__class__r   r,   ZqueryAdapterOrSelfr
   )r   obr,   Zadaptedr   r   r   is_response   s    
zRequest.is_response)r   r   r   __doc__	exceptionexc_infoZ	matchdictZmatched_router   Zrequest_ifacer   ZResponseClassr   r+   r0   r%   r3   r   r   r   r   r*      s   r*   r   c             C   s.   t d|  |dd}t d|  |tfdd|_|S )Nz%s_IRequestz'route_request_iface-generated interface)basesr4   z%s_combined_IRequestz0route_request_iface-generated combined interface)r   r   Zcombined)namer7   Zifacer   r   r   route_request_iface   s    
r9   c                s    fdd}|  | d S )Nc                s&   x  D ]\}}|j ||f qW d S )N)
headerlistr!   )requestr%   kv)r:   r   r   add_headers   s    z0add_global_response_headers.<locals>.add_headers)r#   )r;   r:   r>   r   )r:   r   add_global_response_headers   s    r?   c             C   s  | j }|dd}|dd}tt| dd}d}dddd |D  }|dkrl|dkrl|drl|d7 }|| d}g }	x6|r|	|krP | }
|
r|	d	t	t
|
d
d qW x |r|d dkr|d d }qW d|}|  }||j d< ||j d< ||S )NZSCRIPT_NAME Z	PATH_INFO/subpathr   c             S   s   g | ]}t |d dqS )zutf-8zlatin-1)r   encode).0xr   r   r   
<listcomp>  s    z6call_app_with_subpath_as_path_info.<locals>.<listcomp>r   zlatin-1zutf-8)environgetlistgetattrjoinendswithsplitpopinsertr   r   copyZget_response)r;   ZapprH   script_nameZ	path_inforB   Znew_script_nameZnew_path_infoZworkbacktmpelZnew_requestr   r   r   "call_app_with_subpath_as_path_info  s4    



rU   c             C   s`   |dkr| j t}|dk	r\x0|j D ]"\}}|| | j}t| || q(W t	| |j
 dS )a~  Apply request extensions (methods and properties) to an instance of
    :class:`pyramid.interfaces.IRequest`. This method is dependent on the
    ``request`` containing a properly initialized registry.

    After invoking this method, the ``request`` should have the methods
    and properties that were defined using
    :meth:`pyramid.config.Configurator.add_request_method`.
    N)r,   r-   r	   methodsitems__get__r1   setattrr   Zapply_propertiesZdescriptors)r;   
extensionsr8   fnmethodr   r   r   apply_request_extensions=  s    	r]   c               @   sP   e Zd ZdZedZdddZdd Zddd	Zefd
dZ	dd Z
dd ZdS )RequestLocalCachea  
    A store that caches values during for the lifecycle of a request.

    Wrapping Functions

    Instantiate and use it to decorate functions that accept a request
    parameter. The result is cached and returned in subsequent invocations
    of the function.

    .. code-block:: python

        @RequestLocalCache()
        def get_user(request):
            result = ...  # do some expensive computations
            return result

        value = get_user(request)

        # manipulate the cache directly
        get_user.cache.clear(request)

    The cache instance is attached to the resulting function as the ``cache``
    attribute such that the function may be used to manipulate the cache.

    Wrapping Methods

    A method can be used as the creator function but it needs to be bound to
    an instance such that it only accepts one argument - the request. An easy
    way to do this is to bind the creator in the constructor and then use
    :meth:`.get_or_create`:

    .. code-block:: python

        class SecurityPolicy:
            def __init__(self):
                self.identity_cache = RequestLocalCache(self.load_identity)

            def load_identity(self, request):
                result = ...  # do some expensive computations
                return result

            def identity(self, request):
                return self.identity_cache.get_or_create(request)

    The cache maintains a weakref to each request and will release the cached
    values when the request is garbage-collected. However, in most scenarios,
    it will release resources earlier via
    :meth:`pyramid.request.Request.add_finished_callback`.

    .. versionadded:: 2.0

    NO_VALUENc             C   s   t  | _|| _d S )N)weakrefWeakKeyDictionary_store_creator)r   creatorr   r   r   __init__  s    
zRequestLocalCache.__init__c                s(   t   fdd| _ | _S )Nc                s   j |  S )N)cacheget_or_create)r;   )r[   wrapperr   r   rh     s    z+RequestLocalCache.__call__.<locals>.wrapper)	functoolswrapsrf   rc   )r   r[   r   )r[   rh   r   __call__  s    zRequestLocalCache.__call__c             C   sP   | j || j}|| jkrL|dkr8| j}|dkr8td||}| || |S )a>  
        Return the value from the cache. Compute if necessary.

        If no value is cached then execute the creator, cache the result,
        and return it.

        The creator may be passed in as an argument or bound to the cache
        by decorating a function or supplied as a constructor argument.

        NzUno creator function has been registered with the cache or supplied to "get_or_create")rb   rI   r_   rc   
ValueErrorset)r   r;   rd   resultr   r   r   rg     s    
zRequestLocalCache.get_or_createc             C   s   | j ||S )zi
        Return the value from the cache.

        The cached value is returned or ``default``.

        )rb   rI   )r   r;   defaultr   r   r   rI     s    zRequestLocalCache.getc             C   s*   || j k}|| j |< |s&|| j j dS )z5
        Update the cache with a new value.

        N)rb   r(   rO   )r   r;   valueZalready_setr   r   r   rm     s    

zRequestLocalCache.setc             C   s*   | j }|| jkr&| j| }| j | j|< |S )zo
        Delete the value from the cache.

        The cached value is returned or :attr:`.NO_VALUE`.

        )r_   rb   )r   r;   	old_valuer   r   r   clear  s
    

zRequestLocalCache.clear)N)N)r   r   r   r4   r   r_   re   rk   rg   rI   rm   rr   r   r   r   r   r^   R  s   4
	
	r^   )r   )N)+collectionsr   ri   r`   Zwebobr   Zzope.interfacer   Zzope.interface.interfacer   Zpyramid.decoratorr   Zpyramid.i18nr   Zpyramid.interfacesr   r	   r
   r   Zpyramid.responser   r   Zpyramid.securityr   r   Zpyramid.urlr   Zpyramid.utilr   r   r   r   r   Zpyramid.viewr   r   r   r*   r9   r?   rU   r]   r^   r   r   r   r   <module>   s:   nP
8
