B
    ì¹`+"  ã               @   sB  d Z ddlm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 dd	l
mZ ddlZ
ed
ddddddgƒZeddddgƒZdZdZdde› de› di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)d*„ Z!d+d,„ Z"d-d.„ Z#d/d0„ Z$d1d2„ Z%d3d4„ Z&d5d6„ Z'd7d8„ Z(dS )9zULoad the PGWUI components, parse the PGWUI configuration, and start the
WSGI server.
é    )ÚConfiguratorNé   )Ú
exceptions)Úassets)Úcheck_settings)Úroutes)ÚpluginZpg_hostZpg_portZ
default_dbÚdry_runÚroute_prefixZvalidate_hmacÚautoconfigurer   Z	home_pageZ	menu_pageÚoverride_assetsÚURLú/zpgwui.home_pageztype = z

source = Ú
c             C   s   | | | d |< | |= d S )NÚpgwui© )ÚsettingsÚkeyÚnew_keyr   r   úa/home/kop/projects/devel/pgwui/test_venv/lib/python3.7/site-packages/pgwui_server/pgwui_server.pyÚdot_to_dictJ   s    r   c          	   C   s¬   x¦|   ¡ D ]š}d|kr>| dd¡\}}| | ¡ | ¡ f¡ q
| ¡ }|dkr
y|d \}}W n tk
r|   t |¡‚Y nX t|t	ƒsŽ|g}| |¡ ||f|d< q
W dS )z&Add the parse value to the result
    ú=r   Ú éÿÿÿÿN)
Ú
splitlinesÚsplitÚappendÚrstripÚlstripÚ
IndexErrorÚ	server_exÚMissingEqualErrorÚ
isinstanceÚlist)ÚlinesÚresultÚliner   ÚvalÚstrippedr   r   r   Úparse_multiline_assignmentsO   s    

r)   c             C   sB   g }t | tƒrt| |ƒ n$x"|  ¡ D ]\}}| ||f¡ q$W |S )zBReturn a list of key/value tuples from the lines of a setting
    )r"   Ústrr)   Úitemsr   )r$   r%   r   r'   r   r   r   Úparse_assignmentsd   s    
r,   c             C   sX   | d   |tƒ ¡}z8y| tt| | ƒƒ¡ W n tjk
rF   ‚ Y nX W d| |= X dS )zSPut a multi-line setting into its own dict,
    adding to what's already there
    r   N)Ú
setdefaultÚdictÚupdater,   r    r!   )r   r   Z	pgwui_keyZmulti_settingr   r   r   Údot_to_multiline_settingp   s    r0   c          
   C   st   yt |||ƒ W n< tjk
rL } z|  t d|› |¡¡ dS d}~X Y nX ||krp|  || |d | ƒ¡ dS )zAPut a component's settings in its own dict and validate them
    zpgwui:Nr   )r0   r    r!   r   ÚBadValueErrorÚextend)ÚerrorsÚcomponent_checkersr   r   Ú	componentÚexr   r   r   Úcomponent_setting_into_dict}   s    r7   c          
   C   s¸   |dd… dkr´|dd… }||kr6t | ||||ƒ n~|tkrLt|||ƒ nh|tkr¤yt|||ƒ W q´ tjk
r  } z|  t d|› |¡¡ W dd}~X Y q´X n|  t	 
|¡¡ dS )zXSeparate a pgwui setting into a dict on '.' chars; validate
    component settings.
    Né   zpgwui.zpgwui:)r7   ÚSETTINGSr   ÚMULTI_SETTINGSr0   r    r!   r   r1   Ú	common_exZUnknownSettingKeyError)r3   Ú
componentsr4   r   r   r   r6   r   r   r   Úsetting_into_dictŒ   s    (r=   c             C   s@   t  ¡ }| dtƒ ¡ x$t| ¡ ƒD ]}t| ||||ƒ q$W dS )zUConvert "." in the pgwui settings to dict mappings, and validate
    the result.
    r   N)r   Zfind_pgwui_check_settingsr-   r.   r#   Úkeysr=   )r3   r   r<   r4   r   r   r   r   Údictify_settings£   s
    r?   c             C   sz   dd„ | D ƒ}|  tjt ¡ f¡ x|D ]\}}t ||¡ q(W x*|d |d fD ]\}}t|tjd qRW t 	d¡ dS )zReport errors and exit
    c             S   s   g | ]}t j|f‘qS r   )ÚloggingÚERROR)Ú.0Úerrorr   r   r   ú
<listcomp>±   s    z)exit_reporting_errors.<locals>.<listcomp>r   r   )Úfiler   N)
r   r@   ÚCRITICALr    ZBadSettingsAbortÚlogÚprintÚsysÚstderrÚexit)r3   ZtaggedÚlevelrC   r   r   r   Úexit_reporting_errors®   s    rM   c             C   s&   x t  ¡ D ]\}}|  ||¡ q
W dS )z8Add the default settings to the config if not there
    N)ÚDEFAULT_SETTINGSr+   r-   )r   Zsettingr'   r   r   r   Úadd_default_settings½   s    rO   c             C   s4   t | ƒ g }t|| |ƒ t || ¡ |r0t|ƒ dS )z&Exit when settings don't validate
    N)rO   r?   r   Zvalidate_settingsrM   )r   r<   r3   r   r   r   Úexit_on_invalid_settingsÄ   s    rP   c             C   s2   | d   dd¡}|sg S d| kr.t t ¡ ¡ |S )z(Automatic pgwui component discovery
    r   r   Tzpyramid.include)ÚgetrG   Úinfor    ZAutoconfigureConflict)r   r<   Z
autoconfigr   r   r   Úautoconfigurable_componentsÏ   s    rS   c             C   sT   |   dg ¡}t|tƒr2d|kr(| ¡ }n
| d¡}d|k}t d|› d|› ¡ |S )zBBoolean: Whether or not the pyramid testing pack is installed
    zpyramid.includesr   ú Zpyramid_debugtoolbarzpyramid_debugtoolbar included: z  So introspection set to: )rQ   r"   r*   r   r   rG   Údebug)r   Zpyramid_includesr%   r   r   r   Úin_developmentÜ   s    


rV   c          
   C   sª   t | ƒ}t| |dŒ}| d¡ t| |ƒ}| d  d¡}| |¡: x&|D ]}t d |¡¡ | |¡ qJW t	 
|| ¡ W dQ R X t d¡ t || ¡}||fS Q R X dS )z:Apply component default settings to existing settings
    )r   ÚintrospectionÚpgwui_commonr   r
   z#Autoconfiguring PGWUI component: {}Nz%Done autoconfiguring PGWUI components)rV   r   ÚincluderS   rQ   Zroute_prefix_contextrG   rU   Úformatr   Z
add_routesr   r   )r   r<   rW   ÚconfigZcomponents_to_configÚrpr5   r3   r   r   r   Úapply_component_defaultsì   s     



r]   c          
   C   s€   t  ¡ }t| |ƒ yt| |ƒ\}}W n6 tjjk
rZ } ztt 	|¡gƒ W dd}~X Y nX | 
tj || ¡¡ |r|t|ƒ |S )zConfigure pyramid
    N)r   Zfind_pgwui_componentsrP   r]   Úpyramidr   ÚConfigurationErrorrM   r;   ZBadSettingErrorr2   rX   ÚurlsZadd_urls_setting)r   r<   r[   r3   Úexpr   r   r   Úpgwui_server_config  s    
"rb   c             K   s   t |ƒ}| ¡ S )z&Return a Pyramid WSGI application
    )rb   Zmake_wsgi_app)Zglobal_configr   r[   r   r   r   Úmain  s    rc   ))Ú__doc__Zpyramid.configr   Zpyramid.exceptionsr^   r@   rI   r   r   r    rX   r   r   r;   r   r   Zpgwui_common.urlsÚsetr9   r:   ZDEFAULT_HOME_PAGE_TYPEZDEFAULT_HOME_PAGE_SOURCErN   Ú	getLoggerÚ__name__rG   r   r)   r,   r0   r7   r=   r?   rM   rO   rP   rS   rV   r]   rb   rc   r   r   r   r   Ú<module>   sV   
