o
    JAf                     @   sL  	 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|}|j| |d t	|}t
  zt  t||}t||| W t
  d S t
  w )Nr   readline)encoding)configparser
isinstanceZRawConfigParserConfigParserhasattrZ	read_fileiotext_encodingread_create_formatterslogging_acquireLock_clearExistingHandlers_install_handlers_install_loggers_releaseLock)Zfnamedefaultsdisable_existing_loggersr   r   cp
formattershandlers r   %/usr/lib/python3.10/logging/config.py
fileConfig3   s    



r   c              	   C   sj   	 |  d} | d}t|}| D ]!}|d | }zt||}W q ty2   t| t||}Y qw |S )N.r   )splitpop
__import__getattrAttributeError)nameusedfoundnr   r   r   _resolveV   s   

r&   c                 C   s   t tj| S N)mapstrstrip)Zalistr   r   r   _strip_spacesd   s   r+   c                 C   s   	 | d d }t |si S |d}t|}i }|D ];}d| }| j|ddd d}| j|ddd d}| j|d	dd
d}tj}| | d}	|	rKt|	}||||}
|
||< q|S )Nr   keys,zformatter_%sformatT)rawfallbackdatefmtstyle%class)lenr   r+   getr   	Formatterr&   )r   flistr   ZformZsectnamefsdfsZstlc
class_namefr   r   r   r   g   s&   

r   c              
   C   s`  	 | d d }t |si S |d}t|}i }g }|D ]}| d|  }|d }|dd}z	t|tt}W n ttfyF   t	|}Y nw |dd	}	t|	tt}	|d
d}
t|
tt}
||	i |
}||_
d|v rx|d }|| t |r|||  t|tjjr|dd}t |r|||f |||< q|D ]\}}|||  q|S )Nr   r,   r-   z
handler_%sr4   	formatter args()kwargsz{}leveltarget)r5   r   r+   r6   evalvarsr   r!   	NameErrorr&   r"   setLevelsetFormatter
issubclassr   MemoryHandlerappendZ	setTarget)r   r   hlistr   Zfixupshandsectionklassfmtr@   rB   hrC   rD   tr   r   r   r   }   sH   


r   c                 C   sV   	 t j}| D ]"}|jj| }||v r%t|t js$|t j g |_d|_	q||_
qd S )NT)r   rootmanager
loggerDictr   PlaceHolderrH   NOTSETr   	propagatedisabled)existingchild_loggersdisable_existingrT   logloggerr   r   r   _handle_existing_loggers   s   
r`   c                 C   s  	 | d d }| d}tt|}|d | d }tj}|}d|v r-|d }|| |jd d  D ]}|| q4|d }	t	|	rY|	 d}	t|	}	|	D ]	}
|
||
  qOt|jj }|  g }|D ]}| d|  }|d	 }|jd
dd}t|}||v r||d }|d }t	|}t	|}||k r|| d | |kr|||  |d7 }||k s|| d|v r|d }|| |jd d  D ]}|| q||_d|_|d }	t	|	r|	 d}	t|	}	|	D ]	}
|
||
  qqit||| d S )Nloggersr,   r-   rT   Zlogger_rootrC   r   z	logger_%squalnamerY      )r0   r   r   )r   listr+   remover   rT   rH   r   removeHandlerr5   
