B
    v9aAŒ  ã               @   sJ  d dl Z d dlZd dlZd dlZ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
mZ dZe jZdad*dd„Zdd„ Zd	d
„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Ze dej¡Zdd„ ZG dd„ deƒZG dd„ deeƒZG dd„ de eƒZ!G dd„ de"eƒZ#G d d!„ d!eƒZ$G d"d#„ d#e$ƒZ%e%Z&d$d%„ Z'edfd&d'„Z(d(d)„ Z)dS )+é    N)ÚThreadingTCPServerÚStreamRequestHandleriF#  Tc             C   s„   dd l }t| |jƒr| }n*| |¡}t| dƒr:| | ¡ n
| | ¡ t|ƒ}t 	¡  z t
ƒ  t||ƒ}t|||ƒ W d t ¡  X d S )Nr   Úreadline)ÚconfigparserÚ
isinstanceZRawConfigParserZConfigParserÚhasattrZ	read_fileÚreadÚ_create_formattersÚloggingÚ_acquireLockÚ_clearExistingHandlersÚ_install_handlersÚ_install_loggersÚ_releaseLock)ZfnameÚdefaultsÚdisable_existing_loggersr   ÚcpÚ
formattersÚhandlers© r   ú/usr/lib/python3.7/config.pyÚ
fileConfig3   s    	



r   c          	   C   sp   |   d¡} |  d¡}t|ƒ}xN| D ]F}|d | }yt||ƒ}W q" tk
rf   t|ƒ t||ƒ}Y q"X q"W |S )NÚ.r   )ÚsplitÚpopÚ
__import__ÚgetattrÚAttributeError)ÚnameÚusedÚfoundÚnr   r   r   Ú_resolveU   s    


r"   c             C   s   t tj| ƒS )N)ÚmapÚstrÚstrip)Zalistr   r   r   Ú_strip_spacesc   s    r&   c             C   s²   | d d }t |ƒsi S | d¡}t|ƒ}i }x~|D ]v}d| }| j|ddd d}| j|ddd d}| j|d	dd
d}tj}| |  d¡}	|	r–t|	ƒ}||||ƒ}
|
||< q4W |S )Nr   Úkeysú,zformatter_%sÚformatT)ÚrawÚfallbackÚdatefmtÚstyleú%Úclass)Úlenr   r&   Úgetr
   Ú	Formatterr"   )r   Úflistr   ZformZsectnameZfsZdfsZstlÚcÚ
class_nameÚfr   r   r   r	   f   s$    

r	   c          
   C   sj  | d d }t |ƒsi S | d¡}t|ƒ}i }g }x|D ]}| d|  }|d }| dd¡}yt|ttƒƒ}W n  ttfk
r’   t	|ƒ}Y nX | dd	¡}	t|	ttƒƒ}	| d
d¡}
t|
ttƒƒ}
||	|
Ž}d|krì|d }| 
|¡ t |ƒr| || ¡ t|tjjƒr8| dd¡}t |ƒr8| ||f¡ |||< q:W x |D ]\}}| || ¡ qJW |S )Nr   r'   r(   z
handler_%sr/   Ú	formatterÚ Úargsz()Úkwargsz{}ÚlevelÚtarget)r0   r   r&   r1   ÚevalÚvarsr
   r   Ú	NameErrorr"   ÚsetLevelÚsetFormatterÚ
issubclassr   ÚMemoryHandlerÚappendZ	setTarget)r   r   Úhlistr   ZfixupsÚhandÚsectionÚklassÚfmtr9   r:   Úhr;   r<   Útr   r   r   r   |   sB    




r   c             C   sH   t j}x<| D ]4}|jj| }||kr:t j|_g |_d|_q||_qW d S )NT)	r
   ÚrootÚmanagerÚ
loggerDictZNOTSETr;   r   Ú	propagateÚdisabled)ÚexistingÚchild_loggersÚdisable_existingrL   ÚlogÚloggerr   r   r   Ú_handle_existing_loggers¡   s    
rV   c             C   s&  | d d }|  d¡}tt|ƒƒ}| d¡ | d }tj}|}d|krX|d }| |¡ x |jd d … D ]}| |¡ qhW |d }	t	|	ƒr¸|	  d¡}	t|	ƒ}	x|	D ]}
| 
||
 ¡ q¢W t|jj ¡ ƒ}| ¡  g }x>|D ]4}| d|  }|d	 }|jd
