o
    Ni'L                  
   @  s  U d Z ddlmZ ddlZddlZddlmZ ddlmZm	Z	 ddl
mZmZmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ereddlmZmZmZ ddl
mZ ddlmZ ddlm Z  dZ!dZ"edd ej#ej#ej$ej%ej&e"ej'dZ(de)d< edd ej&e"ej'dZ*de)d< edd ej$dej%dej'diZ+de)d < dpd%d&Z,dqd*d+Z-G d,d- d-ej.Z/G d.d/ d/e/Z0G d0d1 d1e/Z1G d2d$ d$ej2Z3G d3d4 d4ej4Z5G d5d6 d6ej4Z6G d7d8 d8ej7j8Z9edrd:d;Z:edsd=d>Z;edsd?d@Z<e	Z=edtdCdDZ>G dEdF dFZ?G dGdH dHej@ZAG dIdJ dJej@ZBdudPdQZCG dRdS dSej@ZDG dTdU dUej@ZEG dVdW dWej@ZFG dXdY dYej@ZGG dZd[ d[eGZHG d\d] d]eGZIdvdadbZJG dcdd ddejKZLG dedf dfZMG dgdh dhZNdwdndoZOdS )xz%Logging utility functions for Sphinx.    )annotationsN)defaultdict)contextmanagernullcontext)IOTYPE_CHECKINGAny)nodes)get_source_line)SphinxWarning)colorize)abspath)IteratorSequenceSet)NoReturn)Node)Sphinxsphinx   c                   C     t jS N)loggingWARNING r   r   G/home/ubuntu/.local/lib/python3.10/site-packages/sphinx/util/logging.py<lambda>       r   )CRITICALSEVEREERRORr   INFOVERBOSEDEBUGzdefaultdict[str, int]LEVEL_NAMESc                   C  r   r   )r   NOTSETr   r   r   r   r   ,   r   )r         zdefaultdict[int, int]VERBOSITY_MAPc                   C     dS )Nbluer   r   r   r   r   r   5   s    darkredreddarkgrayzdefaultdict[int, str]	COLOR_MAPnamestrreturnSphinxLoggerAdapterc                 C  s"   t td |  }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disabledr2   )r/   loggerr   r   r   r4   >   s   
r4   recordslist[logging.LogRecord]Nonec                 C  s@   | D ]}|  |_d|_t|dd}t|tjrt||_qdS )zConvert LogRecord serializable.r   locationN)	
getMessagemsgargsgetattr
isinstancer	   r   get_node_locationr;   )r8   rr;   r   r   r   convert_serializableT   s   

rC   c                      s4   e Zd ZU dZdZdZded< d
 fdd	Z  ZS )SphinxLogRecordz$Log record class supporting location Nr   r;   r1   r0   c                   sJ   t   }t| dd }|r| d| j | }|S | j|vr#| j| }|S )Nr;   z: )superr<   r?   prefix)selfmessager;   	__class__r   r   r<   f   s   


