B €/g^­Œc@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-2019 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). rNZreadline)Ú configparserÚ isinstanceZRawConfigParserZ ConfigParserÚhasattrZ read_fileZreadÚ_create_formattersÚloggingÚ _acquireLockÚ_clearExistingHandlersÚ_install_handlersÚ_install_loggersÚ _releaseLock)ZfnameZdefaultsÚdisable_existing_loggersrÚcpÚ formattersÚhandlers©rú&/usr/lib64/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    rcCs ttj|ƒS©N)ZmapÚstrZstrip)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)ZrawÚfallbackÚdatefmtÚstyleú%Úclass)Úlenrr!ÚgetrÚ Formatterr) rZflistrZformZsectnameZfsZdfsZstlÚcZ class_nameÚfrrrrfs$     rc 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ÚÚargsú()Úkwargsz{}ÚlevelÚtarget)r*rr!r+ZevalZvarsrrZ NameErrorrÚsetLevelÚ setFormatterÚ issubclassrÚ MemoryHandlerÚappendZ setTarget)rrÚhlistrZfixupsÚhandÚsectionÚklassÚfmtr1r3Úhr4r5Ztrrrr |sB            r cCsXtj}xL|D]D}|jj|}||krJt|tjƒsP| 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Ú loggerDictrZ PlaceHolderr6ZNOTSETrÚ propagateÚdisabled)ÚexistingÚ child_loggersÚdisable_existingrAÚlogÚloggerrrrÚ_handle_existing_loggers¡s     rKcCs&|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#rAZ logger_rootr4Nrz logger_%sZqualnamerDé)r%rr)rÚlistr!ÚremoverrAr6rÚ removeHandlerr*Ú addHandlerrBrCr"ÚsortZgetintÚ getLoggerÚindexr:rDrErK)rrrHZllistr=rArIr4r@r;r<rFrGZqnrDrJÚiÚprefixedÚpflenÚ num_existingrrrr ·sd                 r cCs.tj ¡t tjdd…¡tjdd…=dS)z!Clear and close existing handlersN)rÚ _handlersZclearZshutdownZ _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  r_c@s"eZdZdZddd„Zdd„ZdS) ÚConvertingMixinz?For ConvertingXXX's, this mixin class provides common functionsTcCsB|j |¡}||k r>|r |||<t|ƒtttfkr>||_||_|Sr)Ú configuratorÚconvertÚtypeÚConvertingDictÚConvertingListÚConvertingTupleÚparentÚkey)ÚselfrhÚvalueÚreplaceÚresultrrrÚconvert_with_key"s  z ConvertingMixin.convert_with_keycCs0|j |¡}||k r,t|ƒtttfkr,||_|Sr)rarbrcrdrerfrg)rirjrlrrrrb.s   zConvertingMixin.convertN)T)Ú__name__Ú __module__Ú __qualname__Ú__doc__rmrbrrrrr`s r`c@s,eZdZdZdd„Zd dd„Zd dd„ZdS) rdz A converting dictionary wrapper.cCót ||¡}| ||¡Sr)ÚdictÚ __getitem__rm©rirhrjrrrrtCó zConvertingDict.__getitem__NcCst |||¡}| ||¡Sr)rsr+rm©rirhZdefaultrjrrrr+GózConvertingDict.getcCst |||¡}|j||ddS©NF)rk)rsrrmrwrrrrKrxzConvertingDict.poprr)rnrorprqrtr+rrrrrrd@s rdc@s"eZdZdZdd„Zddd„ZdS) rezA converting list wrapper.cCrrr)rNrtrmrurrrrtQrvzConvertingList.__getitem__éÿÿÿÿcCst ||¡}| |¡Sr)rNrrb)riÚidxrjrrrrUrvzConvertingList.popN)rz)rnrorprqrtrrrrrreOsrec@óeZdZdZdd„ZdS)rfzA converting tuple wrapper.cCst ||¡}|j||ddSry)Útuplertrmrurrrrt[s zConvertingTuple.__getitem__N)rnrorprqrtrrrrrfYsrfc@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_dSr)rdÚconfigra)rirrrrÚ__init__ts 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. rrrMNzCannot resolve %r: %s) rrÚimporterrrZ ImportErrorÚsysZexc_infor\Ú __cause__Z __traceback__) rir]rrrZfragÚeZtbZvrrrÚresolvexs"      zBaseConfigurator.resolvecCs | |¡S)z*Default converter for the ext:// protocol.)r‡©rirjrrrrózBaseConfigurator.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_PATTERNr[r\ZendrZgroupsÚ DOT_PATTERNÚ INDEX_PATTERNÚ DIGIT_PATTERNZintÚ TypeError)rirjZrestr^Údr{rrrrr€“s2       zBaseConfigurator.cfg_convertcCsÐt|tƒs$t|tƒr$t|ƒ}||_n¨t|tƒsHt|tƒrHt|ƒ}||_n„t|tƒsvt|tƒrvt|dƒsvt|ƒ}||_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. Z_fieldsÚprefixNÚsuffix)rrdrsrarerNrfr}rr ÚCONVERT_PATTERNr[Z groupdictÚvalue_convertersr+r)rirjr^rrZ converterr‘rrrrbµs*    zBaseConfigurator.convertcsnˆ d¡}t|ƒs| |¡}ˆ dd¡}‡fdd„ˆDƒ}|f|Ž}|rjx | ¡D]\}}t|||ƒqRW|S)z1Configure an object with a user-supplied factory.r2rNcói|]}t|ƒrˆ||“qSr©r_©Z.0Zk©rrrú Øóz5BaseConfigurator.configure_custom..)rÚcallabler‡ÚitemsÚsetattr)rirr-Úpropsr3rlrrjrr—rÚconfigure_customÑs    z!BaseConfigurator.configure_customcCst|tƒrt|ƒ}|S)z0Utility function which converts lists to tuples.)rrNr}rˆrrrÚas_tupleßs zBaseConfigurator.as_tupleN)rnrorprqÚreÚcompiler’rŠr‹rŒrr“Z staticmethodrrƒr‚r‡rr€rbrž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.Zversionz$dictionary doesn't specify a versionrMzUnsupported version: %sÚ incrementalFrzNo handler found with name %rr4NzUnable to configure handler %rrLTzUnable to configure logger %rrAzUnable to configure root loggerr rz Unable to configure formatter %rÚfilterszUnable to configure filter %rútarget not configured yetr) rr\rrrr+rYr6Ú _checkLevelÚ ExceptionÚconfigure_loggerÚconfigure_rootr Úconfigure_formatterÚconfigure_filterZsortedÚconfigure_handlerrr r…r:rArNrBrCr"rRrTr*rOrKr )rirr£Z EMPTY_DICTrrÚhandlerZhandler_configr4r†rLrArHrr¤ZdeferredrFrGrUrVrWrXrrrÚ 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.r2z'format'r$r?Nr&r'r(r))ržrŽr rr+rr,r) rirÚfactoryrlÚter?Zdfmtr'Úcnamer-rrrrª‰s&      z$DictConfigurator.configure_formattercCs.d|kr| |¡}n| dd¡}t |¡}|S)z%Configure a filter from a dictionary.r2rr0)ržr+rZFilter)rirrlrrrrr«¥s    z!DictConfigurator.configure_filterc Có\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 addFilterrr§r\)riZfiltererr¤r.r†rrrÚ add_filters®ó  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.r/NrzUnable to set formatter %rr4r¤r2r)r5rr¥zUnable to set target handler %rZmailhostZaddressrcr”rr•r–r—rrr˜Þr™z6DictConfigurator.configure_handler..z'stream'ZstreamZstrm)rsrrr§r\ršr‡r8rrr9rZHandlerZupdaterŽZ SMTPHandlerrŸZ SysLogHandlerr r7r6r¦r³r›rœ)rirZ config_copyr/r†r4r¤r-r¯r±r>Zthrr3rlr°rrjrr—rr¬¶sl          "      z"DictConfigurator.configure_handlerc Cr²)z.Add handlers to a logger from a list of names.rzUnable to add handler %rN)rQrr§r\)rirJrr@r†rrrÚ add_handlersõr´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. r4Nrr¤)r+r6rr¦rrPrµr³)rirJrr£r4r@rr¤rrrÚcommon_logger_configýs    z%DictConfigurator.common_logger_configcCs6t |¡}| |||¡| dd¡}|dk r2||_dS)z.Configure a non-root logger from a dictionary.rDN)rrSr¶r+rD)rirrr£rJrDrrrr¨s   z!DictConfigurator.configure_loggercCst ¡}| |||¡dS)z*Configure a root logger from a dictionary.N)rrSr¶)rirr£rArrrr©szDictConfigurator.configure_rootN©Fr·r·) rnrorprqr®rªr«r³r¬rµr¶r¨r©rrrrr¢ås ?  r¢cCst|ƒ ¡dS)z%Configure logging using a dictionary.N)ÚdictConfigClassr®r—rrrÚ dictConfigr‰r¹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@r|)z#listen..ConfigStreamHandlerz¤ Handler for a logging configuration request. It expects a completely new logging configuration and uses fileConfig to install it. c SsHy|j}| d¡}t|ƒdkrt d|¡d}|j |¡}x&t|ƒ|krd|| |t|ƒ¡}q@W|jjdk r~|j |¡}|dk rø| d¡}yddl}|  |¡}t |ƒWnHt k röt   |¡}y t|ƒWnt k rðt ¡YnXYnX|jjr|jj ¡Wn2tk rB}z|jtkr2‚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. iz>LrNzutf-8)Z connectionZrecvr*ÚstructZunpackÚserverÚverifyZdecodeÚjsonZloadsr¹r§ÚioZStringIOrÚ tracebackZ print_excÚreadyÚsetZOSErrorÚerrnoÚ RESET_ERROR)riZconnZchunkZslenr½rZfiler†rrrÚhandle>s6           z*listen..ConfigStreamHandler.handleN)rnrorprqrÄrrrrÚConfigStreamHandler7srÅc@s0eZdZdZdZdedddfdd„Zdd„ZdS) z$listen..ConfigSocketReceiverzD A simple TCP socket-based logging config receiver. rMZ localhostNcSs>t |||f|¡t ¡d|_t ¡d|_||_||_dS)NrrM) rr‚rrÚabortr ÚtimeoutrÀr¼)riZhostÚportr­rÀr¼rrrr‚lsz-listen..ConfigSocketReceiver.__init__cSsdddl}d}xJ|sV| |j ¡ggg|j¡\}}}|r>| ¡t ¡|j}t ¡qW|  ¡dS)Nr) ÚselectZsocketZfilenorÇZhandle_requestrrrÆr Z server_close)rirÉrÆZrdZwrZexrrrÚserve_until_stoppedvs z8listen..ConfigSocketReceiver.serve_until_stopped)rnrorprqZallow_reuse_addressÚDEFAULT_LOGGING_CONFIG_PORTr‚rÊrrrrÚConfigSocketReceiveres  rÌcs&eZdZ‡‡fdd„Zdd„Z‡ZS)zlisten..Servercs4tˆ|ƒ ¡||_||_||_||_t ¡|_dSr) Zsuperr‚ÚrcvrÚhdlrrÈr¼Ú threadingZEventrÀ)rirÍ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¼rrM) rÍrÈrÎrÀr¼Zserver_addressrÁrrÚ _listenerr rÊ)rir»rrrÚrunŽs     zlisten..Server.run)rnrorpr‚rÓZ __classcell__r©rÐ)rÑrrЄsrÐ)rrrÏZThread)rÈr¼rÅrÌrrÔrÚlisten#s.rÕcCs*t ¡ztrdt_daWdt ¡XdS)zN Stop the listening server which was created with a call to listen(). rMN)rrrÒrÆr rrrrÚ stopListenings rÖ)NT)+rqrÂr¾rZlogging.handlersr rºr„rÏr¿Z socketserverrrrËZ ECONNRESETrÃrÒrrr!rr rKr r r¡ZIrZr_Zobjectr`rsrdrNrer}rfr~r¢r¸r¹rÕrÖrrrrZsH "%W! 9z