dd}t |¡}||kr†| |¡d }|d }t	|ƒ}t	|ƒ}x<||k rz|| d |… |krn| || ¡ |d7 }q@W | |¡ d|kr¢|d }| |¡ x"|jd d … D ]}| |¡ q²W ||_d|_|d }	t	|	ƒrÜ|	  d¡}	t|	ƒ}	x|	D ]}
| 
||
 ¡ qúW qÜW t|||ƒ d S )NÚloggersr'   r(   rL   Zlogger_rootr;   r   z	logger_%sÚqualnamerO   é   )r+   r   r   )r   Úlistr&   Úremover
   rL   r@   r   ÚremoveHandlerr0   Ú
addHandlerrM   rN   r'   ÚsortZgetintÚ	getLoggerÚindexrD   rO   rP   rV   )r   r   rS   ZllistrG   rL   rT   r;   rJ   rE   rF   rQ   rR   ZqnrO   rU   ÚiÚprefixedÚpflenÚnum_existingr   r   r   r   ¶   sd    











r   c               C   s.   t j ¡  t  t jd d … ¡ t jd d …= d S )N)r
   Ú	_handlersÚclearZshutdownZ_handlerListr   r   r   r   r     s    
r   z^[a-z_][a-z0-9_]*$c             C   s   t  | ¡}|std|  ƒ‚dS )Nz!Not a valid Python identifier: %rT)Ú
IDENTIFIERÚmatchÚ
ValueError)ÚsÚmr   r   r   Úvalid_ident  s    
rl   c               @   s   e Zd Zddd„Zdd„ ZdS )ÚConvertingMixinTc             C   sB   | j  |¡}||k	r>|r || |< t|ƒtttfkr>| |_||_|S )N)ÚconfiguratorÚconvertÚtypeÚConvertingDictÚConvertingListÚConvertingTupleÚparentÚkey)Úselfru   ÚvalueÚreplaceÚresultr   r   r   Úconvert_with_key!  s    
z ConvertingMixin.convert_with_keyc             C   s0   | j  |¡}||k	r,t|ƒtttfkr,| |_|S )N)rn   ro   rp   rq   rr   rs   rt   )rv   rw   ry   r   r   r   ro   -  s    
zConvertingMixin.convertN)T)Ú__name__Ú
__module__Ú__qualname__rz   ro   r   r   r   r   rm     s   
rm   c               @   s(   e Zd Zdd„ Zddd„Zd	dd„ZdS )
rq   c             C   s   t  | |¡}|  ||¡S )N)ÚdictÚ__getitem__rz   )rv   ru   rw   r   r   r   r   B  s    zConvertingDict.__getitem__Nc             C   s   t  | ||¡}|  ||¡S )N)r~   r1   rz   )rv   ru   Údefaultrw   r   r   r   r1   F  s    zConvertingDict.getc             C   s   t  | ||¡}| j||ddS )NF)rx   )r~   r   rz   )rv   ru   r€   rw   r   r   r   r   J  s    zConvertingDict.pop)N)N)r{   r|   r}   r   r1   r   r   r   r   r   rq   ?  s   
rq   c               @   s   e Zd Zdd„ Zddd„ZdS )rr   c             C   s   t  | |¡}|  ||¡S )N)rZ   r   rz   )rv   ru   rw   r   r   r   r   P  s    zConvertingList.__getitem__éÿÿÿÿc             C   s   t  | |¡}|  |¡S )N)rZ   r   ro   )rv   Úidxrw   r   r   r   r   T  s    zConvertingList.popN)r   )r{   r|   r}   r   r   r   r   r   r   rr   N  s   rr   c               @   s   e Zd Zdd„ ZdS )rs   c             C   s   t  | |¡}| j||ddS )NF)rx   )Útupler   rz   )rv   ru   rw   r   r   r   r   Z  s    zConvertingTuple.__getitem__N)r{   r|   r}   r   r   r   r   r   rs   X  s   rs   c               @   sˆ   e Zd Ze d¡Ze d¡Ze d¡Ze d¡Ze d¡Z	dddœ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 )ÚBaseConfiguratorz%^(?P<prefix>[a-z]+)://(?P<suffix>.*)$z^\s*(\w+)\s*z^\.\s*(\w+)\s*z^\[\s*(\w+)\s*\]\s*z^\d+$Úext_convertÚcfg_convert)ZextZcfgc             C   s   t |ƒ| _| | j_d S )N)rq   Úconfigrn   )rv   r‡   r   r   r   Ú__init__s  s    
zBaseConfigurator.__init__c       	   	   C   sÄ   |  d¡}| d¡}y`|  |¡}xP|D ]H}|d| 7 }yt||ƒ}W q& tk
rl   |  |¡ t||ƒ}Y q&X q&W |S  tk
r¾   t ¡ dd … \}}td||f ƒ}|| |_	|_
|‚Y nX d S )Nr   r   rY   zCannot resolve %r: %s)r   r   Úimporterr   r   ÚImportErrorÚsysÚexc_infori   Ú	__cause__Ú__traceback__)	rv   rj   r   r   r    ZfragÚeÚtbÚvr   r   r   Úresolvew  s"    




zBaseConfigurator.resolvec             C   s
   |   |¡S )N)r’   )rv   rw   r   r   r   r…   Ž  s    zBaseConfigurator.ext_convertc             C   s  |}| j  |¡}|d kr&td| ƒ‚nØ|| ¡ d … }| j| ¡ d  }x´|rü| j |¡}|rp|| ¡ d  }nd| j |¡}|rÔ| ¡ d }| j |¡s¢|| }n2yt	|ƒ}|| }W n t
k
rÒ   || }Y nX |rê|| ¡ d … }qJtd||f ƒ‚qJW |S )NzUnable to convert %rr   zUnable to convert %r at %r)ÚWORD_PATTERNrh   ri   Úendr‡   ÚgroupsÚDOT_PATTERNÚINDEX_PATTERNÚDIGIT_PATTERNÚintÚ	TypeError)rv   rw   Úrestrk   Údr‚   r!   r   r   r   r†   ’  s2    
zBaseConfigurator.cfg_convertc             C   sÆ   t |tƒs$t |tƒr$t|ƒ}| |_nžt |tƒsHt |tƒrHt|ƒ}| |_nzt |tƒslt |tƒrlt|ƒ}| |_nVt |tƒrÂ| j	 
|¡}|rÂ| ¡ }|d }| j |d ¡}|rÂ|d }t| |ƒ}||ƒ}|S )NÚprefixÚsuffix)r   rq   r~   rn   rr   rZ   rs   rƒ   r$   ÚCONVERT_PATTERNrh   Ú	groupdictÚvalue_convertersr1   r   )rv   rw   rk   rœ   r   Z	converterrž   r   r   r   ro   ´  s*    



zBaseConfigurator.convertc                sn   ˆ   d¡}t|ƒs|  |¡}ˆ   dd ¡}‡ fdd„ˆ D ƒ}|f |Ž}|rjx | ¡ D ]\}}t|||ƒ qRW |S )Nz()r   c                s   i | ]}t |ƒrˆ | |“qS r   )rl   )Ú.0Úk)r‡   r   r   ú
<dictcomp>×  s    z5BaseConfigurator.configure_custom.<locals>.<dictcomp>)r   Úcallabler’   ÚitemsÚsetattr)rv   r‡   r4   Úpropsr:   ry   r   rw   r   )r‡   r   Úconfigure_customÐ  s    


z!BaseConfigurator.configure_customc             C   s   t |tƒrt|ƒ}|S )N)r   rZ   rƒ   )rv   rw   r   r   r   Úas_tupleÞ  s    
zBaseConfigurator.as_tupleN)r{   r|   r}   ÚreÚcompilerŸ   r“   r–   r—   r˜   r¡   Ústaticmethodr   r‰   rˆ   r’   r…   r†   ro   r©   rª   r   r   r   r   r„   _  s   




"r„   c               @   sZ   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zddd„Z	ddd„Z
ddd„ZdS )ÚDictConfiguratorc             C   sì  | j }d|krtdƒ‚|d dkr2td|d  ƒ‚| dd¡}i }t ¡  z|rª| d|¡}xŒ|D ]„}|tjkr‚td| ƒ‚qfy6tj| }|| }| d	d ¡}|r¶| t |¡¡ W qf t	k
rè }	 ztd
| ƒ|	‚W d d }	~	X Y qfX qfW | d|¡}
xX|
D ]P}y|  
||
| d¡ W n2 t	k
rL }	 ztd| ƒ|	‚W d d }	~	X Y nX q W | dd ¡}|rÚy|  |d¡ W n. t	k
r¤ }	 ztdƒ|	‚W d d }	~	X Y nX n0| dd¡}tƒ  | d|¡}xX|D ]P}y|  || ¡||< W n2 t	k
r }	 ztd| ƒ|	‚W d d }	~	X Y nX qÎW | d|¡}xX|D ]P}y|  || ¡||< W n2 t	k
r€ }	 ztd| ƒ|	‚W d d }	~	X Y nX q4W | d|¡}g }x‚t|ƒD ]v}y |  || ¡}||_|||< W nN t	k
r }	 z.dt|	jƒkrö| |¡ ntd
| ƒ|	‚W d d }	~	X Y nX q¢W xb|D ]Z}y |  || ¡}||_|||< W n2 t	k
rx }	 ztd
| ƒ|	‚W d d }	~	X Y nX q"W tj}t|jj ¡ ƒ}| ¡  g }| d|¡}
xÎ|
D ]Æ}||kr0| |¡d }|d }t|ƒ}t|ƒ}x<||k r$|| d |… |kr| || ¡ |d7 }qêW | |¡ y|  
||
| ¡ W n2 t	k
rv }	 ztd| ƒ|	‚W d d }	~	X Y nX q´W t|||ƒ | dd ¡}|rÚy|  |¡ W n. t	k
rØ }	 ztdƒ|	‚W d d }	~	X Y nX W d t ¡  X d S )NÚversionz$dictionary doesn't specify a versionrY   zUnsupported version: %sÚincrementalFr   zNo handler found with name %rr;   zUnable to configure handler %rrW   TzUnable to configure logger %rrL   zUnable to configure root loggerr   r   z Unable to configure formatter %rÚfilterszUnable to configure filter %rztarget not configured yetr   ) r‡   ri   r   r
   r   r1   re   r@   Ú_checkLevelÚ	ExceptionÚconfigure_loggerÚconfigure_rootr   Úconfigure_formatterÚconfigure_filterÚsortedÚconfigure_handlerr   r$   r   rD   rL   rZ   rM   rN   r'   r^   r`   r0   r[   rV   r   )rv   r‡   r°   Z
