B
    `d                 @   s  d dl mZ d dlZd dlZd dlZd dlZd dlmZmZm	Z	m
Z
 d dlmZmZ ddddgZd	d
 Zdd Zdd ZG dd deZG dd deZG dd deZe ZG dd deZe ZG dd deZe ZG dd deZe ZG dd deZe ZG dd deZe Z d<ddZ!d=d dZ"d>d!dZ#d?d"dZ$i Z%d@d#d$Z&dAd%d&Z'd'd( Z(e(e%d)< d*d+ Z)e)e%d,< d-d. Z*e*e%d/< G d0d1 d1eZ+G d2d3 d3e+Z,G d4d5 d5e+Z-G d6d7 d7e+Z.G d8d9 d9eZ/G d:d; d;e0Z1dS )B    )with_statementN)ConfigParserunquote	iteritemsdictkeys)fix_calllookup_objectloadapp
loadserver
loadfilter	appconfigc             C   s0   t jd|  }t|dr"| S |dS d S )Nzx=resolveF)pkg_resources
EntryPointparsehasattrr   load)sep r   ]/home/kop/projects/devel/pgwui/test_venv/lib/python3.7/site-packages/paste/deploy/loadwsgi.pyimport_string   s    
r   c             C   s(   | dkrg S t | ttfr| S | gS dS )z
    Turn object into a list; lists and tuples are left as-is, None
    becomes [], and everything else turns into a one-element list.
    N)
isinstancelisttuple)objr   r   r   _aslist   s
    r   c             C   s8   t | ttfs| gS g }x| D ]}|t| qW |S )z 
    Flatten a nested list.
    )r   r   r   extend_flatten)lstresultitemr   r   r   r   ,   s    
r   c               @   sB   e Zd Zdd ZeedejZdd Zdd Z	G dd	 d	e
Zd
S )NicerConfigParserc             O   s4   t j| f|| || _t| dr0| | j| _d S )N_interpolation)r   __init__filenamer   InterpolateWrapperr#   )selfr%   argskwr   r   r   r$   :   s    
zNicerConfigParser.__init__	read_filec             C   s<   t |  }x(t|D ]\}}| d|p.|||< qW |S )zReturn the defaults, with their values interpolated (with the
        defaults dict itself)

        Mainly to support defaults using values such as %(here)s
        DEFAULT)r   defaultscopyr   get)r'   r,   keyvalr   r   r   r,   B   s    zNicerConfigParser.defaultsc             C   sj   yt | ||||S  tk
rd   t d }t|j}d| j|f |d< t||_|d |_	 Y nX d S )N   zError in file %s: %sr   )
r   _interpolate	Exceptionsysexc_infor   r(   r%   r   message)r'   sectionoptionrawvalvarser(   r   r   r   r2   M   s    


zNicerConfigParser._interpolatec               @   s$   e Zd Zdd Zdd Zdd ZdS )z$NicerConfigParser.InterpolateWrapperc             C   s
   || _ d S )N)	_original)r'   originalr   r   r   r$   \   s    z-NicerConfigParser.InterpolateWrapper.__init__c             C   s   t | j|S )N)getattrr<   )r'   namer   r   r   __getattr___   s    z0NicerConfigParser.InterpolateWrapper.__getattr__c             C   sl   y| j |||||S  tk
rf   t d }t|j}d|j|f |d< t||_|d |_	 Y nX d S )Nr1   zError in file %s: %sr   )
r<   
before_getr3   r4   r5   r   r(   r%   r   r6   )r'   parserr7   r8   valuer,   r;   r(   r   r   r   rA   b   s    


z/NicerConfigParser.InterpolateWrapper.before_getN)__name__
__module____qualname__r$   r@   rA   r   r   r   r   r&   Z   s   r&   N)rD   rE   rF   r$   r>   r   readfpr*   r,   r2   objectr&   r   r   r   r   r"   8   s
   r"   c               @   s0   e Zd ZdZdZdZdd Zdd Zdd ZdS )_ObjectTypeNc             C   s0   dd t | jD | _dd t | jD | _d S )Nc             S   s   g | ]}t |qS r   )r   ).0pr   r   r   
<listcomp>|   s    z(_ObjectType.__init__.<locals>.<listcomp>c             S   s   g | ]}t |qS r   )r   )rJ   rK   r   r   r   rL   }   s    )r   egg_protocolsconfig_prefixes)r'   r   r   r   r$   z   s    z_ObjectType.__init__c             C   s   d| j | j| jf S )Nz<%s protocols=%r prefixes=%r>)r?   rM   rN   )r'   r   r   r   __repr__   s    z_ObjectType.__repr__c             C   s(   |j t| jkstt|j|jf|jS )N)protocolr   rM   AssertionErrorr   rH   global_conf
local_conf)r'   contextr   r   r   invoke   s    z_ObjectType.invoke)	rD   rE   rF   r?   rM   rN   r$   rO   rU   r   r   r   r   rI   t   s   rI   c               @   s6   e Zd ZdZdddgZddgddgdd	gZd
d ZdS )_Appapplicationzpaste.app_factoryzpaste.composite_factoryzpaste.composit_factoryapp	compositecompositpipelinez
filter-appc             C   sV   |j dkr"t|j|j|jf|jS |j dkr@t|j|jf|jS dsRtd|j  d S )N)zpaste.composit_factoryzpaste.composite_factoryzpaste.app_factoryr   zProtocol %r unknown)rP   r   rH   loaderrR   rS   rQ   )r'   rT   r   r   r   rU      s    


z_App.invokeN)rD   rE   rF   r?   rM   rN   rU   r   r   r   r   rV      s   rV   c               @   s(   e Zd ZdZddggZdgZdd ZdS )_Filterfilterzpaste.filter_factoryzpaste.filter_app_factoryc                sN    j dkrt j jf jS  j dkr8 fdd}|S dsJtd j  d S )Nzpaste.filter_factoryzpaste.filter_app_factoryc                s   t  j|  jf jS )N)r   rH   rR   rS   )wsgi_app)rT   r   r   filter_wrapper   s    z&_Filter.invoke.<locals>.filter_wrapperr   zProtocol %r unknown)rP   r   rH   rR   rS   rQ   )r'   rT   r`   r   )rT   r   rU      s    

