o ?Og@sNdZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl m Z m Z dZ ejZdad+ddZdd Zd d Zd d ZddZddZddZddZedejZddZGdddeZGdddeeZ Gddde!eZ"Gdd d e#eZ$Gd!d"d"eZ%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)ThreadingTCPServerStreamRequestHandleriF#TcCsddl}t||jr |}n||}t|dr||n t|}|j||dt |}t zt t ||}t|||Wt dSt w)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). rNreadline)encoding) configparser isinstanceZRawConfigParserZ ConfigParserhasattrZ read_fileio text_encodingread_create_formatterslogging _acquireLock_clearExistingHandlers_install_handlers_install_loggers _releaseLock)Zfnamedefaultsdisable_existing_loggersrrcp formattershandlersr5/opt/alt/python310/lib64/python3.10/logging/config.py fileConfig3s      rc Csh|d}|d}t|}|D]!}|d|}zt||}Wqty1t|t||}Yqw|S)z)Resolve a dotted name to a global object..r)splitpop __import__getattrAttributeError)nameusedfoundnrrr_resolveVs    r%cCs ttj|SN)mapstrstrip)Zalistrrr _strip_spacesds r*c Cs|dd}t|s iS|d}t|}i}|D];}d|}|j|dddd}|j|d ddd}|j|d dd d}tj}||d } | rJt| }||||} | ||<q|S) zCreate and return formattersrkeys,z formatter_%sformatTN)rawfallbackdatefmtstyle%class)lenrr*getr Formatterr%) rZflistrZformZsectnameZfsZdfsZstlc class_namefrrrr gs$    r c Cs^|dd}t|s iS|d}t|}i}g}|D]}|d|}|d}|dd}z t|tt}WnttfyEt |}Ynw|dd } t| tt} |d d } t| tt} || i| } || _ d |vrw|d } | | t|r| ||t |tjjr|d d} t| r|| | f| ||<q|D] \} }| ||q|S)zInstall and return handlersrr+r,z handler_%sr3 formatterargs()kwargsz{}leveltarget)r4rr*r5evalvarsr r NameErrorr%r!setLevel setFormatter issubclassr MemoryHandlerappendZ setTarget)rrhlistrZfixupshandsectionklassfmtr<r>hr?r@trrrr}sF           rcCsTtj}|D]"}|jj|}||vr$t|tjs#|tjg|_d|_ q||_ qdS)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 rootmanager loggerDictrZ PlaceHolderrDZNOTSETr propagatedisabled)existing child_loggersdisable_existingrPlogloggerrrr_handle_existing_loggerss    rZcCs|dd}|d}tt|}|d|d}tj}|}d|vr,|d}|||jddD]}||q3|d} t | rX| d} t| } | D] } | || qNt|j j } | g} |D]}|d |}|d } |jd d d }t| }| | vr| | d }| d}t |}t | }||kr| |d||kr| | ||d 7}||ks| | d|vr|d}|||jddD]}||q||_d|_|d} t | r| d} t| } | D] } | || qqht| | |dS)zCreate and install loggersloggersr+r,rPZ logger_rootr?Nrz logger_%squalnamerS)r/rr)rlistr*remover rPrDr removeHandlerr4 addHandlerrQrRr+sortZgetint getLoggerindexrHrSrTrZ)rrrWZllistrKrPrXr?rNrIrJrUrVZqnrSrYiprefixedpflen num_existingrrrrsh              rcCs.tjttjddtjdd=dS)z!Clear and close existing handlersN)r _handlersclearZshutdownZ _handlerListrrrrrs rz^[a-z_][a-z0-9_]*$cCst|}|s td|dS)Nz!Not a valid Python identifier: %rT) IDENTIFIERmatch ValueError)smrrr valid_idents  rpc@s"eZdZdZdddZddZdS) ConvertingMixinz?For ConvertingXXX's, this mixin class provides common functionsTcCsB|j|}||ur|r|||<t|tttfvr||_||_|Sr&) configuratorconverttypeConvertingDictConvertingListConvertingTupleparentkey)selfryvaluereplaceresultrrrconvert_with_key$s  z ConvertingMixin.convert_with_keycCs0|j|}||urt|tttfvr||_|Sr&)rrrsrtrurvrwrx)rzr{r}rrrrs0s  zConvertingMixin.convertN)T)__name__ __module__ __qualname____doc__r~rsrrrrrq!s  rqc@s,eZdZdZddZd ddZd ddZdS) ruz A converting dictionary wrapper.cCt||}|||Sr&)dict __getitem__r~rzryr{rrrrE  zConvertingDict.__getitem__NcCst|||}|||Sr&)rr5r~rzrydefaultr{rrrr5Is zConvertingDict.getcCst|||}|j||ddSNF)r|)rrr~rrrrrMszConvertingDict.popr&)rrrrrr5rrrrrruBs  ruc@s"eZdZdZddZdddZdS) rvzA converting list wrapper.cCrr&)r^rr~rrrrrSrzConvertingList.__getitem__cCst||}||Sr&)r^rrs)rzidxr{rrrrWs  zConvertingList.popN)r)rrrrrrrrrrrvQsrvc@eZdZdZddZdS)rwzA converting tuple wrapper.cCst||}|j||ddSr)tuplerr~rrrrr]s zConvertingTuple.__getitem__N)rrrrrrrrrrw[s rwc@seZdZdZedZedZedZedZ edZ ddd Z e e Zd d Zd d ZddZddZddZddZddZdS)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&)ruconfigrr)rzrrrr__init__vs  zBaseConfigurator.__init__c Cs|d}|d}z-||}|D]"}|d|7}zt||}Wqty4||t||}Yqw|WStyYtdd\}}td||f}|||_ |_ |w)z` Resolve strings to objects using standard import and attribute syntax. rrr]NzCannot resolve %r: %s) rrimporterrr ImportErrorsysexc_inform __cause__ __traceback__) rzrnr!r"r#Zfragetbvrrrresolvezs&       zBaseConfigurator.resolvecCs ||S)z*Default converter for the ext:// protocol.)rrzr{rrrrs zBaseConfigurator.ext_convertcCs|}|j|}|durtd|||d}|j|d}|r||j|}|r6||d}n1|j|}|rg|d}|j|sO||}nz t |}||}Wn t yf||}Ynw|rr||d}ntd||f|s%|S)z*Default converter for the cfg:// protocol.NzUnable to convert %rrzUnable to convert %r at %r) WORD_PATTERNrlrmendrgroups DOT_PATTERN INDEX_PATTERN DIGIT_PATTERNint TypeError)rzr{restrodrr$rrrrs8          zBaseConfigurator.cfg_convertcCst|tst|trt|}||_|St|ts&t|tr&t|}||_|St|ts>t|tr>t|ds>t|}||_|St|t ri|j |}|ri| }|d}|j |d}|ri|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. _fieldsprefixNsuffix)rrurrrrvr^rwrrr(CONVERT_PATTERNrl groupdictvalue_convertersr5r)rzr{rorrZ converterrrrrrss6   zBaseConfigurator.convertcsnd}t|s||}dd}fddD}|di|}|r5|D] \}}t|||q*|S)z1Configure an object with a user-supplied factory.r=rNci|] }t|r||qSrrp.0krrr z5BaseConfigurator.configure_custom..r)rcallableritemssetattr)rzrr7propsr>r}r!r{rrrconfigure_customs   z!BaseConfigurator.configure_customcCst|tr t|}|S)z0Utility function which converts lists to tuples.)rr^rrrrras_tuples zBaseConfigurator.as_tupleN)rrrrrecompilerrrrrr staticmethodrrrrrrrsrrrrrrrbs$     " rc@s^eZdZdZddZddZddZdd Zd d Zd d Z dddZ dddZ dddZ dS)DictConfiguratorz] Configure logging using a dictionary-like object to describe the configuration. c Cs|j}d|vr td|ddkrtd|d|dd}i}tz |r|d|}|D];}|tjvr>td|ztj|}||}|d d }|rX|t|Wq1t yl} ztd || d } ~ ww|d |} | D] }z | || |d Wqut y} ztd|| d } ~ ww|dd } | rz | | d Wnt y} ztd| d } ~ wwnp|dd } t |d|} | D] }z | | || |<Wqt y} ztd|| d } ~ ww|d|}|D]!}z |||||<Wqt y} ztd|| d } ~ ww|d|}g}t|D]<}z|||}||_|||<Wq#t y_} zdt| jvrM||ntd || WYd } ~ q#d } ~ ww|D]'}z|||}||_|||<Wqbt y} ztd || d } ~ wwtj} t| jj}|g}|d |} | D]^}||vr||d}|d}t|}t|}||kr||d ||kr||||d7}||ks||z | || |Wqt y} ztd|| d } ~ wwt||| |dd } | r9z| | Wn't y*} ztd| d } ~ wwWtd SWtd SWtd SWtd Stw)zDo the configuration.versionz$dictionary doesn't specify a versionr]zUnsupported version: %s incrementalFrzNo handler found with name %rr?NzUnable to configure handler %rr[TzUnable to configure logger %rrPzUnable to configure root loggerrrz Unable to configure formatter %rfilterszUnable to configure filter %rtarget not configured yetr) rrmrr rr5rirD _checkLevel Exceptionconfigure_loggerconfigure_rootrconfigure_formatterconfigure_filtersortedconfigure_handlerr!r(rrHrPr^rQrRr+rbrdr4r_rZr)rzrrZ EMPTY_DICTrr!handlerZhandler_configr?rr[rPrWrrZdeferredrUrVrerfrgrhrrr configuresB                     x v zDictConfigurator.configurec Csd|vr;|d}z||}W|Sty:}zdt|vr|d|d<||d<||}WYd}~|Sd}~ww|dd}|dd}|dd}|d d}|sYtj} nt|} d |vrl| ||||d }|S| |||}|S) z(Configure a formatter from a dictionary.r=z'format'r-rMNr0r1r2r3Zvalidate)rrr(rr5r r6r%) rzrfactoryr}terMZdfmtr1cnamer7rrrrs4      z$DictConfigurator.configure_formattercCs0d|vr ||}|S|dd}t|}|S)z%Configure a filter from a dictionary.r=r!r;)rr5r ZFilter)rzrr}r!rrrrs   z!DictConfigurator.configure_filterc CL|D]!}z ||jd|Wqty#}ztd||d}~wwdS)z/Add filters to a filterer from a list of names.rzUnable to add filter %rN)Z addFilterrrrm)rzZfiltererrr9rrrr add_filterszDictConfigurator.add_filtersc sbt}dd}|r)z |jd|}Wnty(}ztd||d}~wwdd}dd}dvrJd}t|sG||}|}ntd} || } t| tj j rd vrz|jd d } t | tj sx |td | d <Wn>ty}z td d |d}~wwt| tj jrd vr|d d <nt| tj jrdvr|dd<| }dd} fddD} z |di| }Wn'ty}zdt|vr| d| d<|di| }WYd}~nd}~ww|r|||dur|t||r|||| r/| D] \}}t|||q#|S)z&Configure a handler from a dictionary.r:NrzUnable to set formatter %rr?rr=r3r@rrzUnable to set target handler %rZmailhostZaddressrcrrrrrrrrrz6DictConfigurator.configure_handler..z'stream'streamZstrmr)rrrrrmrrrFr rrGrZHandlerupdaterZ SMTPHandlerrZ SysLogHandlerr(rErDrrrr)rzrZ config_copyr:rr?rr7rrrLZthrr>r}rr!r{rrrrs               z"DictConfigurator.configure_handlerc Cr)z.Add handlers to a logger from a list of names.rzUnable to add handler %rN)rarrrm)rzrYrrNrrrr add_handlersrzDictConfigurator.add_handlersFcCs|dd}|dur|t||sA|jddD]}||q|dd}|r1||||dd}|rC|||dSdSdS)zU Perform configuration which is common to root and non-root loggers. r?Nrr)r5rDr rrr`rr)rzrYrrr?rNrrrrrcommon_logger_configs     z%DictConfigurator.common_logger_configcCs@t|}||||d|_|dd}|dur||_dSdS)z.Configure a non-root logger from a dictionary.FrSN)r rcrrTr5rS)rzr!rrrYrSrrrrs   z!DictConfigurator.configure_loggercCst}||||dS)z*Configure a root logger from a dictionary.N)r rcr)rzrrrPrrrr"szDictConfigurator.configure_rootN)F) rrrrrrrrrrrrrrrrrrs$ ?   rcCst|dS)z%Configure logging using a dictionary.N)dictConfigClassrrrrr dictConfig)srcsDGdddt}Gdddt}Gfdddtj||||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 SsNz|j}|d}t|dkrtd|d}|j|}t||kr4|||t|}t||ks#|jjdur@|j|}|dur{|d}zddl}| |}t |Wn"t yzt |}zt|Wn t ywtYnwYnw|jjr|jjWdSWdSWdSty}z |jtkrWYd}~dSd}~ww)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 connectionZrecvr4structZunpackserververifydecodejsonloadsrrr StringIOr traceback print_excreadysetOSErrorerrno RESET_ERROR)rzZconnchunkZslenrrfilerrrrhandleIsH               z*listen..ConfigStreamHandler.handleN)rrrrrrrrrConfigStreamHandlerBs rc@s0eZdZdZdZdedddfddZddZdS) z$listen..ConfigSocketReceiverzD A simple TCP socket-based logging config receiver. r]Z localhostNcSs>t|||f|td|_td|_||_||_dS)Nrr]) rrr rabortrtimeoutrr)rzZhostportrrrrrrrws z-listen..ConfigSocketReceiver.__init__cSsbddl}d}|s+||jggg|j\}}}|r|t|j}t|r| dS)Nr) selectZsocketfilenorZhandle_requestr rrrZ server_close)rzrrZrdZwrZexrrrserve_until_stoppeds  z8listen..ConfigSocketReceiver.serve_until_stopped)rrrrZallow_reuse_addressDEFAULT_LOGGING_CONFIG_PORTrrrrrrConfigSocketReceiverps rcs&eZdZfddZddZZS)zlisten..Servercs4t|||_||_||_||_t|_dSr&) superrrcvrhdlrrr threadingZEventr)rzrrrr)Server __class__rrrs zlisten..Server.__init__cSsZ|j|j|j|j|jd}|jdkr|jd|_|jt|a t | dS)N)rrrrrr]) rrrrrZserver_addressrr r _listenerrr)rzrrrrruns     zlisten..Server.run)rrrrr __classcell__rr)rrrsr)rrrZThread)rrrrrrrlisten.s.rcCs>tztrdt_daWtdSWtdStw)zN Stop the listening server which was created with a call to listen(). r]N)r rrrrrrrr stopListenings r)NTN)+rrr r Zlogging.handlersrrrrrZ socketserverrrrZ ECONNRESETrrrr%r*r rrZrrrIrkrpobjectrqrrur^rvrrwrrrrrrrrrrsJ  #&W! B z