EMPTY_DICTr   r   ÚhandlerZhandler_configr;   r   rW   rL   rS   r   r±   ZdeferredrQ   rR   ra   rb   rc   rd   r   r   r   Ú	configureê  sÔ    



 
"
"
""
"


"zDictConfigurator.configurec       
   
   C   sÆ   d|krr|d }y|   |¡}W qÂ tk
rn } z2dt|ƒkr>‚ | d¡|d< ||d< |   |¡}W d d }~X Y qÂX nP| dd ¡}| dd ¡}| dd¡}| dd ¡}|s®tj}	nt|ƒ}	|	|||ƒ}|S )	Nz()z'format'r)   rI   r,   r-   r.   r/   )r©   rš   r$   r   r1   r
   r2   r"   )
rv   r‡   Úfactoryry   ÚterI   Zdfmtr-   Úcnamer4   r   r   r   r¶   ˆ  s&    z$DictConfigurator.configure_formatterc             C   s.   d|kr|   |¡}n| dd¡}t |¡}|S )Nz()r   r8   )r©   r1   r
   ZFilter)rv   r‡   ry   r   r   r   r   r·   ¤  s
    
z!DictConfigurator.configure_filterc             C   s\   xV|D ]N}y|  | jd | ¡ W q tk
rR } ztd| ƒ|‚W d d }~X Y qX qW d S )Nr±   zUnable to add filter %r)Z	addFilterr‡   r³   ri   )rv   Zfiltererr±   r6   r   r   r   r   Úadd_filters­  s
    
