B
    ê¹`  ã               @   sr   d Z ddlZddlZddlmZ ddlmZ e 	e
¡Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )z0Expose useful response "variables" to templates
é    Né   )Úfind_pgwui_components)Ú
exceptionsc          
   C   s>   y
|   |¡S  tk
r8 } zt ||¡‚W dd}~X Y nX dS )z1Return the route path of the page's "source"
    N)Ú
route_pathÚKeyErrorÚexZBadRouteError)ÚrequestÚ	page_nameÚsourceÚold_ex© r   úY/home/kop/projects/devel/pgwui/test_venv/lib/python3.7/site-packages/pgwui_common/urls.pyr   #   s    
r   c          
   C   s>   y
|   |¡S  tk
r8 } zt ||¡‚W dd}~X Y nX dS )z3Return the static path to the asset's "source"
    N)Zstatic_pathÚ
ValueErrorr   ZBadAssetError)r   r	   r
   r   r   r   r   Ú
asset_path,   s    
r   c             C   sp   | j jd | }|d }|d }|dkr,|S |dkrD|  d|› ¡S |dkrXt| ||ƒS |dkrlt| ||ƒS d	S )
zReturn a url to the page.  This may or may not be fully
    qualified, depending on what the user specifies in the settings.
    ÚpgwuiÚtyper
   ÚURLÚfilezpgwui_common.ZrouteZassetN)ÚregistryÚsettingsr   r   )r   r	   Z	page_confr   r
   r   r   r   Úurl_of_page5   s    r   c             C   s„   yt | dƒ}W n\ tk
rF   y|  d¡}W n tk
r@   g S X Y n& tjk
rj } z|gS d}~X Y nX ||d kr€||d< g S )z8Add urls for pgwui_menu, return non-menu components
    Z	menu_pageÚ
pgwui_menuNÚ
pgwui_home)r   r   r   r   ÚBadPathError)r   ÚurlsZmenu_urlÚexpr   r   r   Úset_menu_urlF   s    
r   c          	   C   sd   t | |ƒ}tƒ }d|kr"| d¡ x<|D ]4}y|  |¡}W n tk
rN   Y q(X | ||¡ q(W |S )z9Add urls for each pgwui component to the 'urls' dict
    r   )r   r   Úremover   r   Ú
setdefault)r   r   ÚerrorsÚ
componentsÚ	componentÚurlr   r   r   Úset_component_urlsW   s    


r#   c          
   C   sn   g }yt | dƒ}W n: tjk
rL } z| |¡ | dd¡ W dd}~X Y nX | d|¡ | t| |ƒ¡ |S )z(Build 'urls' dict with all the urls
    Z	home_pager   N)r   r   r   Úappendr   Úextendr#   )r   r   r   Zhome_urlr   r   r   r   Úset_urlsj   s    
r&   c             C   sF   t ƒ }tjj d¡}| j|_t||ƒ}||d d< t d|› ¡ |S )z\Add the 'urls' dict to settings, from the existing settings and
    route configuration
    ú/r   r   zIRouting map of route names to url paths which is given to the templates: )	ÚdictÚpyramidr   ÚRequestZblankr   r&   ÚlogÚdebug)Úconfigr   r   r   r   r   r   r   Úadd_urls_setting{   s    
r.   )Ú__doc__ÚloggingZpyramid.requestr)   Zpluginr   Ú r   r   Ú	getLoggerÚ__name__r+   r   r   r   r   r#   r&   r.   r   r   r   r   Ú<module>   s   
		