3 \Ќ� @stdZddlZddlZddlZddlZddlZddlZddlZddlZyddl Z ddl Z Wne k rpdZ YnXddl mZmZdZejZdad+dd�Zdd �Zd d �Zd d �Zdd�Zdd�Zdd�Zdd�Zejdej�Zdd�ZGdd�de �Z!Gdd�de"e!�Z#Gdd�de$e!�Z%Gdd �d e&e!�Z'Gd!d"�d"e �Z(Gd#d$�d$e(�Z)e)Z*d%d&�Z+edfd'd(�Z,d)d*�Z-dS),a Configuration functions for the logging package for Python. The core package is based on PEP 282 and comments thereto in comp.lang.python, and influenced by Apache's log4j system. Copyright (C) 2001-2014 Vinay Sajip. All Rights Reserved. To use, simply 'import logging' and log away! �N)�ThreadingTCPServer�StreamRequestHandleriF#Tc Cs�ddl}t||j�r|}n*|j|�}t|d�r:|j|�n |j|�t|�}tj �z t �t ||�}t |||�Wdtj �XdS)aD Read the logging configuration from a ConfigParser-format file. This can be called several times from an application, allowing an end user the ability to select from various pre-canned configurations (if the developer provides a mechanism to present the choices and load the chosen configuration). rN�readline)� configparser� isinstanceZRawConfigParserZ ConfigParser�hasattrZ read_file�read�_create_formatters�logging� _acquireLock�_clearExistingHandlers�_install_handlers�_install_loggers� _releaseLock)ZfnameZdefaults�disable_existing_loggersr�cp� formatters�handlers�r�&/usr/lib64/python3.6/logging/config.py� fileConfig8s       rc Csp|jd�}|jd�}t|�}xN|D]F}|d|}yt||�}Wq"tk rft|�t||�}Yq"Xq"W|S)z)Resolve a dotted name to a global object.�.r)�split�pop� __import__�getattr�AttributeError)�name�used�found�nrrr�_resolveZs    r!cCstdd�|�S)NcSs|j�S)N)�strip)�xrrr�isz_strip_spaces..)�map)Zalistrrr� _strip_spaceshsr&c Cs�|dd}t|�siS|jd�}t|�}i}x~|D]v}d|}|j|dddd�}|j|d ddd�}|j|d dd d�}tj}||jd �} | r�t| �}||||�} | ||<q4W|S) zCreate and return formattersr�keys�,z formatter_%s�formatTN)�raw�fallback�datefmt�style�%�class)�lenrr&�getr � Formatterr!) rZflistrZformZsectnameZfsZdfsZstl�c� class_name�frrrr ks$     r c CsD|dd}t|�siS|jd�}t|�}i}g}x�|D]�}|d|}|d}|jdd�}yt|tt��}Wn ttfk r�t |�}YnX|d} t| tt��} || �} d |kr�|d } | j | �t|�r�| j ||�t |tj j��r|jd d�} t| ��r|j| | f�| ||<q8Wx |D]\} } | j|| ��q$W|S) zInstall and return handlersrr'r(z handler_%sr/� formatter��args�level�target)r0rr&r1�eval�varsr r� NameErrorr!�setLevel� setFormatter� issubclassr� MemoryHandler�appendZ setTarget)rr�hlistrZfixups�hand�section�klass�fmtr8�hr9r:�trrrr �s>         r cCsHtj}x<|D]4}|jj|}||kr:tj|_g|_d|_q ||_q WdS)a� When (re)configuring logging, handle loggers which were in the previous configuration but are not in the new configuration. There's no point deleting them as other threads may continue to hold references to them; and by disabling them, you stop them doing any logging. However, don't disable children of named loggers, as that's probably not what was intended by the user. Also, allow existing loggers to NOT be disabled if disable_existing is false. TN) r �root�manager� loggerDictZNOTSETr9r� propagate�disabled)�existing� child_loggers�disable_existingrJ�log�loggerrrr�_handle_existing_loggers�s   rTcCs,|dd}|jd�}ttdd�|��}|jd�|d}tj}|}d|kr^|d}|j|�x |jd d �D]}|j|�qnW|d } t | �r�| jd�} t | �} x| D]} |j || �q�Wt|j j j��} | j�g} �x>|D�]4}|d |}|d } |jd dd�}tj| �}| | k�r�| j| �d}| d}t |�}t | �}x<||k�r�| |d |�|k�rt| j| |�|d7}�qFW| j| �d|k�r�|d}|j|�x"|jd d �D]}|j|��q�W||_d|_|d } t | �r�| jd�} t | �} x| D]} |j || ��qWq�Wt| | |�d S)zCreate and install loggers�loggersr'r(cSs|j�S)N)r")r#rrrr$�sz"_install_loggers..rJZ logger_rootr9Nrz logger_%s�qualnamerM�)r+rr)r�listr%�remover rJr>r� removeHandlerr0r&� addHandlerrKrLr'�sortZgetint� getLogger�indexrBrMrNrT)rrrQZllistrErJrRr9rHrCrDrOrPZqnrMrS�i�prefixed�pflen� num_existingrrrr�sd                rcCs.tjj�tjtjdd��tjdd�=dS)z!Clear and close existing handlersN)r � _handlers�clearZshutdownZ _handlerListrrrrr s r z^[a-z_][a-z0-9_]*$cCstj|�}|std|��dS)Nz!Not a valid Python identifier: %rT)� IDENTIFIER�match� ValueError)�s�mrrr� valid_idents  rjc@s"eZdZdZddd�Zdd�ZdS) �ConvertingMixinz?For ConvertingXXX's, this mixin class provides common functionsTcCsB|jj|�}||k r>|r |||<t|�tttfkr>||_||_|S)N)� configurator�convert�type�ConvertingDict�ConvertingList�ConvertingTuple�parent�key)�selfrs�value�replace�resultrrr�convert_with_key$s  z ConvertingMixin.convert_with_keycCs0|jj|�}||k r,t|�tttfkr,||_|S)N)rlrmrnrorprqrr)rtrurwrrrrm0s   zConvertingMixin.convertN)T)�__name__� __module__� __qualname__�__doc__rxrmrrrrrk!s rkc@s,eZdZdZdd�Zd dd�Zd dd�ZdS) roz A converting dictionary wrapper.cCstj||�}|j||�S)N)�dict� __getitem__rx)rtrsrurrrr~Es zConvertingDict.__getitem__NcCstj|||�}|j||�S)N)r}r1rx)rtrs�defaultrurrrr1IszConvertingDict.getcCstj|||�}|j||dd�S)NF)rv)r}rrx)rtrsrrurrrrMszConvertingDict.pop)N)N)ryrzr{r|r~r1rrrrrroBs roc@s"eZdZdZdd�Zd dd�ZdS) rpzA converting list wrapper.cCstj||�}|j||�S)N)rXr~rx)rtrsrurrrr~Ss zConvertingList.__getitem__rWcCstj||�}|j|�S)N)rXrrm)rt�idxrurrrrWs zConvertingList.popN�����)r�)ryrzr{r|r~rrrrrrpQsrpc@seZdZdZdd�ZdS)rqzA converting tuple wrapper.cCstj||�}|j||dd�S)NF)rv)�tupler~rx)rtrsrurrrr~]s zConvertingTuple.__getitem__N)ryrzr{r|r~rrrrrq[srqc@s�eZdZdZejd�Zejd�Zejd�Zejd�Z ejd�Z ddd �Z e e �Zd d �Zd d �Zdd�Zdd�Zdd�Zdd�Zdd�ZdS)�BaseConfiguratorzI The configurator base class which defines some useful defaults. z%^(?P[a-z]+)://(?P.*)$z ^\s*(\w+)\s*z^\.\s*(\w+)\s*z^\[\s*(\w+)\s*\]\s*z^\d+$� ext_convert� cfg_convert)ZextZcfgcCst|�|_||j_dS)N)ro�configrl)rtr�rrr�__init__vs zBaseConfigurator.__init__c Cs�|jd�}|jd�}y`|j|�}xP|D]H}|d|7}yt||�}Wq&tk rl|j|�t||�}Yq&Xq&W|Stk r�tj�dd�\}}td||f�}|||_ |_ |�YnXdS)z` Resolve strings to objects using standard import and attribute syntax. rrrWNzCannot resolve %r: %s) rr�importerrr� ImportError�sys�exc_inforg� __cause__� __traceback__) rtrhrrrZfrag�e�tb�vrrr�resolvezs"      zBaseConfigurator.resolvecCs |j|�S)z*Default converter for the ext:// protocol.)r�)rtrurrrr��szBaseConfigurator.ext_convertc Cs|}|jj|�}|dkr&td|��n�||j�d�}|j|j�d}x�|r�|jj|�}|rp||j�d}nd|jj|�}|r�|j�d}|jj|�s�||}n2yt |�}||}Wnt k r�||}YnX|r�||j�d�}qJtd||f��qJW|S)z*Default converter for the cfg:// protocol.NzUnable to convert %rrzUnable to convert %r at %r) � WORD_PATTERNrfrg�endr��groups� DOT_PATTERN� INDEX_PATTERN� DIGIT_PATTERN�int� TypeError)rtru�restri�dr�r rrrr��s2       zBaseConfigurator.cfg_convertcCs�t|t� r&t|t�r&t|�}||_n�t|t� rLt|t�rLt|�}||_n|t|t� rrt|t�rrt|�}||_nVt|t�r�|j j |�}|r�|j �}|d}|j j |d�}|r�|d}t||�}||�}|S)z� Convert values to an appropriate type. dicts, lists and tuples are replaced by their converting alternatives. Strings are checked to see if they have a conversion format and are converted if they do. �prefixN�suffix)rror}rlrprXrqr��str�CONVERT_PATTERNrf� groupdict�value_convertersr1r)rtrurir�r�Z converterr�rrrrm�s*     zBaseConfigurator.convertcsr�jd�}t|�s|j|�}�jdd�}t�fdd��D��}|f|�}|rnx |j�D]\}}t|||�qVW|S)z1Configure an object with a user-supplied factory.z()rNcs g|]}t|�r|�|f�qSr)rj)�.0�k)r�rr� �sz5BaseConfigurator.configure_custom..)r�callabler�r}�items�setattr)rtr�r3�props�kwargsrwrrur)r�r�configure_custom�s    z!BaseConfigurator.configure_customcCst|t�rt|�}|S)z0Utility function which converts lists to tuples.)rrXr�)rtrurrr�as_tuple�s zBaseConfigurator.as_tupleN)ryrzr{r|�re�compiler�r�r�r�r�r�� staticmethodrr�r�r�r�r�rmr�r�rrrrr�bs      "r�c@s^eZdZdZdd�Zdd�Zdd�Zdd �Zd d �Zd d �Z ddd�Z ddd�Z ddd�Z dS)�DictConfiguratorz] Configure logging using a dictionary-like object to describe the configuration. cCs�|j}d|krtd��|ddkr2td|d��|jdd�}i}tj��z�|�r�|jd|�}x�|D]�}|tjkr�td|��qfy6tj|}||}|jd d �}|r�|jtj|��Wqft k r�} ztd || f��WYd d } ~ XqfXqfW|jd |�} xZ| D]R}y|j || |d �Wn4t k �rP} ztd|| f��WYd d } ~ XnX�qW|jdd �} | �r�y|j | d �Wn0t k �r�} ztd| ��WYd d } ~ XnX�n:|jdd �} t �|jd|�} xZ| D]R}y|j | |�| |<Wn4t k �r"} ztd|| f��WYd d } ~ XnX�q�W|jd|�}xZ|D]R}y|j||�||<Wn4t k �r�} ztd|| f��WYd d } ~ XnX�q� _checkLevel� Exception�configure_logger�configure_rootr �configure_formatter�configure_filter�sorted�configure_handlerrr�rBrJrXrKrLr'r\r^r0rYrTr)rtr�r�Z EMPTY_DICTrr�handlerZhandler_configr9r�rUrJrQrr�ZdeferredrOrPr_r`rarbrrr� configure�s�        "  $    $  $   $  $      $ zDictConfigurator.configurec Cs�d|krr|d}y|j|�}Wq�tk rn}z4dt|�kr>�|jd�|d<||d<|j|�}WYdd}~Xq�XnP|jdd�}|jdd�}|jdd�}|jd d�}|s�tj} nt|�} | |||�}|S) z(Configure a formatter from a dictionary.z()z'format'r)rGNr,r-r.r/)r�r�r�rr1r r2r!) rtr��factoryrw�terGZdfmtr-�cnamer3rrrr��s&      z$DictConfigurator.configure_formattercCs.d|kr|j|�}n|jdd�}tj|�}|S)z%Configure a filter from a dictionary.z()rr7)r�r1r ZFilter)rtr�rwrrrrr��s    z!DictConfigurator.configure_filtercCs^xX|D]P}y|j|jd|�Wqtk rT}ztd||f��WYdd}~XqXqWdS)z/Add filters to a filterer from a list of names.r�zUnable to add filter %r: %sN)Z addFilterr�r�rg)rtZfiltererr�r5r�rrr� add_filters�s  zDictConfigurator.add_filtersc/s�t��}�jdd�}|r^y|jd|}Wn2tk r\}ztd||f��WYdd}~XnX�jdd�}�jdd�}d�kr��jd�}t|�s�|j|�}|}�n�jd�} |j| �} t| tj j �o�d �k�rHy>|jd �d } t | tj ��s�j |�td ��| �d <Wn8tk �rD}ztd �d |f��WYdd}~XnXnZt| tj j��rvd �k�rv|j�d ��d <n,t| tj j��r�d�k�r�|j�d��d<| }�jdd�} t�fdd��D��} y|f| �}WnLtk �r"}z.dt|�k�r��| jd�| d<|f| �}WYdd}~XnX|�r4|j|�|dk �rN|jtj|��|�r`|j||�| �r�x"| j�D]\}}t|||��qpW|S)z&Configure a handler from a dictionary.r6NrzUnable to set formatter %r: %sr9r�z()r/r:rztarget not configured yetz#Unable to set target handler %r: %sZmailhostZaddressrcs g|]}t|�r|�|f�qSr)rj)r�r�)r�rrr��sz6DictConfigurator.configure_handler..z'stream'�streamZstrm)r}rr�r�rgr�r�r@r rrArZHandler�updater�Z SMTPHandlerr�Z SysLogHandlerr�r?r>r�r�r�r�)rtr�Z config_copyr6r�r9r�r3r�r�rFZthr�r�rwr�rrur)r�rr��sl          $      z"DictConfigurator.configure_handlercCs^xX|D]P}y|j|jd|�Wqtk rT}ztd||f��WYdd}~XqXqWdS)z.Add handlers to a logger from a list of names.rzUnable to add handler %r: %sN)r[r�r�rg)rtrSrrHr�rrr� add_handlers�s  zDictConfigurator.add_handlersFcCs�|jdd�}|dk r$|jtj|��|s�x |jdd�D]}|j|�q8W|jdd�}|rf|j||�|jdd�}|r�|j||�dS)zU Perform configuration which is common to root and non-root loggers. r9Nrr�)r1r>r r�rrZr�r�)rtrSr�r�r9rHrr�rrr�common_logger_config�s    z%DictConfigurator.common_logger_configcCs6tj|�}|j|||�|jdd�}|dk r2||_dS)z.Configure a non-root logger from a dictionary.rMN)r r]r�r1rM)rtrr�r�rSrMrrrr�s   z!DictConfigurator.configure_loggercCstj�}|j|||�dS)z*Configure a root logger from a dictionary.N)r r]r�)rtr�r�rJrrrr�szDictConfigurator.configure_rootN)F)F)F) ryrzr{r|r�r�r�r�r�r�r�r�r�rrrrr��s ?  r�cCst|�j�dS)z%Configure logging using a dictionary.N)�dictConfigClassr�)r�rrr� dictConfig sr�csPts td��Gdd�dt�}Gdd�dt�}G�fdd�dtj���||||�S)au Start up a socket server on the specified port, and listen for new configurations. These will be sent as a file suitable for processing by fileConfig(). Returns a Thread object on which you can call start() to start the server, and which you can join() when appropriate. To stop the server, call stopListening(). Use the ``verify`` argument to verify any bytes received across the wire from a client. If specified, it should be a callable which receives a single argument - the bytes of configuration data received across the network - and it should return either ``None``, to indicate that the passed in bytes could not be verified and should be discarded, or a byte string which is then passed to the configuration machinery as normal. Note that you can return transformed bytes, e.g. by decrypting the bytes passed in. z listen() needs threading to workc@seZdZdZdd�ZdS)z#listen..ConfigStreamHandlerz� Handler for a logging configuration request. It expects a completely new logging configuration and uses fileConfig to install it. cSs\�y"|j}|jd�}t|�dk�r"tjd|�d}|jj|�}x&t|�|krd||j|t|��}q@W|jjdk r~|jj|�}|dk �r |jd�}y,ddl}|j |�}t |t �s�t �t |�WnLtk �r tj|�}y t|�Wntk �rtj�YnXYnX|jj�r"|jjj�Wn2tk �rV}z|jtk�rF�WYdd}~XnXdS)z� Handle a request. Each request is expected to be a 4-byte length, packed using struct.pack(">L", n), followed by the config file. Uses fileConfig() to do the grunt work. �z>LrNzutf-8)Z connectionZrecvr0�structZunpack�server�verify�decode�json�loadsrr}�AssertionErrorr�r��io�StringIOr� traceback� print_exc�ready�set�OSError�errno� RESET_ERROR)rtZconn�chunkZslenr�r��filer�rrr�handleBs8            z*listen..ConfigStreamHandler.handleN)ryrzr{r|r�rrrr�ConfigStreamHandler;sr�c@s0eZdZdZdZdedddfdd�Zdd�ZdS) z$listen..ConfigSocketReceiverzD A simple TCP socket-based logging config receiver. rWZ localhostNcSs>tj|||f|�tj�d|_tj�d|_||_||_dS)NrrW) rr�r r �abortr�timeoutr�r�)rt�host�portr�r�r�rrrr�psz-listen..ConfigSocketReceiver.__init__cSsfddl}d}xJ|sV|j|jj�ggg|j�\}}}|r>|j�tj�|j}tj�qW|jj �dS)Nr) �selectZsocket�filenor�Zhandle_requestr r r�r�close)rtr�r�Zrd�wrZexrrr�serve_until_stoppedzs z8listen..ConfigSocketReceiver.serve_until_stopped)ryrzr{r|Zallow_reuse_address�DEFAULT_LOGGING_CONFIG_PORTr�r�rrrr�ConfigSocketReceiveris  r�cs&eZdZ��fdd�Zdd�Z�ZS)zlisten..Servercs4t�|�j�||_||_||_||_tj�|_dS)N) �superr��rcvr�hdlrr�r�� threadingZEventr�)rtr�r�r�r�)�Server� __class__rrr��s zlisten..Server.__init__cSsZ|j|j|j|j|jd�}|jdkr0|jd|_|jj�tj�|a tj �|j �dS)N)r�r�r�r�rrW) r�r�r�r�r�Zserver_addressr�r r � _listenerrr�)rtr�rrr�run�s     zlisten..Server.run)ryrzr{r�r�� __classcell__r)r�)r�rr��sr�)�thread�NotImplementedErrorrrr�ZThread)r�r�r�r�r)r�r�listen%s .r�c Cs*tj�ztrdt_daWdtj�XdS)zN Stop the listening server which was created with a call to listen(). rWN)r r r�r�rrrrr� stopListening�s r�)NT).r|r�r�r Zlogging.handlersr�r�r�r��_threadr�r�r�Z socketserverrrr�Z ECONNRESETr�r�rr!r&r r rTrr r��Irerj�objectrkr}rorXrpr�rqr�r�r�r�r�r�rrrr�sP   "#W! 9|