z_Filter.invokeN)rD   rE   rF   r?   rM   rN   rU   r   r   r   r   r]      s   
r]   c               @   s(   e Zd ZdZddggZdgZdd ZdS )_Serverserverzpaste.server_factoryzpaste.server_runnerc                sN    j dkrt j jf jS  j dkr8 fdd}|S dsJtd j  d S )Nzpaste.server_factoryzpaste.server_runnerc                s   t  j|  jf jS )N)r   rH   rR   rS   )r_   )rT   r   r   server_wrapper   s    z&_Server.invoke.<locals>.server_wrapperr   zProtocol %r unknown)rP   r   rH   rR   rS   rQ   )r'   rT   rc   r   )rT   r   rU      s    

z_Server.invokeN)rD   rE   rF   r?   rM   rN   rU   r   r   r   r   ra      s   
ra   c               @   s   e Zd ZdZdd ZdS )	_PipeLiner[   c             C   s<   |j  }dd |jD }|  x|D ]}||}q(W |S )Nc             S   s   g | ]}|  qS r   )create)rJ   cr   r   r   rL      s    z$_PipeLine.invoke.<locals>.<listcomp>)app_contextre   filter_contextsreverse)r'   rT   rX   filtersr^   r   r   r   rU      s    

z_PipeLine.invokeN)rD   rE   rF   r?   rU   r   r   r   r   rd      s   rd   c               @   s   e Zd ZdZdd ZdS )
_FilterAppZ
filter_appc             C   s   |j  }|j }||S )N)next_contextre   filter_context)r'   rT   Znext_appr^   r   r   r   rU      s    

z_FilterApp.invokeN)rD   rE   rF   r?   rU   r   r   r   r   rk      s   rk   c               @   s   e Zd ZdZdd ZdS )_FilterWithZfiltered_withc                s>   |j   |j |jjtkr( S  fdd}|S d S )Nc                s    | S )Nr   )rX   )r^   filteredr   r   composed   s    z$_FilterWith.invoke.<locals>.composed)rm   re   rl   object_typeAPP)r'   rT   rp   r   )r^   ro   r   rU      s    