zSphinxLogRecord.getMessager1   r0   )	__name__
__module____qualname____doc__rG   r;   __annotations__r<   __classcell__r   r   rJ   r   rD   `   s
   
 rD   c                   @  s   e Zd ZdZdZdS )SphinxInfoLogRecordz)Info log record class supporting locationrE   N)rM   rN   rO   rP   rG   r   r   r   r   rS   q       rS   c                   @  s   e Zd ZdZedddZdS )SphinxWarningLogRecordz,Warning log record class supporting locationr1   r0   c                 C  s$   | j tjkrdS | j tjkrdS dS )Nz
CRITICAL: zERROR: z	WARNING: )levelnor   r   r    rH   r   r   r   rG   z   s
   zSphinxWarningLogRecord.prefixNrL   )rM   rN   rO   rP   propertyrG   r   r   r   r   rU   w   s    rU   c                      sf   e Zd ZdZg dZd( fddZd)ddZd*ddZd+ddZdddddddd, fd&d'Z	  Z
S )-r2   z9LoggerAdapter allowing ``type`` and ``subtype`` keywords.typesubtyper;   nonlcoloroncelevel	int | strr=   r0   r>   r   kwargsr1   r:   c                   sR   t |trt j||g|R i | d S t| }t j||g|R i | d S r   )r@   intrF   logr$   )rH   r_   r=   r>   ra   rV   rJ   r   r   rc      s   
  zSphinxLoggerAdapter.logc                 O  s   | j t|g|R i | d S r   )rc   r"   )rH   r=   r>   ra   r   r   r   verbose   s   zSphinxLoggerAdapter.verbosedicttuple[str, dict]c                 C  s6   | di }| jD ]}||v r||||< q	||fS )Nextra)
setdefaultKEYWORDSpop)rH   r=   ra   rg   keywordr   r   r   process   s   
zSphinxLoggerAdapter.processrecordlogging.LogRecordc                 C  s   | j | d S r   )r7   handlerH   rm   r   r   r   ro         zSphinxLoggerAdapter.handleNTFobjectrZ   
None | strr[   r;   1None | str | tuple[str | None, int | None] | Noder\   boolr]   
str | Noner^   c          
   	     s(   t  j|g|R ||||||d|	S )aj  Log a sphinx warning.

        It is recommended to include a ``type`` and ``subtype`` for warnings as
        these can be displayed to the user using :confval:`show_warning_types`
        and used in :confval:`suppress_warnings` to suppress specific warnings.

        It is also recommended to specify a ``location`` whenever possible
        to help users in correcting the warning.

        :param msg: The message, which may contain placeholders for ``args``.
        :param args: The arguments to substitute into ``msg``.
        :param type: The type of the warning.
        :param subtype: The subtype of the warning.
        :param location: The source location of the warning's origin,
            which can be a string (the ``docname`` or ``docname:lineno``),
            a tuple of ``(docname, lineno)``,
            or the docutils node object.
        :param nonl: Whether to append a new line terminator to the message.
        :param color: A color code for the message.
        :param once: Do not log this warning,
            if a previous warning already has same ``msg``, ``args`` and ``once=True``.
        rY   )rF   warning)
rH   r=   rZ   r[   r;   r\   r]   r^   r>   ra   rJ   r   r   rw      s   "	zSphinxLoggerAdapter.warning)
r_   r`   r=   r0   r>   r   ra   r   r1   r:   )r=   r0   r>   r   ra   r   r1   r:   )r=   r0   ra   re   r1   rf   rm   rn   r1   r:   )r=   rr   r>   rr   rZ   rs   r[   rs   r;   rt   r\   ru   r]   rv   r^   ru   ra   r   r1   r:   )rM   rN   rO   rP   ri   rc   rd   rl   ro   rw   rR   r   r   rJ   r   r2      s    
	

c                   @  s   e Zd ZdZdS )WarningStreamHandlerzStreamHandler for warnings.N)rM   rN   rO   rP   r   r   r   r   ry      s    ry   c                      s"   e Zd ZdZd fddZ  ZS )	NewLineStreamHandlerzAStreamHandler which switches line terminator by record.nonl flag.rm   rn   r1   r:   c                   sL   z|    t|ddrd| _t | W d| _|   d S d| _|   w )Nr\   FrE   
)acquirer?   
terminatorrF   emitreleaserp   rJ   r   r   r~      s   
zNewLineStreamHandler.emitrx   )rM   rN   rO   rP   r~   rR   r   r   rJ   r   rz      s    rz   c                      sT   e Zd ZU dZded< d fddZdddZdddZdddZdddZ	  Z
S )MemoryHandlerzHandler buffering all logs.r9   bufferr1   r:   c                   s   t  d d S )N)rF   __init__rW   rJ   r   r   r      rq   zMemoryHandler.__init__rm   rn   ru   c                 C  r)   )NFr   rp   r   r   r   shouldFlush   s   zMemoryHandler.shouldFlushc                 C  s   d S r   r   rW   r   r   r   flush   s   zMemoryHandler.flushr7   logging.Loggerc                 C  s>   |    z| jD ]}|| qg | _W |   d S |   w r   )r|   r   ro   r   )rH   r7   rm   r   r   r   flushTo   s   
zMemoryHandler.flushToc                 C  s   | j g }| _ |S r   )r   )rH   r   r   r   r   clear   s   zMemoryHandler.clearr1   r:   rm   rn   r1   ru   )r7   r   r1   r:   )r1   r9   )rM   rN   rO   rP   rQ   r   r   r   r   r   rR   r   r   rJ   r   r      s   
 


	r   Iterator[logging.Handler]c               
   c  s    t t} t }|t j z:g }| jdd D ]}t|tr*| 	| |
