B
    `1                 @   s   d dl Z d dlZd dl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mZ d dlmZ d dlmZ d	Zd
ZdZejdfddZdd Zdd Zdd Zdd Zdd ZG dd dZedkre e pd  dS )    N)	Interface)not_)IRouteRequest)	bootstrap)get_config_loader
parse_vars)static_view)_find_views   *z	<unknown>Fc             C   s   t | |}| S )N)PRoutesCommandrun)argvquietcommand r   _/home/kop/projects/devel/pgwui/test_venv/lib/python3.7/site-packages/pyramid/scripts/proutes.pymain   s    
r   c             C   s   | j }|dsd| }|S )N/z/%s)pattern
startswith)router   r   r   r   _get_pattern   s    
r   c             C   s\   d}||||d}g }x:t | D ].\}}	||	 t }
|d|	|f 7 }||
 q W |j| S )N )namer   viewmethodz{{%s: <{%s}}} )	enumeratePADappendformat)fmtmax_namemax_patternmax_view
max_methodZ	print_fmtZmax_mapsizesindexcolsizer   r   r   _get_print_format!   s    r*   c       	      C   s"  t  }| rt | } |rVt |}x6| D ]*}|dr(|| ||dd   q(W | d k	}t|dk}|pp|}|dkr|dkrtg}n6|dkr|dkr|}n |dkr|dkr| }n
| |}t ||}|r|sd}nB|r|r|thkrx|D ]}|d|  qW d	t
|}|S )	N!   r   FTz<route mismatch>z!%s,)setcopyr   removeaddlenANY_KEYintersection
differencejoinsorted)	route_request_methodsview_request_methodsexcludesr   Zhas_route_methodsZhas_view_methodsZhas_methodsrequest_methodsexcluder   r   r   _get_request_methods3   s:    


r=   c             C   s   | d krt S t| dr`t| dr(| j}nd }t|trX|jd k	rPd|j|jf S |jS qh| j}nt| }d| j	|f }|dkrd}|S )N__name____original_view__z%s:%sz%s.%szpyramid.router.decoratorz	<wsgiapp>)
UNKNOWN_KEYhasattrr?   
isinstancer   package_nameZdocrootr>   str
__module__)view_callableZoriginal_viewZ	view_nameview_moduler   r   r   _get_view_module^   s$    




rH   c             C   s
  t | }|jt| jd}d }g }i }d }|jd| j}|d krT| jt | ttfgS t||t	d}	|	rp|	d }nd }t
|}
|d krg ||
< ||
 n2|dddkr| j|d ttfgS |d	 }|j|}|rx|D ]}|d	}|d k	r|d
d k	r2t|d |d
 }dt
|d |d
 f }
n|d }t
|}
|
|kr^g ||
< ||
 t|trr|f}nt|trd|j f}||
 | q|
|krg ||
< ||
 qW ng ||
< ||
 g }x4|D ],}
||
 }t||}|| j||
|f qW |S )N)r   routesr   r   ZstaticFTZexternal_urlr;   attrcallablez%s.%sz!%s)r   ZqueryUtilityr   r   Zintrospectorgetr@   r3   r	   r   rH   r   relatedgetattrrB   rD   r   valueextendr=   )r   registryr   Zrequest_ifacer8   Zview_request_methods_orderr9   rF   Z
route_intrZview_callablesrG   Z	view_intrr   request_methodZfinal_routesmethodsr;   r   r   r   get_route_data   sh    









rT   c               @   s   e Zd ZdZeeZeeZejZe	j
eee	jdZejddddddd	 ejd
dddddd	 ejddddd ejddddd d$ddZdd Zd%ddZdd Zd d! Zd&d"d#ZdS )'r   a      Print all URL dispatch routes used by a Pyramid application in the
    order in which they are evaluated.  Each route includes the name of the
    route, the pattern of the route, and the view callable which will be
    invoked when the route is matched.

    This command accepts one positional argument named 'config_uri'.  It
    specifies the PasteDeploy config file to use for the interactive
    shell. The format is 'inifile#name'. If the name is left off, 'main'
    will be assumed.  Example: 'proutes myapp.ini'.

    )descriptionformatter_classz-gz--globstoreglobr   z$Display routes matching glob pattern)actiondestdefaulthelpz-fz--formatr    z_Choose which columns to display, this will override the format key in the [proutes] ini section