z_FilterWith.invokeN)rD   rE   rF   r?   rU   r   r   r   r   rn      s   rn   c             K   s   t t| fd|i|S )Nr?   )loadobjrr   )urir?   r)   r   r   r   r	      s    c             K   s   t t| fd|i|S )Nr?   )rs   FILTER)rt   r?   r)   r   r   r   r      s    c             K   s   t t| fd|i|S )Nr?   )rs   SERVER)rt   r?   r)   r   r   r   r
     s    c             C   s   t t| |||d}| S )N)r?   relative_torR   )loadcontextrr   config)rt   r?   rw   rR   rT   r   r   r   r     s    c             C   s   t | ||||d}| S )N)r?   rw   rR   )rx   re   )rq   rt   r?   rw   rR   rT   r   r   r   rs     s    rs   c             C   s   d|kr2|d kr"| dd\}}n| ddd }|d kr>d}d|krRtd| | dd\}}| }|tkrtd|dt f t| | |||||d	S )
N#r1   r   main:zURI has no scheme: %rz"URI scheme not known: %r (from %s)z, )r?   rw   rR   )splitLookupErrorlower_loadersjoinkeys)rq   rt   r?   rw   rR   schemepathr   r   r   rx     s$    rx   c             C   s   t j|}|dd}|sX|s,td| |dd}|drL|| }n|d | }|drn|dd  }t|}t|}|r|j	|dd |
| ||S )N\/zECannot resolve relative uri %r; no relative_to keyword argument givenz///   F)	overwrite)osr   isabsreplace
ValueErrorendswith
startswithr   ConfigLoaderupdate_defaultsget_context)rq   rt   r   r?   rw   rR   r   r\   r   r   r   _loadconfig1  s$    


r   ry   c             C   s   t |}|| ||S )N)	EggLoaderr   )rq   rt   specr?   rw   rR   r\   r   r   r   _loadeggK  s    r   Zeggc             C   s   t |}|| ||S )N)
FuncLoaderr   )rq   rt   r   r?   rw   rR   r\   r   r   r   	_loadfuncS  s    r   callc               @   sZ   e Zd ZdddZdddZdddZddd	Zdd
dZdddZe	
dZdd ZdS )_LoaderNc             C   s   | j ||d S )N)r?   rR   )rg   re   )r'   r?   rR   r   r   r   get_appb  s    z_Loader.get_appc             C   s   | j ||d S )N)r?   rR   )rm   re   )r'   r?   rR   r   r   r   
get_filterf  s    z_Loader.get_filterc             C   s   | j ||d S )N)r?   rR   )server_contextre   )r'   r?   rR   r   r   r   
get_serverj  s    z_Loader.get_serverc             C   s   | j t||dS )N)r?   rR   )r   rr   )r'   r?   rR   r   r   r   rg   n  s    z_Loader.app_contextc             C   s   | j t||dS )N)r?   rR   )r   ru   )r'   r?   rR   r   r   r   rm   r  s    z_Loader.filter_contextc             C   s   | j t||dS )N)r?   rR   )r   rv   )r'   r?   rR   r   r   r   r   v  s    z_Loader.server_contextz^[a-zA-Z]+:c             C   s   |dkrdS | j |S )z<
        Returns true if the name includes a scheme
        NF)_absolute_research)r'   r?   r   r   r   absolute_name|  s    z_Loader.absolute_name)NN)NN)NN)NN)NN)NN)rD   rE   rF   r   r   r   rg   rm   r   recompiler   r   r   r   r   r   r   `  s   






r   c               @   sZ   e Zd Zdd ZdddZdddZd	d
 Zdd Zdd Zdd Z	dddZ
dd ZdS )r   c          	   C   sj   |   | _}tjtj|tj|d}t||d| _t| j_	t
|}| j| W d Q R X d S )N)here__file__)r,   )stripr%   r   r   dirnameabspathr"   rB   stroptionxformopenr*   )r'   r%   r,   fr   r   r   r$     s    
zConfigLoader.__init__Tc             C   s8   x2t |D ]&\}}|s$|| jjkr$q
|| jj|< q
W d S )N)r   rB   	_defaults)r'   Znew_defaultsr   r/   rC   r   r   r   r     s    zConfigLoader.update_defaultsNc             C   s.  |  |r$t||tj| j|dS | j||d}| j }|	 }|d k	rV|
| |}i }i }i }	x| j|D ]}
|
dr|
dd   }| j||
 ||< ||< qt|