| q| | |V  W | 	| |D ]}| | q;||  dS | 	| |D ]}| | qQ||  w )zgContext manager to postpone logging warnings temporarily.

    Similar to :func:`pending_logging`.
    N)r   r4   r5   r   setLevelr   handlersr@   ry   removeHandlerappend
addHandlerr   r7   
memhandlerr   handlerr   r   r   pending_warnings  s*   






r   Iterator[MemoryHandler]c               
   c  s    t t} t }z0g }| jdd D ]}| | || q| | |V  W | | |D ]}| | q0dS | | |D ]}| | qAw )zContext manager to suppress logging all logs temporarily.

    For example::

        >>> with suppress_logging():
        >>>     logger.warning('Warning message!')  # suppressed
        >>>     some_long_process()
        >>>
    N)r   r4   r5   r   r   r   r   r   r   r   r   r   suppress_logging  s$   




r   c               	   c  sf    t t} z&t }|V  W d   n1 sw   Y  W ||  dS W ||  dS ||  w )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)r   r4   r5   r   r   )r7   r   r   r   r   pending_logging;  s   
r   rG   Iterator[None]c              	   c  s    t t}d}|jD ]}t|tr|} nqdV  dS d}|jD ]}t|tr,|} nq!|rCz|j}| |_dV  W ||_dS ||_w t| }z|	| dV  W |
| dS |
| w )zContext 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)r   r4   r5   r   r@   ry   filtersMessagePrefixFilterrG   	addFilterremoveFilter)rG   r7   warning_handlerr   prefix_filter_filterpreviousr   r   r   prefixed_warningsR  s6   





r   c                   @  s$   e Zd Zd	ddZed
ddZdS )LogCollectorr1   r:   c                 C  s
   g | _ d S r   )logsrW   r   r   r   r        
zLogCollector.__init__r   c                 c  s>    t  }d V  | | _W d    d S 1 sw   Y  d S r   )r   r   r   )rH   r   r   r   r   collect  s
   "zLogCollector.collectNr   )r1   r   )rM   rN   rO   r   r   r   r   r   r   r   r     s    
r   c                   @     e Zd ZdZd	ddZdS )

InfoFilterz"Filter error and warning messages.rm   rn   r1   ru   c                 C  s   |j tjk S r   )rV   r   r   rp   r   r   r   filter  s   zInfoFilter.filterNr   rM   rN   rO   rP   r   r   r   r   r   r         r   c                   @  r   )
_RaiseOnWarningFilterz(Raise exception if a warning is emitted.rm   rn   r1   r   c              	   C  sn   z|j |j }W n ttfy   |j }Y nw t|dd }r&| d| }|jd ur3t||jd t|)Nr;   rE   :r&   )r=   r>   	TypeError
ValueErrorr?   exc_infor   )rH   rm   rI   r;   r   r   r   r     s   