zDictConfigurator.add_filtersc          
      sˆ  t ˆ ƒ}ˆ  dd ¡}|r\y| jd | }W n0 tk
rZ } ztd| ƒ|‚W d d }~X Y nX ˆ  dd ¡}ˆ  dd ¡}dˆ kr ˆ  d¡}t|ƒs˜|  |¡}|}nˆ  d¡}	|  |	¡}
t|
tj	j
ƒrFdˆ krFy>| jd	 ˆ d  }t|tjƒsˆ  |¡ td
ƒ‚|ˆ d< W n6 tk
rB } ztdˆ d  ƒ|‚W d d }~X Y nX nZt|
tj	jƒrtdˆ krt|  ˆ d ¡ˆ d< n,t|
tj	jƒr dˆ kr |  ˆ d ¡ˆ d< |
}ˆ  dd ¡}‡ fdd„ˆ D ƒ}y|f |Ž}W nL tk
r } z,dt|ƒkrô‚ | d¡|d< |f |Ž}W d d }~X Y nX |r.| |¡ |d k	rH| t |¡¡ |rZ|  ||¡ |r„x"| ¡ D ]\}}t|||ƒ qjW |S )Nr7   r   zUnable to set formatter %rr;   r±   z()r/   r<   r   ztarget not configured yetzUnable to set target handler %rZmailhostZaddressr   c                s   i | ]}t |ƒrˆ | |“qS r   )rl   )r¢   r£   )r‡   r   r   r¤   Ý  s    z6DictConfigurator.configure_handler.<locals>.<dictcomp>z'stream'ÚstreamZstrm)r~   r   r‡   r³   ri   r¥   r’   rB   r
   r   rC   r   ZHandlerÚupdaterš   ZSMTPHandlerrª   ZSysLogHandlerr$   rA   r@   r²   r¿   r¦   r§   )rv   r‡   Zconfig_copyr7   r   r;   r±   r4   r¼   r¾   rH   Úthr¨   r:   ry   r½   r   rw   r   )r‡   r   r¹   µ  sl    





