B
    æ¹`í+  ã               @   sŠ   d Z ddlZddlZdadaddddgZdd„ ZG d	d„ deƒZeƒ Z	G d
d„ deƒZ
dd„ Ze
j ZG dd„ deƒZddd„Zej e_ dS )z*Paste Configuration Middleware and Objectsé    NÚDispatchingConfigÚCONFIGÚConfigMiddlewareÚPrefixMiddlewarec              C   sN   yt jS  tk
r,   t ¡ a i  t _} | S  tk
rH   i  t _} | S X d S )N)Zconfig_localZ	wsgi_dictÚ	NameErrorÚ	threadingÚlocalÚAttributeError)Úresult© r   ú[/home/kop/projects/devel/pgwui/test_venv/lib/python3.7/site-packages/paste/deploy/config.pyÚ
local_dict   s    

r   c               @   st   e Zd ZdZe ¡ Zdd„ Zdd„ Zddd„Z	d	d
„ Z
dd„ Zddd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )r   a  
    This is a configuration object that can be used globally,
    imported, have references held onto.  The configuration may differ
    by thread (or may not).

    Specific configurations are registered (and deregistered) either
    for the process or for threads.
    c             C   sZ   | j  ¡  z8d| _x,d| j | _| jtƒ kr.P |  jd7  _qW W d | j  ¡  X g | _d S )Nr   zpaste.processconfig_%ié   )Ú_constructor_lockÚacquireZdispatching_idÚ
_local_keyr   ÚreleaseÚ_process_configs)Úselfr   r   r   Ú__init__,   s    
zDispatchingConfig.__init__c             C   s   t ƒ  | jg ¡ |¡ dS )aŸ  
        Make ``conf`` the active configuration for this thread.
        Thread-local configuration always overrides process-wide
        configuration.

        This should be used like::

            conf = make_conf()
            dispatching_config.push_thread_config(conf)
            try:
                ... do stuff ...
            finally:
                dispatching_config.pop_thread_config(conf)
        N)r   Ú
setdefaultr   Úappend)r   Úconfr   r   r   Úpush_thread_config9   s    z$DispatchingConfig.push_thread_configNc             C   s   |   tƒ | j |¡ dS )z¹
        Remove a thread-local configuration.  If ``conf`` is given,
        it is checked against the popped configuration and an error
        is emitted if they don't match.
        N)Ú	_pop_fromr   r   )r   r   r   r   r   Úpop_thread_configJ   s    z#DispatchingConfig.pop_thread_configc             C   s,   |  ¡ }|d k	r(||k	r(td||f ƒ‚d S )NzBThe config popped (%s) is not the same as the config expected (%s))ÚpopÚAssertionError)r   Úlstr   Úpoppedr   r   r   r   R   s
    zDispatchingConfig._pop_fromc             C   s   | j  |¡ dS )zg
        Like push_thread_config, but applies the configuration to
        the entire process.
        N)r   r   )r   r   r   r   r   Úpush_process_configZ   s    z%DispatchingConfig.push_process_configc             C   s   |   | j|¡ d S )N)r   r   )r   r   r   r   r   Úpop_process_configa   s    z$DispatchingConfig.pop_process_configc             C   s"   |   ¡ }|d krtdƒ‚t||ƒS )Nz?No configuration has been registered for this process or thread)Úcurrent_confr	   Úgetattr)r   Úattrr   r   r   r   Ú__getattr__d   s
    zDispatchingConfig.__getattr__c             C   s2   t ƒ  | j¡}|r|d S | jr*| jd S d S d S )Néÿÿÿÿ)r   Úgetr   r   )r   Zthread_configsr   r   r   r"   l   s    
zDispatchingConfig.current_confc             C   s    |   ¡ }|d krtdƒ‚|| S )Nz?No configuration has been registered for this process or thread)r"   Ú	TypeError)r   Úkeyr   r   r   r   Ú__getitem__u   s
    zDispatchingConfig.__getitem__c             C   s   || kS )Nr   )r   r)   r   r   r   Ú__contains__~   s    zDispatchingConfig.__contains__c             C   s   |   ¡ }|||< d S )N)r"   )r   r)   Úvaluer   r   r   r   Ú__setitem__‚   s    zDispatchingConfig.__setitem__)N)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚLockr   r   r   r   r   r    r!   r%   r"   r*   r+   r-   r   r   r   r   r      s   	

		c               @   s    e Zd ZdZdd„ Zdd„ ZdS )r   zÌ
    A WSGI middleware that adds a ``paste.config`` key to the request
    environment, as well as registering the configuration temporarily
    (for the length of the request) with ``paste.CONFIG``.
    c             C   s   || _ || _dS )zv
        This delegates all requests to `application`, adding a *copy*
        of the configuration `config`.
        N)ÚapplicationÚconfig)r   r3   r4   r   r   r   r   ’   s    zConfigMiddleware.__init__c          	      sÞ   t d kr&dd l}| d¡ ddlm a  d }d|kr:|d }| j ¡  ‰ |d< d }t ˆ ¡ z|  ||¡}W d |d krŽt 	ˆ ¡ |d k	rŽ||d< X t
|ƒttfkr¾t 	ˆ ¡ |d k	rº||d< |S ‡ fdd„}t  ||¡}|S d S )Nr   ZPaste)Úwsgilibzpaste.configc                  s   t  ˆ ¡ d S )N)r   r   r   )r   r   r   Úclose_config·   s    z/ConfigMiddleware.__call__.<locals>.close_config)r5   Úpkg_resourcesÚrequireZpaster4   Úcopyr   r   r3   r   ÚtypeÚlistÚtupleZ	add_close)r   ÚenvironÚstart_responser7   Zpopped_configZapp_iterr6   Znew_app_iterr   )r   r   Ú__call__š   s0    




zConfigMiddleware.__call__N)r.   r/   r0   r1   r   r?   r   r   r   r   r   Š   s   c             K   s   |  ¡ }| |¡ t| |ƒS )N)r9   Úupdater   )ÚappÚglobal_confZ
local_confr   r   r   r   Úmake_config_filter½   s    
rC   c               @   s"   e Zd ZdZd	dd„Zdd„ ZdS )
r   a	  Translate a given prefix into a SCRIPT_NAME for the filtered
    application.

    PrefixMiddleware provides a way to manually override the root prefix
    (SCRIPT_NAME) of your application for certain, rare situations.

    When running an application under a prefix (such as '/james') in
    FastCGI/apache, the SCRIPT_NAME environment variable is automatically
    set to to the appropriate value: '/james'. Pylons' URL generating
    functions, such as url_for, always take the SCRIPT_NAME value into account.

    One situation where PrefixMiddleware is required is when an application
    is accessed via a reverse proxy with a prefix. The application is accessed
    through the reverse proxy via the the URL prefix '/james', whereas the
    reverse proxy forwards those requests to the application at the prefix '/'.

    The reverse proxy, being an entirely separate web server, has no way of
    specifying the SCRIPT_NAME variable; it must be manually set by a
    PrefixMiddleware instance. Without setting SCRIPT_NAME, url_for will
    generate URLs such as: '/purchase_orders/1', when it should be
    generating: '/james/purchase_orders/1'.

    To filter your application through a PrefixMiddleware instance, add the
    following to the '[app:main]' section of your .ini file:

    .. code-block:: ini

        filter-with = proxy-prefix

        [filter:proxy-prefix]
        use = egg:PasteDeploy#prefix
        prefix = /james

    The name ``proxy-prefix`` simply acts as an identifier of the filter
    section; feel free to rename it.

    Also, unless disabled, the ``X-Forwarded-Server`` header will be
    translated to the ``Host`` header, for cases when that header is
    lost in the proxying.  Also ``X-Forwarded-Host``,
    ``X-Forwarded-Scheme``, and ``X-Forwarded-Proto`` are translated.

    If ``force_port`` is set, SERVER_PORT and HTTP_HOST will be
    rewritten with the given port.  You can use a number, string (like
    '80') or the empty string (whatever is the default port for the
    scheme).  This is useful in situations where there is port
    forwarding going on, and the server believes itself to be on a
    different port than what the outside world sees.

    You can also use ``scheme`` to explicitly set the scheme (like
    ``scheme = https``).
    Nú/Tc             C   s:   || _ | d¡| _|| _t d| j ¡| _|| _|| _d S )NrD   z^%s(.*)$)	rA   ÚrstripÚprefixÚtranslate_forwarded_serverÚreÚcompileÚ	regprefixÚ
force_portÚscheme)r   rA   rB   rF   rG   rK   rL   r   r   r   r   ù   s    zPrefixMiddleware.__init__c             C   sb  |d }t  | jd|¡}|s d}||d< | j|d< | jrÎd|kr`| d¡ d¡d  |d< |d	< d
|kr€| d
¡ d¡d |d	< d|kr | d¡ d¡d |d< d|kr¸| d¡|d< nd|krÎ| d¡|d< | jd k	r@| d	d¡ dd¡d }| jrd|| jf }t	| jƒ|d< n |d dkr,d}nd}||d< ||d	< | j
d k	rV| j
|d< |  ||¡S )NZ	PATH_INFOz\1rD   ZSCRIPT_NAMEZHTTP_X_FORWARDED_SERVERú,r   ZSERVER_NAMEZ	HTTP_HOSTZHTTP_X_FORWARDED_HOSTZHTTP_X_FORWARDED_FORZREMOTE_ADDRZHTTP_X_FORWARDED_SCHEMEzwsgi.url_schemeZHTTP_X_FORWARDED_PROTOÚ ú:r   z%s:%sZSERVER_PORTÚhttpÚ80Ú443)rH   ÚsubrJ   rF   rG   r   ÚsplitrK   r'   ÚstrrL   rA   )r   r=   r>   ÚurlÚhostÚportr   r   r   r?     s<    
 
zPrefixMiddleware.__call__)NrD   TNN)r.   r/   r0   r1   r   r?   r   r   r   r   r   Å   s
   3  
rD   Tc             C   s&   ddl m} ||ƒ}t| ||||dS )Nr   )Úasbool)rF   rG   rK   rL   )Zpaste.deploy.convertersrY   r   )rA   rB   rF   rG   rK   rL   rY   r   r   r   Úmake_prefix_middleware&  s    rZ   )rD   TNN)r1   r   rH   r5   r   Ú__all__r   Úobjectr   r   r   rC   Zmake_config_middlewarer   rZ   r   r   r   r   Ú<module>   s    l3b  