dr|
dd   }| j||
|	|< qt|
|krqt| j||
||
< qtW x"|	 D ]\}}|| ||< qW |ttfkrDd|krD|d}nd }d|krzx |d  D ]}t| q`W |d= |dr| j||||||d	}nT|d
r| j||||||d	}n0d|kr| |||||}n| |||||}|d k	r*td td ||| d}| j||d|_||_|S |S )N)rw   rR   )r?   zset    zget zfilter-withrequirezfilter-app:)r?   rR   rS   global_additionsz	pipeline:use)r   rq   rP   rR   rS   r\   )r?   rR   )r   rx   r   r   r   r%   find_config_sectionrB   r,   r-   updateoptionsr   r   r.   itemsrr   ru   popr}   r   r   _filter_app_context_pipeline_app_context_context_from_use_context_from_explicitLoaderContextFILTER_WITHrm   rl   )r'   rq   r?   rR   r7   r,   Z_global_confrS   r   Zget_from_globalsr8   Z	local_varZglob_varZfilter_withr   rT   Zfilter_with_contextr   r   r   r     s|    










zConfigLoader.get_contextc       	      C   s   | d}| j|||d}|j| |j| d|krH|d |jd< | |_|jd kr|ddd }|dkrxd|_n|d	krd
|_n
d| |_|S )Nr   )r?   rR   r   r|   r1   r   )rW   rX   zpaste.app_factory)rZ   rY   zpaste.composit_factoryzpaste.%s_factory)r   r   rR   r   rS   r\   rP   r}   )	r'   rq   rS   rR   r   r7   r   rT   Zsection_protocolr   r   r   r     s     


zConfigLoader._context_from_usec             C   s   g }x8|j D ].}x(|D ] }||kr|||| f P qW qW t|dkrZtd||f |sjtd| |d \}	}
||	= t|
}t|||	||| }|S )Nr1   z*Multiple protocols given in section %r: %szNo loader given in section %rr   )rM   appendlenr~   r   r   )r'   rq   rS   rR   Zglobal_additionr7   possibleprotocol_optionsrP   Zfound_protocolZ
found_exprrC   rT   r   r   r   r     s(    



z#ConfigLoader._context_from_explicitc       	      C   s|   d|krt d|| jf |d}td td ||| }| t|||_d|krd| t	|||||_
n| t	|||||_
|S )Nnextz2The [%s] section in %s is missing a 'next' settingr   )r~   r%   r   r   
FILTER_APPr   rr   rl   r   ru   rm   r   )	r'   rq   r7   r?   rR   rS   r   Z	next_namerT   r   r   r   r     s"    


z ConfigLoader._filter_app_contextc       	         s   d|krt d|jf |d }|rJt d|jd| f td td  |}t	|d  |_
 fdd|d d D |_|S )Nr[   z6The [%s] section in %s is missing a 'pipeline' settingzCThe [%s] pipeline section in %s has extra (disallowed) settings: %sz, c                s   g | ]} t| qS r   )r   ru   )rJ   r?   )rR   r'   r   r   rL   6  s   z6ConfigLoader._pipeline_app_context.<locals>.<listcomp>)r~   r%   r   r}   r   r   r   PIPELINEr   rr   rg   rh   )	r'   rq   r7   r?   rR   rS   r   r[   rT   r   )rR   r'   r   r   %  s     
z"ConfigLoader._pipeline_app_contextc          
   C   s   g }x@|j D ]6}x0|D ](}| | j ||}|r|| P qW qW |sptd|dttt	|j | j
f t|dkrtd||dttt	|j | j
f |d S )a/  
        Return the section name with the given name prefix (following the
        same pattern as ``protocol_desc`` in ``config``.  It must have the
        given name, or for ``'main'`` an empty name is allowed.  The
        prefix must be followed by a ``:``.

        Case is *not* ignored.
        z1No section %r (prefixed by %s) found in config %sz or r1   zMAmbiguous section names %r for section %r (prefixed by %s) found in config %sr   )rN   _find_sectionsrB   sectionsr   r~   r   mapreprr   r%   r   )r'   rq   r?   r   Zname_optionsname_prefixfoundr   r   r   r   :  s*    	