addHandlerrU   rV   r,   sortZgetint	getLoggerindexrL   rY   rZ   r`   )r   r   r]   ZllistrO   rT   r^   rC   rR   rM   rN   r[   r\   ZqnrY   r_   iprefixedpflennum_existingr   r   r   r      sj   







r   c                   C   s0   	 t j  t t jd d   t jd d = d S r'   )r   	_handlersclearshutdown_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   
rx   c                   @   s    e Zd Z	 dddZdd ZdS )ConvertingMixinTc                 C   sB   | j |}||ur|r|| |< t|tttfv r| |_||_|S r'   )configuratorconverttypeConvertingDictConvertingListConvertingTupleparentkey)selfr   valuereplaceresultr   r   r   convert_with_key$  s   
z ConvertingMixin.convert_with_keyc                 C   s0   | j |}||urt|tttfv r| |_|S r'   )rz   r{   r|   r}   r~   r   r   )r   r   r   r   r   r   r{   0  s   
zConvertingMixin.convertN)T)__name__
__module____qualname__r   r{   r   r   r   r   ry   !  s    
ry   c                   @   s*   e Zd Z	 dd ZdddZdddZdS )	r}   c                 C      t | |}| ||S r'   )dict__getitem__r   r   r   r   r   r   r   r   E     zConvertingDict.__getitem__Nc                 C   s   t | ||}| ||S r'   )r   r6   r   r   r   defaultr   r   r   r   r6   I  s   zConvertingDict.getc                 C   s   t | ||}| j||ddS NF)r   )r   r   r   r   r   r   r   r   M  s   zConvertingDict.popr'   )r   r   r   r   r6   r   r   r   r   r   r}   B  s
    
r}   c                   @   s    e Zd Z	 dd ZdddZdS )r~   c                 C   r   r'   )rd   r   r   r   r   r   r   r   S  r   zConvertingList.__getitem__c                 C   s   t | |}| |S r'   )rd   r   r{   )r   idxr   r   r   r   r   W  s   
zConvertingList.popN)r   )r   r   r   r   r   r   r   r   r   r~   Q  s    r~   c                   @      e Zd Z	 dd ZdS )r   c                 C   s   t | |}| j||ddS r   )tupler   r   r   r   r   r   r   ]  s   zConvertingTuple.__getitem__N)r   r   r   r   r   r   r   r   r   [  s    r   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)extZcfgc                 C   s   t || _| | j_d S r'   )r}   configrz   )r   r   r   r   r   __init__v  s   
zBaseConfigurator.__init__c           	   	   C   s   	 | d}|d}z-| |}|D ]"}|d| 7 }zt||}W q ty5   | | t||}Y qw |W S  tyZ   t dd  \}}td||f }|||_	|_
|w )Nr   r   rc   zCannot resolve %r: %s)r   r   importerr    r!   ImportErrorsysexc_inforu   	__cause____traceback__)	r   rv   r"   r#   r$   fragetbvr   r   r   resolvez  s(   



zBaseConfigurator.resolvec                 C   s   	 |  |S r'   )r   r   r   r   r   r   r     s   
zBaseConfigurator.ext_convertc                 C   s   	 |}| j |}|d u rtd| || d  }| j| d  }|r}| j|}|r7|| d  }n1| j|}|rh| d }| j|sP|| }nz
t	|}|| }W n t
yg   || }Y nw |rs|| d  }ntd||f |s&|S )NzUnable to convert %rr   zUnable to convert %r at %r)WORD_PATTERNrt   ru   endr   groupsDOT_PATTERNINDEX_PATTERNDIGIT_PATTERNint	TypeError)r   r   restrw   dr   r%   r   r   r   r     s:   
zBaseConfigurator.cfg_convertc                 C   s   	 t |tst |trt|}| |_|S t |ts't |tr't|}| |_|S t |ts?t |tr?t|ds?t|}| |_|S t |t	rj| j
|}|rj| }|d }| j|d }|rj|d }t| |}||}|S )N_fieldsprefixsuffix)r   r}   r   rz   r~   rd   r   r   r	   r)   CONVERT_PATTERNrt   	groupdictvalue_convertersr6   r    )r   r   rw   r   r   	converterr   r   r   r   r{     s8   


zBaseConfigurator.convertc                    sp   	   d}t|s| |}  dd } fdd D }|di |}|r6| D ]
\}}t||| q+|S )NrA   r   c                       i | ]}t |r| | qS r   rx   .0kr   r   r   
<dictcomp>      z5BaseConfigurator.configure_custom.<locals>.<dictcomp>r   )r   callabler   itemssetattr)r   r   r;   propsrB   r   r"   r   r   r   r   configure_custom  s   

z!BaseConfigurator.configure_customc                 C   s   	 t |tr
t|}|S r'   )r   rd   r   r   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   r{   r   r   r   r   r   r   r   b  s$    




"r   c                   @   s\   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|vrtd|d dkrtd|d  |dd}i }t  z |r|d|}|D ];}|tjvr?td| ztj| }|| }|d	d }|rY|t| W q2 t	ym }	 ztd
| |	d }	~	ww |d|}
|
D ] }z| 
||
| d W qv t	y }	 ztd| |	d }	~	ww |dd }|rz	| |d W n t	y }	 ztd|	d }	~	ww np|dd}t  |d|}|D ] }z| || ||< W q t	y }	 ztd| |	d }	~	ww |d|}|D ]!}z| || ||< W q t	y }	 ztd| |	d }	~	ww |d|}g }t|D ]<}z| || }||_|||< W q$ t	y` }	 zdt|	jv rN|| ntd
| |	W Y d }	~	q$d }	~	ww |D ]'}z| || }||_|||< W qc t	y }	 ztd
| |	d }	~	ww tj}t|jj }|  g }|d|}
|
D ]^}||v r||d }|d }t|}t|}||k r|| d | |kr|||  |d7 }||k s|| z| 
||
|  W q t	y }	 ztd| |	d }	~	ww t||| |dd }|r:z| | W n' t	y+ }	 ztd|	d }	~	ww W t  d S W t  d S W t  d S W t  d S t  w )Nversionz$dictionary doesn't specify a versionrc   zUnsupported version: %sincrementalFr   zNo handler found with name %rrC   zUnable to configure handler %rra   TzUnable to configure logger %rrT   zUnable to configure root loggerr   r   z Unable to configure formatter %rfilterszUnable to configure filter %rtarget not configured yetr   ) r   ru   r   r   r   r6   ro   rH   _checkLevel	Exceptionconfigure_loggerconfigure_rootr   configure_formatterconfigure_filtersortedconfigure_handlerr"   r)   r   rL   rT   rd   rU   rV   r,   rh   rj   r5   re   r`   r   )r   r   r   
EMPTY_DICTr   r"   handlerZhandler_configrC   r   ra   rT   r]   r   r   Zdeferredr[   r\   rk   rl   rm   rn   r   r   r   	configure  sD  





