3

T'ícUGã	@s†UdZddlZddlZddlmZddlmZddlmZm	Z	m
Z
mZmZm
Z
mZmZmZmZddlmZddlmZddlmZdd	lmZdd
lmZe	rªddlmZdZd
Zedd„ƒZ ee!e"f e j#ej$ej$ej%ej&ej'eej(dœƒedd„ƒZ)ee"e"f)e)j#ej'eej(dœƒedd„ej%dej&dej(diƒZ*e!ddœdd„Z+e
ej,ddœdd„Z-Gdd„dej,ƒZ.Gd d!„d!e.ƒZ/Gd"d#„d#e.ƒZ0Gd$d„dej1ƒZ2Gd%d&„d&ej3ƒZ4Gd'd(„d(ej3ƒZ5Gd)d*„d*ej6j7ƒZ8eeej9ddfd+œd,d-„ƒZ:eee8ddfd+œd.d/„ƒZ;eee8ddfd+œd0d1„ƒZ<ed]e=ed^d3œd4d5„ƒZ>ee!ed_d6œd7d8„ƒZ?Gd9d:„d:ƒZ@Gd;d<„d<ejAƒZBe!e!e
e!e=d=œd>d?„ZCGd@dA„dAejAƒZDGdBdC„dCejAƒZEGdDdE„dEejAƒZFGdFdG„dGejAƒZGGdHdI„dIejAƒZHGdJdK„dKejAƒZIGdLdM„dMeIƒZJGdNdO„dOeIƒZKeee!dPœdQdR„ZLGdSdT„dTejMƒZNGdUdV„dVƒZOGdWdX„dXƒZPdYeeddZœd[d\„ZQdS)`z%Logging utility functions for Sphinx.éN)Údefaultdict)Úcontextmanager)
ÚIOÚ
TYPE_CHECKINGÚAnyÚDictÚ	GeneratorÚListÚOptionalÚTupleÚTypeÚUnion)Únodes)ÚNode)Úget_source_line)Ú
SphinxWarning)Úcolorize)ÚSphinxZsphinxécCstjS)N)ÚloggingÚWARNING©rrú5/tmp/pip-build-gk9425m9/sphinx/sphinx/util/logging.pyÚ<lambda>sr)ÚCRITICALZSEVEREÚERRORrÚINFOÚVERBOSEÚDEBUGcCsdS)Nrrrrrrr"s)réécCsdS)NZbluerrrrrr)sZdarkredZredZdarkgrayÚSphinxLoggerAdapter)ÚnameÚreturncCs"tjtd|ƒ}d|_t|iƒS)aüGet logger wrapped by :class:`sphinx.util.logging.SphinxLoggerAdapter`.

    Sphinx logger always uses ``sphinx.*`` namespace to be independent from
    settings of root logger.  It ensures logging is consistent even if a
    third-party extension or imported application resets logger settings.

    Example usage::

        >>> from sphinx.util import logging
        >>> logger = logging.getLogger(__name__)
        >>> logger.info('Hello, this is an extension!')
        Hello, this is an extension!
    Ú.F)rÚ	getLoggerÚ	NAMESPACEÚdisabledr!)r"Úloggerrrrr%1sr%)Úrecordsr#cCsDx>|D]6}|jƒ|_f|_t|ddƒ}t|tjƒrt|ƒ|_qWdS)zConvert LogRecord serializable.ÚlocationN)	Ú
getMessageÚmsgÚargsÚgetattrÚ
isinstancerrÚget_node_locationr*)r)Úrr*rrrÚconvert_serializableGs

r2cs4eZdZUdZdZdZeedœ‡fdd„Z‡Z	S)ÚSphinxLogRecordz$Log record class supporting locationÚN)r#csDtƒjƒ}t|ddƒ}|r,d||j|f}n|j|kr@|j|}|S)Nr*z%s: %s%s)Úsuperr+r.Úprefix)ÚselfÚmessager*)Ú	__class__rrr+Xs


zSphinxLogRecord.getMessage)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r6r*rÚstrr+Ú
__classcell__rr)r9rr3Ss
r3c@seZdZdZdZdS)ÚSphinxInfoLogRecordz)Info log record class supporting locationr4N)r:r;r<r=r6rrrrr@csr@c@s"eZdZdZeedœdd„ƒZdS)ÚSphinxWarningLogRecordz,Warning log record class supporting location)r#cCs(|jtjkrdS|jtjkr dSdSdS)Nz
CRITICAL: zERROR: z	WARNING: )Úlevelnorrr)r7rrrr6js
zSphinxWarningLogRecord.prefixN)r:r;r<r=Úpropertyr>r6rrrrrAhsrAcs†eZdZdZddddddgZeeefeeedd	œ‡fd
d„Z	eeeddœd
d„Z
eeeeefdœdd„Z
ejddœdd„Z‡ZS)r!z9LoggerAdapter allowing ``type`` and ``subtype`` keywords.ÚtypeÚsubtyper*ÚnonlÚcolorÚonceN)Úlevelr,r-Úkwargsr#csDt|tƒr"tƒj||f|ž|Žnt|}tƒj||f|ž|ŽdS)N)r/Úintr5ÚlogÚLEVEL_NAMES)r7rIr,r-rJrB)r9rrrLxs
zSphinxLoggerAdapter.log)r,r-rJr#cOs|jt|f|ž|ŽdS)N)rLr)r7r,r-rJrrrÚverboseszSphinxLoggerAdapter.verbose)r,rJr#cCs:|jdiƒ}x$|jD]}||kr|j|ƒ||<qW||fS)NÚextra)Ú
setdefaultÚKEYWORDSÚpop)r7r,rJrOÚkeywordrrrÚprocess‚s
zSphinxLoggerAdapter.process)Úrecordr#cCs|jj|ƒdS)N)r(Úhandle)r7rUrrrrVŠszSphinxLoggerAdapter.handle)r:r;r<r=rQr
rKr>rrLrNrrrTrÚ	LogRecordrVr?rr)r9rr!ts"c@seZdZdZdS)ÚWarningStreamHandlerzStreamHandler for warnings.N)r:r;r<r=rrrrrXŽsrXcs*eZdZdZejddœ‡fdd„Z‡ZS)ÚNewLineStreamHandlerzAStreamHandler which switches line terminator by record.nonl flag.N)rUr#cs@z*|jƒt|ddƒrd|_tƒj|ƒWdd|_|jƒXdS)NrFFr4Ú
)Úacquirer.Ú
terminatorr5ÚemitÚrelease)r7rU)r9rrr]–szNewLineStreamHandler.emit)r:r;r<r=rrWr]r?rr)r9rrY“srYcsxeZdZUdZeejddœ‡fdd„Zeje	dœdd„Z
ddœd	d
„Zejddœdd
„Z
eejdœdd„Z‡ZS)Ú
MemoryHandlerzHandler buffering all logs.N)r#cstƒjdƒdS)Nréÿÿÿÿ)r5Ú__init__)r7)r9rrra§szMemoryHandler.__init__)rUr#cCsdS)NFr)r7rUrrrÚshouldFlushªszMemoryHandler.shouldFlushcCsdS)Nr)r7rrrÚflush­szMemoryHandler.flush)r(r#c
Cs<|jƒz$x|jD]}|j|ƒqWg|_Wd|jƒXdS)N)r[ÚbufferrVr^)r7r(rUrrrÚflushTo²s
zMemoryHandler.flushTocCs|jg}|_|S)N)rd)r7rdrrrÚclear»szMemoryHandler.clear)r:r;r<r=r	rrWrdraÚboolrbrcÚLoggerrerfr?rr)r9rr_¢s

	r_)r#ccsžtjtƒ}tƒ}|jtjƒzNg}x4|jdd…D]"}t|tƒr2|j	|ƒ|j
|ƒq2W|j|ƒ|VWd|j	|ƒx|D]}|j|ƒq|W|j|ƒXdS)zgContext manager to postpone logging warnings temporarily.

    Similar to :func:`pending_logging`.
    N)
rr%r&r_ÚsetLevelrÚhandlersr/rXÚ
removeHandlerÚappendÚ
addHandlerre)r(Ú
memhandlerrjÚhandlerrrrÚpending_warningsÀs






rpccs~tjtƒ}tƒ}zDg}x*|jdd…D]}|j|ƒ|j|ƒq&W|j|ƒ|VWd|j|ƒx|D]}|j|ƒqfWXdS)zèContext manager to suppress logging all logs temporarily.

    For example::

        >>> with suppress_logging():
        >>>     logger.warning('Warning message!')  # suppressed
        >>>     some_long_process()
        >>>
    N)rr%r&r_rjrkrlrm)r(rnrjrorrrÚsuppress_loggingÜs





rqccs8tjtƒ}ztƒ}|VWdQRXWd|j|ƒXdS)a$Context manager to postpone logging all logs temporarily.

    For example::

        >>> with pending_logging():
        >>>     logger.warning('Warning message!')  # not flushed yet
        >>>     some_long_process()
        >>>
        Warning message!  # the warning is flushed here
    N)rr%r&rqre)r(rnrrrÚpending_loggingùs

rrT)Úskipr#ccsjtjtƒ}|dkrdVnLz.tƒ}x|jD]}|jjd|ƒq*WdVWdx|jD]}|j|ƒqRWXdS)z9Context manager to skip WarningIsErrorFilter temporarily.FNr)rr%r&ÚDisableWarningIsErrorFilterrjÚfiltersÚinsertÚremoveFilter)rsr(ZdisablerrorrrÚskip_warningiserror
s

rx)r6r#ccs°tjtƒ}d}x(|jD]}t|tƒr|}PqWdVdSd}x|jD]}t|tƒrD|}PqDW|r‚z|j}||_dVWd||_Xn*t|ƒ}z|j	|ƒdVWd|j
|ƒXdS)zþContext manager to prepend prefix to all warning log records temporarily.

    For example::

        >>> with prefixed_warnings("prefix:"):
        >>>     logger.warning('Warning message!')  # => prefix: Warning message!

    .. versionadded:: 2.0
    N)rr%r&rjr/rXruÚMessagePrefixFilterr6Ú	addFilterrw)r6r(Úwarning_handlerroZ
prefix_filterZ_filterÚpreviousrrrÚprefixed_warnings!s0






r}c@s0eZdZddœdd„Zeeddœdd„ƒZdS)ÚLogCollectorN)r#cCs
g|_dS)N)Úlogs)r7rrrraPszLogCollector.__init__c	cs&tƒ}dV|jƒ|_WdQRXdS)N)rrrfr)r7rnrrrÚcollectSszLogCollector.collect)NNN)r:r;r<rarrr€rrrrr~Osr~c@s"eZdZdZejedœdd„ZdS)Ú
InfoFilterz"Filter error and warning messages.)rUr#cCs|jtjkrdSdSdS)NTF)rBrr)r7rUrrrÚfilter^szInfoFilter.filterN)r:r;r<r=rrWrgr‚rrrrr[sr)rDrEÚsuppress_warningsr#cCs\|dkrdSxJ|D]B}d|kr0|jddƒ\}}n
|d}}||kr|d|dfkrdSqWdS)z/Check whether the warning is suppressed or not.NFr$rÚ*T)Úsplit)rDrErƒZwarning_typeÚtargetZ	subtargetrrrÚis_suppressed_warninges

r‡cs:eZdZdZdddœ‡fdd„Zejedœdd	„Z‡Z	S)
ÚWarningSuppressorz#Filter logs by `suppress_warnings`.rN)Úappr#cs||_tƒjƒdS)N)r‰r5ra)r7r‰)r9rrra{szWarningSuppressor.__init__)rUr#cCsht|ddƒ}t|ddƒ}y|jjj}Wntk
r>g}YnXt|||ƒrPdS|jjd7_dSdS)NrDrEFrT)r.r‰ÚconfigrƒÚAttributeErrorr‡Z
_warncount)r7rUrDrErƒrrrr‚s
zWarningSuppressor.filter)
r:r;r<r=rarrWrgr‚r?rr)r9rrˆxsrˆcs:eZdZdZdddœ‡fdd„Zejedœdd	„Z‡Z	S)
ÚWarningIsErrorFilterz#Raise exception if warning emitted.rN)r‰r#cs||_tƒjƒdS)N)r‰r5ra)r7r‰)r9rrra“szWarningIsErrorFilter.__init__)rUr#cCsœt|ddƒrdS|jjr”t|ddƒ}y|j|j}Wnttfk
rR|j}YnX|rnt|dt|ƒƒ}nt|ƒ}|j	dk	rŽ||j	d‚q˜|‚ndSdS)NÚskip_warningsiserrorFTr*r4ú:r)
r.r‰Zwarningiserrorr,r-Ú	TypeErrorÚ
ValueErrorrr>Úexc_info)r7rUr*r8Úexcrrrr‚—s
zWarningIsErrorFilter.filter)
r:r;r<r=rarrWrgr‚r?rr)r9rrŒsrŒc@s"eZdZdZejedœdd„ZdS)rtz6Disable WarningIsErrorFilter if this filter installed.)rUr#cCs
d|_dS)NT)r)r7rUrrrr‚±sz"DisableWarningIsErrorFilter.filterN)r:r;r<r=rrWrgr‚rrrrrt®srtcs:eZdZdZeddœ‡fdd„Zejedœdd„Z	‡Z
S)	ryz"Prepend prefix to all log records.N)r6r#cs||_tƒjƒdS)N)r6r5ra)r7r6)r9rrra¹szMessagePrefixFilter.__init__)rUr#cCs|jr|jd|j|_dS)Nú T)r6r,)r7rUrrrr‚½szMessagePrefixFilter.filter)r:r;r<r=r>rarrWrgr‚r?rr)r9rry¶srycs<eZdZdZd
eddœ‡fdd„
Zejedœdd	„Z	‡Z
S)Ú
OnceFilterzShow the message only once.r4N)r"r#cstƒj|ƒi|_dS)N)r5raÚmessages)r7r")r9rrraÆszOnceFilter.__init__)rUr#cCsFt|ddƒ}|sdS|jj|jgƒ}|j|kr2dS|j|jƒdSdS)NrHr4TF)r.r•rPr,r-rl)r7rUrHÚparamsrrrr‚Ês
zOnceFilter.filter)r4)r:r;r<r=r>rarrWrgr‚r?rr)r9rr”Ãsr”csDeZdZUdZeejdddœ‡fdd„Ze	e
dœdd	„Z‡ZS)
ÚSphinxLogRecordTranslatorz‚Converts a log record to one Sphinx expects

    * Make a instance of SphinxLogRecord
    * docname to path if location given
    rN)r‰r#cs||_tƒjƒdS)N)r‰r5ra)r7r‰)r9rrraßsz"SphinxLogRecordTranslator.__init__)rUr#cCs²t|tjƒr|j|_t|ddƒ}t|tƒrv|\}}|rT|rTd|jjj	|ƒ|f|_
q®|rnd|jjj	|ƒ|_
q®d|_
n8t|tjƒrŽt
|ƒ|_
n |r®d|kr®d|jjj	|ƒ|_
dS)Nr*z%s:%sz%srŽT)r/rrWÚLogRecordClassr9r.Útupler‰ÚenvZdoc2pathr*rrr0)r7rUr*ZdocnameÚlinenorrrr‚ãs
z SphinxLogRecordTranslator.filter)
r:r;r<r=rrrWr˜rarArgr‚r?rr)r9rr—×s

r—c@seZdZdZeZdS)ÚInfoLogRecordTranslatorz/LogRecordTranslator for INFO level log records.N)r:r;r<r=r@r˜rrrrrœùsrœc@seZdZdZeZdS)ÚWarningLogRecordTranslatorz2LogRecordTranslator for WARNING level log records.N)r:r;r<r=rAr˜rrrrrþsr)Únoder#cCs@t|ƒ\}}|r |r d||fS|r,d|S|r8d|SdSdS)Nz%s:%sz%s:z<unknown>:%s)r)ržÚsourceÚlinerrrr0sr0cs&eZdZejedœ‡fdd„Z‡ZS)ÚColorizeFormatter)rUr#csBtƒj|ƒ}t|ddƒ}|dkr,tj|jƒ}|r:t||ƒS|SdS)NrG)r5Úformatr.Ú	COLOR_MAPÚgetrBr)r7rUr8rG)r9rrr¢s
zColorizeFormatter.format)r:r;r<rrWr>r¢r?rr)r9rr¡sr¡c@s>eZdZdZeddœdd„Zeddœdd„Zdd	œd
d„ZdS)ÚSafeEncodingWriterz7Stream writer which ignores UnicodeEncodeError silentlyN)Ústreamr#cCs||_t|ddƒpd|_dS)NÚencodingÚascii)r¦r.r§)r7r¦rrrraszSafeEncodingWriter.__init__)Údatar#cCsHy|jj|ƒWn2tk
rB|jj|j|jdƒj|jƒƒYnXdS)NÚreplace)r¦ÚwriteÚUnicodeEncodeErrorÚencoder§Údecode)r7r©rrrr«"szSafeEncodingWriter.write)r#cCst|jdƒr|jjƒdS)Nrc)Úhasattrr¦rc)r7rrrrc*szSafeEncodingWriter.flush)	r:r;r<r=rrar>r«rcrrrrr¥sr¥c@s2eZdZdZdeddœdd„Zeddœdd	„ZdS)
ÚLastMessagesWriterzBStream writer storing last 10 messages in memory to save trackbackrN)r‰r¦r#cCs
||_dS)N)r‰)r7r‰r¦rrrra1szLastMessagesWriter.__init__)r©r#cCs|jjj|ƒdS)N)r‰Z
messagelogrl)r7r©rrrr«4szLastMessagesWriter.write)r:r;r<r=rrar>r«rrrrr°/sr°r)r‰ÚstatusÚwarningr#cCs4tjtƒ}|jtjƒd|_x |jdd…D]}|j|ƒq,Wtt	|ƒƒ}|j
tƒƒ|j
t|ƒƒ|jt
|jƒ|jtƒƒtt	|ƒƒ}|j
t|ƒƒ|j
t|ƒƒ|j
t|ƒƒ|j
tƒƒ|jtjƒ|jtƒƒtjt||ƒƒ}|j
tƒƒ|jt
|jƒ|jtƒƒ|j|ƒ|j|ƒ|j|ƒdS)zSetup root logger for SphinxFN)rr%r&rirÚ	propagaterjrkrYr¥rzrrœÚ
VERBOSITY_MAPÚ	verbosityÚsetFormatterr¡rXrˆrrŒr”rÚ
StreamHandlerr°rm)r‰r±r²r(roZinfo_handlerr{Zmessagelog_handlerrrrÚsetup8s0


r¸)T)NNN)NNN)Rr=rÚlogging.handlersÚcollectionsrÚ
contextlibrÚtypingrrrrrr	r
rrr
ZdocutilsrZdocutils.nodesrZdocutils.utilsrZ
sphinx.errorsrZsphinx.util.consolerZsphinx.applicationrr&rrMr>rKÚupdaterrrrrr´r£r%rWr2r3r@rAÚ
LoggerAdapterr!r·rXrYrjÚBufferingHandlerr_ÚHandlerrprqrrrgrxr}r~ÚFilterrr‡rˆrŒrtryr”r—rœrr0Ú	Formatterr¡r¥r°r¸rrrrÚ<module>s†0-

"