config_uri?Nz"The URI to the configuration file.)nargsr[   r\   config_varsr   r   zVariables required by the config file. For example, `http_port=%%(http_port)s` would expect `http_port=8080` to be passed here.Fc             C   s6   | j |dd  | _|| _ddddg| _| j| _d S )Nr,   r   r   r   r   )parser
parse_argsargsr   available_formatscolumn_format)selfr   r   r   r   r   __init__  s    zPRoutesCommand.__init__c             C   sN   g }x |D ]}|| j kr
|| q
W d}|rJ||| j f }| | dS dS )Nz9You provided invalid formats %s. Available formats are %sFT)rd   r   out)rf   formatsZinvalid_formatsr!   msgr   r   r   validate_formats  s    


zPRoutesCommand.validate_formatsc             C   s:   | d|}|d}|r6td|}dd |D | _d S )NZproutesr    z	[,|\s\n]+c             S   s   g | ]}|  qS r   )strip).0xr   r   r   
<listcomp>,  s    z6PRoutesCommand.proutes_file_config.<locals>.<listcomp>)Zget_settingsrL   resplitre   )rf   loaderZglobal_confsettingsr    colsr   r   r   proutes_file_config'  s
    
z"PRoutesCommand.proutes_file_configc             C   s   | j st| d S )N)r   print)rf   rj   r   r   r   rh   .  s    zPRoutesCommand.outc             C   s   ddl m} ||d}| S )Nr   )Configurator)rQ   )pyramid.configrw   Zget_routes_mapper)rf   rQ   rw   configr   r   r   _get_mapper2  s    
zPRoutesCommand._get_mapperc          	   C   s  | j js| d dS | j j}t| j j}| |}|| | || | j||d}|d }| 	|}| j j
r| j j
d}dd |D | _| | j}	|	dkrdS |d krd	S td
}
td}td}td}|jdd}t|d	krd	S d
dddddddddg}x|D ]}t||}x|D ]\}}}}| j jrht|| j jp\t|| j j}|shq&t||
kr~t|}
t||krt|}t||krt|}t||krt|}|||||d q&W qW t| j|
|||}x |D ]}| |j
f | qW d	S )Nzrequires a config file argument   )optionsrQ   r-   c             S   s   g | ]}|  qS r   )rl   )rm   rn   r   r   r   ro   I  s    z&PRoutesCommand.run.<locals>.<listcomp>Fr   NamePatternZViewMethodT)Zinclude_static)r   r   r   r   z----z-------z------)rc   r]   rh   r   r`   r   setup_loggingru   r   rz   r    rq   re   rk   r2   Z
get_routesrT   rX   fnmatchr   r*   )rf   r   r]   r`   rr   envrQ   ZmappercolumnsZis_validr"   r#   r$   r%   rI   Zmapped_routesr   Z
route_datar   r   r   r   matchr!   r   r   r   r   8  sz    








zPRoutesCommand.run)F)N)F)r>   rE   __qualname__rU   staticmethodr   r   sysstdoutargparseArgumentParsertextwrapdedentRawDescriptionHelpFormatterra   add_argumentrg   rk   ru   rh   rz   r   r   r   r   r   r      sJ   


r   __main__)!r   r   rp   r   r   Zzope.interfacer   rx   r   Zpyramid.interfacesr   Zpyramid.pasterr   Zpyramid.scripts.commonr   r   Zpyramid.staticr   Zpyramid.viewr	   r   r3   r@   r   r   r   r*   r=   rH   rT   r   r>   exitr   r   r   r   <module>   s0   +%S A