"



z"DictConfigurator.configure_handlerc             C   s\   xV|D ]N}y|  | jd | ¡ W q tk
rR } ztd| ƒ|‚W d d }~X Y qX qW d S )Nr   zUnable to add handler %r)r]   r‡   r³   ri   )rv   rU   r   rJ   r   r   r   r   Úadd_handlersô  s
    
zDictConfigurator.add_handlersFc             C   s†   |  dd ¡}|d k	r$| t |¡¡ |s‚x |jd d … D ]}| |¡ q8W |  dd ¡}|rf|  ||¡ |  dd ¡}|r‚|  ||¡ d S )Nr;   r   r±   )r1   r@   r
   r²   r   r\   rÃ   r¿   )rv   rU   r‡   r°   r;   rJ   r   r±   r   r   r   Úcommon_logger_configü  s    z%DictConfigurator.common_logger_configc             C   s6   t  |¡}|  |||¡ | dd ¡}|d k	r2||_d S )NrO   )r
   r_   rÄ   r1   rO   )rv   r   r‡   r°   rU   rO   r   r   r   r´     s
    
z!DictConfigurator.configure_loggerc             C   s   t  ¡ }|  |||¡ d S )N)r
   r_   rÄ   )rv   r‡   r°   rL   r   r   r   rµ     s    zDictConfigurator.configure_rootN)F)F)F)r{   r|   r}   r»   r¶   r·   r¿   r¹   rÃ   rÄ   r´   rµ   r   r   r   r   r®   ä  s    	?