z_RaiseOnWarningFilter.filterN)rm   rn   r1   r   r   r   r   r   r   r     r   r   warning_typesub_typesuppress_warningsSet[str] | Sequence[str]ru   c                 C  sP   | du s
t |dkrdS t|}| |v rdS |  d|v rdS |  d| |v S )z/Check whether the warning is suppressed or not.Nr   FTz.*r3   )len	frozenset)r   r   r   suppressed_warningsr   r   r   is_suppressed_warning  s   r   c                      ,   e Zd ZdZd fddZdddZ  ZS )WarningSuppressorz#Filter logs by `suppress_warnings`.appr   r1   r:   c                      || _ t   d S r   r   rF   r   rH   r   rJ   r   r   r        zWarningSuppressor.__init__rm   rn   ru   c                 C  sb   t |dd}t |dd}z| jjj}W n ty   d}Y nw t|||r'dS | j jd7  _dS )NrZ   rE   r[   r   Fr&   T)r?   r   configr   AttributeErrorr   
_warncount)rH   rm   rZ   r[   r   r   r   r   r     s   zWarningSuppressor.filterr   r   r1   r:   r   rM   rN   rO   rP   r   r   rR   r   r   rJ   r   r         r   c                      r   )r   z"Prepend prefix to all log records.rG   r0   r1   r:   c                   r   r   )rG   rF   r   )rH   rG   rJ   r   r   r     r   zMessagePrefixFilter.__init__rm   rn   ru   c                 C  s   | j r| j d |j |_dS )N T)rG   r=   rp   r   r   r   r     s   zMessagePrefixFilter.filter)rG   r0   r1   r:   r   r   r   r   rJ   r   r     r   r   c                      s.   e Zd ZdZdd fddZdddZ  ZS )
OnceFilterzShow the message only once.rE   r/   r0   r1   r:   c                   s   t  | i | _d S r   )rF   r   messages)rH   r/   rJ   r   r   r     s   
zOnceFilter.__init__rm   rn   ru   c                 C  sB   t |dd}|s
dS | j|jg }|j|v rdS ||j dS )Nr^   rE   TF)r?   r   rh   r=   r>   r   )rH   rm   r^   paramsr   r   r   r     s   
zOnceFilter.filter)rE   )r/   r0   r1   r:   r   r   r   r   rJ   r   r     s    r   c                      s6   e Zd ZU dZded< d fdd	ZdddZ  ZS )SphinxLogRecordTranslatorzConverts a log record to one Sphinx expects

    * Make a instance of SphinxLogRecord
    * docname to path if location given
    * append warning type/subtype to message if :confval:`show_warning_types` is ``True``
    ztype[logging.LogRecord]LogRecordClassr   r   r1   r:   c                   r   r   r   r   rJ   r   r   r     r   z"SphinxLogRecordTranslator.__init__rm   rU   ru   c                 C  s   t |tjr
| j|_t|dd }t |tr<|\}}|r7|r,| jj	| d| |_
dS | jj	| |_
dS d |_
dS t |tjrIt||_
dS |rXd|vrX| jj	| |_
dS )Nr;   r   T)r@   r   	LogRecordr   rK   r?   tupler   envdoc2pathr;   r	   r   rA   )rH   rm   r;   docnamelinenor   r   r   r     s&   


z SphinxLogRecordTranslator.filterr   rm   rU   r1   ru   )rM   rN   rO   rP   rQ   r   r   rR   r   r   rJ   r   r     s
   
 r   c                   @  s   e Zd ZdZeZdS )InfoLogRecordTranslatorz/LogRecordTranslator for INFO level log records.N)rM   rN   rO   rP   rS   r   r   r   r   r   r     rT   r   c                      s&   e Zd ZdZeZd fddZ  ZS )	WarningLogRecordTranslatorz2LogRecordTranslator for WARNING level log records.rm   rU   r1   ru   c                   s   t  |}z| jjj}W n ty   d}Y nw |rFt|dd }rFt|dd }r;| jd| d| d7  _|S | jd| d7  _|S )NFrZ   rE   r[   z [r3   ])rF   r   r   r   show_warning_typesr   r?   r=   )rH   rm   retr   log_typelog_subtyperJ   r   r   r     s   z!WarningLogRecordTranslator.filterr   )rM   rN   rO   rP   rU   r   r   rR   r   r   rJ   r   r     s    r   noder   rv   c                 C  sJ   t | \}}|r|rt| d| S |rt| dS |r#d| S d S )Nr   z