z ConfigLoader.find_config_sectionc             C   sh   g }|d kr"||kr| | d}x@|D ]8}||d r(|t|d d   |kr(| | q(W |S )Nr{   r|   r1   )r   r   r   r   )r'   r   r   r?   r   r7   r   r   r   r   Z  s    

zConfigLoader._find_sections)T)NN)N)rD   rE   rF   r$   r   r   r   r   r   r   r   r   r   r   r   r   r     s   

E
 r   c               @   s(   e Zd Zdd ZdddZd	ddZdS )
r   c             C   s
   || _ d S )N)r   )r'   r   r   r   r   r$   i  s    zEggLoader.__init__Nc          
   C   sP   |  |rt|||dS | j||d\}}}t||||p:i i | t| j|dS )N)rR   )r?   )distributionentry_point_name)r   rx   find_egg_entry_pointr   r   get_distributionr   )r'   rq   r?   rR   entry_pointrP   Zep_namer   r   r   r   l  s    

zEggLoader.get_contextc                s    dkrd g }xX|j D ]N}xH|D ]@}tj tj| }|dk	r"|| ||jf P q"W qW |stj}t	d j|j
dt|j dt fdd|D pdf t|dkrt	d	 jdt|f |d
 S )z^
        Returns the (entry_point, protocol) for the with the given
        ``name``.
        Nr{   zMEntry point %r not found in egg %r (dir: %s; protocols: %s; entry_points: %s)z, c                s$   g | ]}t tj| pi qS r   )r   r   get_entry_infor   )rJ   Zprot)r?   r'   r   r   rL     s   z2EggLoader.find_egg_entry_point.<locals>.<listcomp>z(no entry points)r1   z7Ambiguous entry points for %r in egg %r (protocols: %s)r   )rM   r   r   r   r   r   r   r?   r   r~   locationr   r   r   )r'   rq   r?   r   r   rP   entrydistr   )r?   r'   r   r   {  s8    

zEggLoader.find_egg_entry_point)NN)N)rD   rE   rF   r$   r   r   r   r   r   r   r   g  s   
r   c               @   s"   e Zd ZdZdd ZdddZdS )r   a8   Loader that supports specifying functions inside modules, without
    using eggs at all. Configuration should be in the format:
        use = call:my.module.path:function_name
        
    Dot notation is supported in both the module and function name, e.g.:
        use = call:my.module.path:object.method
    c             C   s   || _ d|krtdd S )Nr|   z+Configuration not in format module:function)r   r~   )r'   r   r   r   r   r$     s    zFuncLoader.__init__Nc             C   s    t | j}t||d |pi i | S )N)r   r   r   )r'   rq   r?   rR   r   r   r   r   r     s    
zFuncLoader.get_context)NN)rD   rE   rF   __doc__r$   r   r   r   r   r   r     s   r   c               @   s&   e Zd ZdddZdd Zdd ZdS )	r   Nc	       	      C   s4   || _ || _|| _|| _|| _|| _|| _|| _d S )N)rH   rq   rP   rR   rS   r\   r   r   )	r'   r   rq   rP   rR   rS   r\   r   r   r   r   r   r$     s    zLoaderContext.__init__c             C   s   | j | S )N)rq   rU   )r'   r   r   r   re     s    zLoaderContext.createc             C   s0   t | j}|| j | j|_| j|_| |_|S )N)AttrDictrR   r   rS   rT   )r'   confr   r   r   ry     s    
zLoaderContext.config)NN)rD   rE   rF   r$   re   ry   r   r   r   r   r     s   
r   c               @   s   e Zd ZdZdS )r   z/
    A dictionary that can be assigned to.
    N)rD   rE   rF   r   r   r   r   r   r     s   r   )N)N)N)NNN)NNN)NNN)2
__future__r   r   r4   r   r   Zpaste.deploy.compatr   r   r   r   Zpaste.deploy.utilr   r   __all__r   r   r   r"   rH   rI   rV   rr   r]   ru   ra   rv   rd   r   rk   r   rn   r   r	   r   r
   r   r   rs   rx   r   r   r   r   r   r   r   r   dictr   r   r   r   r   <module>   s\   
<



 
 
% c9