B °-_AŒã@sNdZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl m Z m Z dZ ejZdad+dd„Zdd „Zd d „Zd d „Zdd„Zdd„Zdd„Zdd„Ze dej¡Zdd„ZGdd„deƒZGdd„deeƒ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(e dfd'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-2016 Vinay Sajip. All Rights Reserved. To use, simply 'import logging' and log away! éN)ÚThreadingTCPServerÚStreamRequestHandleriF#TcCs„ddl}t||jƒr|}n*| |¡}t|dƒr:| |¡n | |¡t|ƒ}t  ¡z t ƒt ||ƒ}t |||ƒWdt  ¡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)ZfnameÚdefaultsÚdisable_existing_loggersrÚcpÚ formattersÚhandlers©rú$/usr/lib/python3.7/logging/config.pyÚ fileConfig3s       rc Csp| d¡}| d¡}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Ú_resolveUs    r"cCs ttj|ƒS)N)ÚmapÚstrÚstrip)ZalistrrrÚ _strip_spacescsr&c Cs²|dd}t|ƒsiS| d¡}t|ƒ}i}x~|D]v}d|}|j|dddd}|j|d ddd}|j|d dd d}tj}|| d ¡} | 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 fs$     r c Csj|dd}t|ƒsiS| d¡}t|ƒ}i}g}x|D]}|d|}|d}| dd¡}yt|ttƒƒ}Wn ttfk r’t |ƒ}YnX| dd ¡} t| ttƒƒ} | d d ¡} t| ttƒƒ} || | Ž} d |krì|d } |   | ¡t|ƒr|   ||¡t |tj jƒr8| d d¡} t| ƒr8| | | f¡| ||<q:Wx |D]\} }|  ||¡qJW|S)zInstall and return handlersrr'r(z handler_%sr/Ú formatterÚÚargsz()Úkwargsz{}ÚlevelÚtarget)r0rr&r1ÚevalÚvarsr rÚ NameErrorr"ÚsetLevelÚ setFormatterÚ issubclassrÚ MemoryHandlerÚappendZ setTarget)rrÚhlistrZfixupsÚhandÚsectionÚklassÚfmtr8r9Úhr:r;Útrrrr |sB            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Ú loggerDictZNOTSETr:rÚ propagateÚdisabled)ÚexistingÚ child_loggersÚdisable_existingrKÚlogÚloggerrrrÚ_handle_existing_loggers¡s   rUcCs&|dd}| d¡}tt|ƒƒ}| d¡|d}tj}|}d|krX|d}| |¡x |jdd…D]}| |¡qhW|d} t | ƒr¸|  d¡} t| ƒ} x| D]} |  || ¡q¢Wt|j j   ¡ƒ} |  ¡g} x>|D]4}|d |}|d } |jd d d }t | ¡}| | kr†|  | ¡d }| d}t |ƒ}t | ƒ}x<||krz| |d|…|krn|  | |¡|d 7}q@W|  | ¡d|kr¢|d}| |¡x"|jdd…D]}| |¡q²W||_d|_|d} t | ƒrÜ|  d¡} t| ƒ} x| D]} |  || ¡qúWqÜWt| | |ƒdS)zCreate and install loggersÚloggersr'r(rKZ logger_rootr:Nrz logger_%sÚqualnamerNé)r+rr)rÚlistr&Úremover rKr?rÚ removeHandlerr0Ú addHandlerrLrMr'ÚsortZgetintÚ getLoggerÚindexrCrNrOrU)rrrRZllistrFrKrSr:rIrDrErPrQZqnrNrTÚiÚprefixedÚpflenÚ num_existingrrrr¶sd                 rcCs.tj ¡t tjdd…¡tjdd…=dS)z!Clear and close existing handlersN)r Ú _handlersÚclearZshutdownZ _handlerListrrrrr s r z^[a-z_][a-z0-9_]*$cCst |¡}|std|ƒ‚dS)Nz!Not a valid Python identifier: %rT)Ú IDENTIFIERÚmatchÚ ValueError)ÚsÚmrrrÚ valid_idents  rkc@s"eZdZdZddd„Zdd„ZdS) ÚConvertingMixinz?For ConvertingXXX's, this mixin class provides common functionsTcCsB|j |¡}||k r>|r |||<t|ƒtttfkr>||_||_|S)N)Ú configuratorÚconvertÚtypeÚConvertingDictÚConvertingListÚConvertingTupleÚparentÚkey)ÚselfrtÚvalueÚreplaceÚresultrrrÚconvert_with_key!s  z ConvertingMixin.convert_with_keycCs0|j |¡}||k r,t|ƒtttfkr,||_|S)N)rmrnrorprqrrrs)rurvrxrrrrn-s   zConvertingMixin.convertN)T)Ú__name__Ú __module__Ú __qualname__Ú__doc__ryrnrrrrrls rlc@s,eZdZdZdd„Zd dd„Zd dd„ZdS) rpz A converting dictionary wrapper.cCst ||¡}| ||¡S)N)ÚdictÚ __getitem__ry)rurtrvrrrrBs zConvertingDict.__getitem__NcCst |||¡}| ||¡S)N)r~r1ry)rurtÚdefaultrvrrrr1FszConvertingDict.getcCst |||¡}|j||ddS)NF)rw)r~rry)rurtr€rvrrrrJszConvertingDict.pop)N)N)rzr{r|r}rr1rrrrrrp?s rpc@s"eZdZdZdd„Zddd„ZdS) rqzA converting list wrapper.cCst ||¡}| ||¡S)N)rYrry)rurtrvrrrrPs zConvertingList.__getitem__éÿÿÿÿcCst ||¡}| |¡S)N)rYrrn)ruÚidxrvrrrrTs zConvertingList.popN)r)rzr{r|r}rrrrrrrqNsrqc@seZdZdZdd„ZdS)rrzA converting tuple wrapper.cCst ||¡}|j||ddS)NF)rw)Útuplerry)rurtrvrrrrZs zConvertingTuple.__getitem__N)rzr{r|r}rrrrrrrXsrrc@sŒeZdZdZe d¡Ze d¡Ze d¡Ze d¡Z e d¡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)rpÚconfigrm)rur‡rrrÚ__init__ss zBaseConfigurator.__init__c CsÄ| d¡}| d¡}y`| |¡}xP|D]H}|d|7}yt||ƒ}Wq&tk rl| |¡t||ƒ}Yq&Xq&W|Stk r¾t ¡dd…\}}td||fƒ}|||_ |_ |‚YnXdS)z` Resolve strings to objects using standard import and attribute syntax. rrrXNzCannot resolve %r: %s) rrÚimporterrrÚ ImportErrorÚsysÚexc_inforhÚ __cause__Ú __traceback__) rurirrr ZfragÚeÚtbÚvrrrÚresolvews"      zBaseConfigurator.resolvecCs | |¡S)z*Default converter for the ext:// protocol.)r’)rurvrrrr…ŽszBaseConfigurator.ext_convertcCs|}|j |¡}|dkr&td|ƒ‚nØ|| ¡d…}|j| ¡d}x´|rü|j |¡}|rp|| ¡d}nd|j |¡}|rÔ| ¡d}|j |¡s¢||}n2yt |ƒ}||}Wnt k rÒ||}YnX|rê|| ¡d…}qJtd||fƒ‚qJW|S)z*Default converter for the cfg:// protocol.NzUnable to convert %rrzUnable to convert %r at %r) Ú WORD_PATTERNrgrhÚendr‡ÚgroupsÚ DOT_PATTERNÚ INDEX_PATTERNÚ DIGIT_PATTERNÚintÚ TypeError)rurvÚrestrjÚdr‚r!rrrr†’s2       zBaseConfigurator.cfg_convertcCsÆ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)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)rrpr~rmrqrYrrrƒr$ÚCONVERT_PATTERNrgÚ groupdictÚvalue_convertersr1r)rurvrjrœrZ converterržrrrrn´s*     zBaseConfigurator.convertcsnˆ d¡}t|ƒs| |¡}ˆ dd¡}‡fdd„ˆDƒ}|f|Ž}|rjx | ¡D]\}}t|||ƒqRW|S)z1Configure an object with a user-supplied factory.z()rNcsi|]}t|ƒrˆ||“qSr)rk)Ú.0Úk)r‡rrú ×sz5BaseConfigurator.configure_custom..)rÚcallabler’ÚitemsÚsetattr)rur‡r3Úpropsr9rxrrvr)r‡rÚconfigure_customÐs    z!BaseConfigurator.configure_customcCst|tƒrt|ƒ}|S)z0Utility function which converts lists to tuples.)rrYrƒ)rurvrrrÚas_tupleÞs zBaseConfigurator.as_tupleN)rzr{r|r}ÚreÚcompilerŸr“r–r—r˜r¡Ú staticmethodrr‰rˆr’r…r†rnr©rªrrrrr„_s      "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. c Csì|j}d|krtdƒ‚|ddkr2td|dƒ‚| dd¡}i}t ¡z|rª| d|¡}xŒ|D]„}|tjkr‚td|ƒ‚qfy6tj|}||}| d d ¡}|r¶| t |¡¡Wqft k rè} ztd |ƒ| ‚Wd d } ~ XYqfXqfW| d |¡} xX| D]P}y|  || |d ¡Wn2t k rL} ztd|ƒ| ‚Wd d } ~ XYnXqW| dd ¡} | rÚy|  | d ¡Wn.t k r¤} ztdƒ| ‚Wd d } ~ XYnXn0| dd ¡} t ƒ| d|¡} xX| D]P}y|  | |¡| |<Wn2t k r} ztd|ƒ| ‚Wd d } ~ XYnXqÎW| d|¡}xX|D]P}y| ||¡||<Wn2t k r€} ztd|ƒ| ‚Wd d } ~ XYnXq4W| d|¡}g}x‚t|ƒD]v}y | ||¡}||_|||<WnNt k r} z.dt| jƒkrö| |¡ntd |ƒ| ‚Wd d } ~ XYnXq¢Wxb|D]Z}y | ||¡}||_|||<Wn2t k rx} ztd |ƒ| ‚Wd d } ~ XYnXq"Wtj} t| jj ¡ƒ}| ¡g}| d |¡} xÎ| D]Æ}||kr0| |¡d}|d}t|ƒ}t|ƒ}x<||kr$||d |…|kr| ||¡|d7}qêW| |¡y|  || |¡Wn2t k rv} ztd|ƒ| ‚Wd d } ~ XYnXq´Wt||| ƒ| dd ¡} | rÚy|  | ¡Wn.t k rØ} ztdƒ| ‚Wd d } ~ XYnXWd t ¡Xd S)zDo the configuration.Úversionz$dictionary doesn't specify a versionrXzUnsupported version: %sÚ incrementalFrzNo handler found with name %rr:NzUnable to configure handler %rrVTzUnable to configure logger %rrKzUnable to configure root loggerrrz Unable to configure formatter %rÚfilterszUnable to configure filter %rztarget not configured yetr) r‡rhrr r r1rdr?Ú _checkLevelÚ ExceptionÚconfigure_loggerÚconfigure_rootr Úconfigure_formatterÚconfigure_filterÚsortedÚconfigure_handlerrr$rrCrKrYrLrMr'r]r_r0rZrUr)rur‡r°Z EMPTY_DICTrrÚhandlerZhandler_configr:rrVrKrRrr±ZdeferredrPrQr`rarbrcrrrÚ configureêsÔ           "    "  "   "  "      " zDictConfigurator.configurec CsÆd|krr|d}y| |¡}WqÂtk rn}z2dt|ƒkr>‚| d¡|d<||d<| |¡}Wdd}~XYqÂXnP| dd¡}| dd¡}| dd¡}| d d¡}|s®tj} nt|ƒ} | |||ƒ}|S) z(Configure a formatter from a dictionary.z()z'format'r)rHNr,r-r.r/)r©ršr$rr1r r2r") rur‡ÚfactoryrxÚterHZdfmtr-Úcnamer3rrrr¶ˆs&      z$DictConfigurator.configure_formattercCs.d|kr| |¡}n| dd¡}t |¡}|S)z%Configure a filter from a dictionary.z()rr7)r©r1r ZFilter)rur‡rxrrrrr·¤s    z!DictConfigurator.configure_filterc Cs\xV|D]N}y| |jd|¡Wqtk rR}ztd|ƒ|‚Wdd}~XYqXqWdS)z/Add filters to a filterer from a list of names.r±zUnable to add filter %rN)Z addFilterr‡r³rh)ruZfiltererr±r5rrrrÚ add_filters­s  zDictConfigurator.add_filtersc sˆtˆƒ}ˆ dd¡}|r\y|jd|}Wn0tk rZ}ztd|ƒ|‚Wdd}~XYnXˆ dd¡}ˆ dd¡}dˆkr ˆ d¡}t|ƒs˜| |¡}|}nˆ d¡} | | ¡} t| tj j ƒrFd ˆkrFy>|jd ˆd } t | tj ƒsˆ  |¡td ƒ‚| ˆd <Wn6tk rB}ztd ˆd ƒ|‚Wdd}~XYnXnZt| tj jƒrtd ˆkrt| ˆd ¡ˆd <n,t| tj jƒr dˆkr | ˆd¡ˆd<| }ˆ dd¡} ‡fdd„ˆDƒ} y|f| Ž}WnLtk r}z,dt|ƒkrô‚|  d¡| d<|f| Ž}Wdd}~XYnX|r.| |¡|dk rH| t |¡¡|rZ| ||¡| r„x"|  ¡D]\}}t|||ƒqjW|S)z&Configure a handler from a dictionary.r6NrzUnable to set formatter %rr:r±z()r/r;rztarget not configured yetzUnable to set target handler %rZmailhostZaddressrcsi|]}t|ƒrˆ||“qSr)rk)r¢r£)r‡rrr¤Ýsz6DictConfigurator.configure_handler..z'stream'ÚstreamZstrm)r~rr‡r³rhr¥r’rAr rrBrZHandlerÚupdateršZ SMTPHandlerrªZ SysLogHandlerr$r@r?r²r¿r¦r§)rur‡Z config_copyr6rr:r±r3r¼r¾rGZthr¨r9rxr½rrvr)r‡rr¹µsl          "      z"DictConfigurator.configure_handlerc Cs\xV|D]N}y| |jd|¡Wqtk rR}ztd|ƒ|‚Wdd}~XYqXqWdS)z.Add handlers to a logger from a list of names.rzUnable to add handler %rN)r\r‡r³rh)rurTrrIrrrrÚ add_handlersôs  zDictConfigurator.add_handlersFcCs†| dd¡}|dk r$| t |¡¡|s‚x |jdd…D]}| |¡q8W| dd¡}|rf| ||¡| dd¡}|r‚| ||¡dS)zU Perform configuration which is common to root and non-root loggers. r:Nrr±)r1r?r r²rr[rÂr¿)rurTr‡r°r:rIrr±rrrÚcommon_logger_configüs    z%DictConfigurator.common_logger_configcCs6t |¡}| |||¡| dd¡}|dk r2||_dS)z.Configure a non-root logger from a dictionary.rNN)r r^rÃr1rN)rurr‡r°rTrNrrrr´s   z!DictConfigurator.configure_loggercCst ¡}| |||¡dS)z*Configure a root logger from a dictionary.N)r r^rÃ)rur‡r°rKrrrrµszDictConfigurator.configure_rootN)F)F)F) rzr{r|r}r»r¶r·r¿r¹rÂrÃr´rµrrrrr®äs ?  r®cCst|ƒ ¡dS)z%Configure logging using a dictionary.N)ÚdictConfigClassr»)r‡rrrÚ dictConfigsrÅcsDGdd„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. c@seZdZdZdd„ZdS)z#listen..ConfigStreamHandlerz¤ Handler for a logging configuration request. It expects a completely new logging configuration and uses fileConfig to install it. c Ss\y"|j}| d¡}t|ƒdkr"t d|¡d}|j |¡}x&t|ƒ|krd|| |t|ƒ¡}q@W|jjdk r~|j |¡}|dk r | d¡}y,ddl}|  |¡}t |t ƒs´t ‚t |ƒWnLtk r t |¡}y t|ƒWntk rt ¡YnXYnX|jjr"|jj ¡Wn2tk rV}z|jtkrF‚Wdd}~XYnXdS)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)ruZconnÚchunkZslenrËrœÚfilerrrrÚhandle=s8            z*listen..ConfigStreamHandler.handleN)rzr{r|r}rÙrrrrÚConfigStreamHandler6srÚc@s0eZdZdZdZdedddfdd„Zdd„ZdS) z$listen..ConfigSocketReceiverzD A simple TCP socket-based logging config receiver. rXZ localhostNcSs>t |||f|¡t ¡d|_t ¡d|_||_||_dS)NrrX) rrˆr r ÚabortrÚtimeoutrÒrÉ)ruZhostÚportrºrÒrÉrrrrˆksz-listen..ConfigSocketReceiver.__init__cSsdddl}d}xJ|sV| |j ¡ggg|j¡\}}}|r>| ¡t ¡|j}t ¡qW|  ¡dS)Nr) ÚselectZsocketÚfilenorÜZhandle_requestr r rÛrZ server_close)rurÞrÛZrdZwrZexrrrÚserve_until_stoppedus z8listen..ConfigSocketReceiver.serve_until_stopped)rzr{r|r}Zallow_reuse_addressÚDEFAULT_LOGGING_CONFIG_PORTrˆràrrrrÚConfigSocketReceiverds  râcs&eZdZ‡‡fdd„Zdd„Z‡ZS)zlisten..Servercs4tˆ|ƒ ¡||_||_||_||_t ¡|_dS)N) ÚsuperrˆÚrcvrÚhdlrrÝrÉÚ threadingZEventrÒ)rurärårÝrÉ)ÚServerÚ __class__rrrˆ…s zlisten..Server.__init__cSsZ|j|j|j|j|jd}|jdkr0|jd|_|j ¡t ¡|a t  ¡|  ¡dS)N)rÝrºrÒrÉrrX) rärÝrårÒrÉZserver_addressrÓr r Ú _listenerrrà)rurÈrrrÚruns     zlisten..Server.run)rzr{r|rˆrêÚ __classcell__r)rç)rèrrçƒsrç)rrræZThread)rÝrÉrÚrâr)rçrÚlisten"s.rìcCs*t ¡ztrdt_daWdt ¡XdS)zN Stop the listening server which was created with a call to listen(). rXN)r r rérÛrrrrrÚ stopListeningœs rí)NT)+r}rÕrÎr Zlogging.handlersr«rÇr‹rærÐZ socketserverrrráZ ECONNRESETrÖrérr"r&r r rUrr r¬ÚIrfrkÚobjectrlr~rprYrqrƒrrr„r®rÄrÅrìrírrrrÚsH "%W! 9z