<unknown>:)r
   r   )r   sourceliner   r   r   rA   +  s   
rA   c                      s   e Zd Zd fddZ  ZS )ColorizeFormatterrm   rn   r1   r0   c                   s>   t  |}t|dd }|d u rt|j}|rt||S |S )Nr]   )rF   formatr?   r.   getrV   r   )rH   rm   rI   r]   rJ   r   r   r   7  s   
zColorizeFormatter.format)rm   rn   r1   r0   )rM   rN   rO   r   rR   r   r   rJ   r   r   6  s    r   c                   @  s.   e Zd ZdZdddZdd
dZdddZdS )SafeEncodingWriterz7Stream writer which ignores UnicodeEncodeError silentlystreamr   r1   r:   c                 C  s   || _ t|ddp
d| _d S )Nencodingascii)r   r?   r   )rH   r   r   r   r   r   F  s   zSafeEncodingWriter.__init__datar0   c              	   C  sF   z	| j | W d S  ty"   | j || jd| j Y d S w )Nreplace)r   writeUnicodeEncodeErrorencoder   decoderH   r   r   r   r   r   J  s   
zSafeEncodingWriter.writec                 C  s   t | jdr| j  d S d S )Nr   )hasattrr   r   rW   r   r   r   r   T  s   zSafeEncodingWriter.flushN)r   r   r1   r:   r   r0   r1   r:   r   )rM   rN   rO   rP   r   r   r   r   r   r   r   r   C  s
    


r   c                   @  s$   e Zd ZdZddd	ZdddZdS )LastMessagesWriterzBStream writer storing last 10 messages in memory to save trackbackr   r   r   r   r1   r:   c                 C  s
   || _ d S r   )r   )rH   r   r   r   r   r   r   \  r   zLastMessagesWriter.__init__r   r0   c                 C  s   | j j| d S r   )r   
messagelogr   r   r   r   r   r   _  s   zLastMessagesWriter.writeN)r   r   r   r   r1   r:   r   )rM   rN   rO   rP   r   r   r   r   r   r   r   Y  s    
r   r   r   statusr   rw   c                 C  s(  t t}|t j d|_|jdd D ]}|| qtt	|}|
t  |
t|  |t| j  |t  tt	|}| jrM|
t  |
t|  |
t|  |
t  |t j |t  t t| |}|
t  |t| j  || || || dS )zSetup root logger for SphinxFN)r   r4   r5   r   r#   	propagater   r   rz   r   r   r   r   r(   	verbositysetFormatterr   ry   _exception_on_warningr   r   r   r   r   StreamHandlerr   r   )r   r   rw   r7   r   info_handlerr   messagelog_handlerr   r   r   setupc  s0   


r   )r/   r0   r1   r2   )r8   r9   r1   r:   )r1   r   )r1   r   )rG   r0   r1   r   )r   r0   r   r0   r   r   r1   ru   )r   r   r1   rv   )r   r   r   r   rw   r   r1   r:   )PrP   
__future__r   r   logging.handlerscollectionsr   
contextlibr   r   typingr   r   r   docutilsr	   docutils.utilsr
   sphinx.errorsr   sphinx.util.consoler   sphinx.util.osutilr   collections.abcr   r   r   r   docutils.nodesr   sphinx.applicationr   r5   r"   r   r    r   r!   r#   r$   rQ   r(   r.   r4   rC   r   rD   rS   rU   LoggerAdapterr2   r   ry   rz   r   BufferingHandlerr   r   r   r   skip_warningiserrorr   r   Filterr   r   r   r   r   r   r   r   r   rA   	Formatterr   r   r   r   r   r   r   r   <module>   s    	


K-
%