r®   c             C   s   t | ƒ ¡  d S )N)ÚdictConfigClassr»   )r‡   r   r   r   Ú
dictConfig  s    rÆ   c                sD   G dd„ dt ƒ}G dd„ dtƒ}G ‡ fdd„dtjƒ‰ ˆ ||| |ƒS )Nc               @   s   e Zd Zdd„ ZdS )z#listen.<locals>.ConfigStreamHandlerc             S   sH  y| j }| d¡}t|ƒdkrt d|¡d }| j  |¡}x&t|ƒ|k rd|| |t|ƒ ¡ }q@W | jjd k	r~| j |¡}|d k	rø| d¡}ydd l}| 	|¡}t
|ƒ W nH tk
rö   t |¡}yt|ƒ W n tk
rð   t ¡  Y nX Y nX | jjr| jj ¡  W n2 tk
rB } z|jtkr2‚ W d d }~X Y nX d S )Né   z>Lr   zutf-8)Z
connectionZrecvr0   ÚstructÚunpackÚserverÚverifyÚdecodeÚjsonÚloadsrÆ   r³   ÚioÚStringIOr   Ú	tracebackÚ	print_excÚreadyÚsetÚOSErrorÚerrnoÚRESET_ERROR)rv   ZconnÚchunkZslenrÍ   rœ   Úfiler   r   r   r   Úhandle=  s6    




z*listen.<locals>.ConfigStreamHandler.handleN)r{   r|   r}   rÚ   r   r   r   r   ÚConfigStreamHandler6  s   rÛ   c               @   s,   e Zd ZdZdedddfdd„Zdd„ ZdS )z$listen.<locals>.ConfigSocketReceiverrY   Z	localhostNc             S   s>   t  | ||f|¡ t ¡  d| _t ¡  d| _|| _|| _d S )Nr   rY   )	r   rˆ   r
   r   Úabortr   ÚtimeoutrÓ   rË   )rv   ZhostÚportrº   rÓ   rË   r   r   r   rˆ   k  s    z-listen.<locals>.ConfigSocketReceiver.__init__c             S   sd   dd l }d}xJ|sV|  | j ¡ gg g | j¡\}}}|r>|  ¡  t ¡  | j}t ¡  qW |  	¡  d S )Nr   )
ÚselectZsocketÚfilenorÝ   Zhandle_requestr
   r   rÜ   r   Zserver_close)rv   rß   rÜ   ZrdZwrZexr   r   r   Úserve_until_stoppedu  s    z8listen.<locals>.ConfigSocketReceiver.serve_until_stopped)r{   r|   r}   Zallow_reuse_addressÚDEFAULT_LOGGING_CONFIG_PORTrˆ   rá   r   r   r   r   ÚConfigSocketReceiverd  s   	rã   c                   s&   e Zd Z‡‡ fdd„Zdd„ Z‡  ZS )zlisten.<locals>.Serverc                s4   t ˆ | ƒ ¡  || _|| _|| _|| _t ¡ | _d S )N)	Úsuperrˆ   ÚrcvrÚhdlrrÞ   rË   Ú	threadingZEventrÓ   )rv   rå   ræ   rÞ   rË   )ÚServerÚ	__class__r   r   rˆ   …  s    zlisten.<locals>.Server.__init__c             S   sZ   | j | j| j| j| jd}| jdkr0|jd | _| j ¡  t ¡  |a	t 
¡  | ¡  d S )N)rÞ   rº   rÓ   rË   r   rY   )rå   rÞ   ræ   rÓ   rË   Zserver_addressrÔ   r
   r   Ú	_listenerr   rá   )rv   rÊ   r   r   r   Úrun  s    


zlisten.<locals>.Server.run)r{   r|   r}   rˆ   rë   Ú__classcell__r   )rè   )ré   r   rè   ƒ  s   rè   )r   r   rç   ZThread)rÞ   rË   rÛ   rã   r   )rè   r   Úlisten"  s    .rí   c               C   s*   t  ¡  ztrdt_d aW d t  ¡  X d S )NrY   )r
   r   rê   rÜ   r   r   r   r   r   ÚstopListeningœ  s    rî   )NT)*rÖ   rÏ   r
   Zlogging.handlersr«   rÈ   r‹   rç   rÑ   Zsocketserverr   r   râ   Z
ECONNRESETr×   rê   r   r"   r&   r	   r   rV   r   r   r¬   ÚIrg   rl   Úobjectrm   r~   rq   rZ   rr   rƒ   rs   r„   r®   rÅ   rÆ   rí   rî   r   r   r   r   Ú<module>   sF   
"%W!
   9z