xvzDictConfigurator.configurec           
   
   C   s   	 d|v r<|d }z|  |}W |S  ty; } zdt|vr  |d|d< ||d< |  |}W Y d }~|S d }~ww |dd }|dd }|dd}|dd }|sZtj}	nt|}	d	|v rm|	||||d	 }|S |	|||}|S )
NrA   z'format'r.   rQ   r1   r2   r3   r4   validate)r   r   r)   r   r6   r   r7   r&   )
r   r   factoryr   terQ   Zdfmtr2   cnamer;   r   r   r   r     s6   z$DictConfigurator.configure_formatterc                 C   s2   	 d|v r|  |}|S |dd}t|}|S )NrA   r"   r?   )r   r6   r   Filter)r   r   r   r"   r   r   r   r     s   

z!DictConfigurator.configure_filterc                 C   N   	 |D ]!}z| | jd |  W q ty$ } ztd| |d }~ww d S )Nr   zUnable to add filter %r)	addFilterr   r   ru   )r   Zfiltererr   r=   r   r   r   r   add_filters     zDictConfigurator.add_filtersc              
      sd  	 t  } dd }|r*z	| jd | }W n ty) } ztd| |d }~ww  dd } dd }d v rK d}t|sH| |}|}nt d}	| |	}
t|
tj	j
rd v rz| jd	  d  }t|tjsy | td
| d< W n> ty } z	td d  |d }~ww t|
tj	jrd v r|  d  d< nt|
tj	jrd v r|  d  d< |
} dd } fdd D }z	|di |}W n' ty } zdt|vr |d|d< |di |}W Y d }~nd }~ww |r|| |d ur|t| |r| || |r0| D ]\}}t||| q$|S )Nr>   r   zUnable to set formatter %rrC   r   rA   r4   rD   r   r   zUnable to set target handler %rZmailhostaddressr   c                    r   r   r   r   r   r   r   r     r   z6DictConfigurator.configure_handler.<locals>.<dictcomp>z'stream'streamZstrmr   )r   r   r   r   ru   r   r   rJ   r   r   rK   r   Handlerupdater   ZSMTPHandlerr   ZSysLogHandlerr)   rI   rH   r   r   r   r   )r   r   Zconfig_copyr>   r   rC   r   r;   r   r   rP   thr   rB   r   r   r"   r   r   r   r   r     s   




	

z"DictConfigurator.configure_handlerc                 C   r   )Nr   zUnable to add handler %r)rg   r   r   ru   )r   r_   r   rR   r   r   r   r   add_handlers  r   zDictConfigurator.add_handlersFc                 C   s   	 | dd }|d ur|t| |sB|jd d  D ]}|| q| dd }|r2| || | dd }|rD| || d S d S d S )NrC   r   r   )r6   rH   r   r   r   rf   r   r   )r   r_   r   r   rC   rR   r   r   r   r   r   common_logger_config  s   z%DictConfigurator.common_logger_configc                 C   sB   	 t |}| ||| d|_|dd }|d ur||_d S d S )NFrY   )r   ri   r   rZ   r6   rY   )r   r"   r   r   r_   rY   r   r   r   r     s   

z!DictConfigurator.configure_loggerc                 C   s   	 t  }| ||| d S r'   )r   ri   r   )r   r   r   rT   r   r   r   r   "  s   zDictConfigurator.configure_rootN)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 r'   )dictConfigClassr   r   r   r   r   
dictConfig)  s   r   c                    sF   	 G dd dt }G dd dt}G  fdddtj  ||| |S )Nc                   @   r   )z#listen.<locals>.ConfigStreamHandlerc                 S   sP  	 z| j }|d}t|dkrtd|d }| j |}t||k r5|||t|  }t||k s$| jjd urA| j|}|d ur||d}zdd l}|	|}t
| W n" ty{   t|}zt| W n tyx   t  Y nw Y nw | jjr| jj  W d S W d S W d S  ty } z|jtkr W Y d }~d S d }~ww )N   z>Lr   zutf-8)
connectionrecvr5   structunpackserververifydecodejsonloadsr   r   r
   StringIOr   	traceback	print_excreadysetOSErrorerrnoRESET_ERROR)r   connchunkZslenr   r   filer   r   r   r   handleI  sJ   




z*listen.<locals>.ConfigStreamHandler.handleN)r   r   r   r  r   r   r   r   ConfigStreamHandlerB  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>.ConfigSocketReceiverrc   	localhostNc                 S   s>   t | ||f| t  d| _t  d| _|| _|| _d S )Nr   rc   )	r   r   r   r   abortr   timeoutr   r   )r   hostportr   r   r   r   r   r   r   w  s   
z-listen.<locals>.ConfigSocketReceiver.__init__c                 S   sb   dd l }d}|s+| | j gg g | j\}}}|r|   t  | j}t  |r| 	  d S )Nr   )
selectsocketfilenor  Zhandle_requestr   r   r  r   Zserver_close)r   r  r  Zrdwrexr   r   r   serve_until_stopped  s   
	z8listen.<locals>.ConfigSocketReceiver.serve_until_stopped)r   r   r   Zallow_reuse_addressDEFAULT_LOGGING_CONFIG_PORTr   r  r   r   r   r   ConfigSocketReceiverp  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 r'   )	superr   rcvrhdlrr  r   	threadingEventr   )r   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r|jd | _| j  t  |a	t
  |  d S )N)r  r   r   r   r   rc   )r  r  r  r   r   Zserver_addressr   r   r   	_listenerr   r  )r   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  Thread)r  r   r  r  r   r  r   listen.  s
   .r  c                   C   s@   	 t   ztrdt_d aW t   d S W t   d S t   w )Nrc   )r   r   r  r  r   r   r   r   r   stopListening  s   r  )NTN)*r   r
   r   Zlogging.handlersr   r   r   r  r   socketserverr   r   r  Z
ECONNRESETr   r  r   r&   r+   r   r   r`   r   r   r   Irs   rx   objectry   r   r}   rd   r~   r   r   r   r   r   r   r  r  r   r   r   r   <module>   sJ   

#&W!
   Bz