o
    _c                     @   s   d Z ddlmZ ddlZddlZddlmZ zddlmZm	Z	m
Z
mZ W n	 ey-   Y nw ddlmZ G dd	 d	ejZd
d Zdd ZedefddZedd ZdS )zB
Helper functionality for interoperability with stdlib `logging`.
    )absolute_importN)contextmanager)IteratorListOptionalType   )tqdmc                       s(   e Zd Zef fdd	Zdd Z  ZS )_TqdmLoggingHandlerc                    s   t t|   || _d S N)superr
   __init__
tqdm_class)selfr   	__class__ ?/usr/local/lib/python3.10/dist-packages/tqdm/contrib/logging.pyr      s   
z_TqdmLoggingHandler.__init__c              	   C   sT   z|  |}| jj|| jd |   W d S  ttfy       | | Y d S )N)file)formatr   writestreamflushKeyboardInterrupt
SystemExithandleError)r   recordmsgr   r   r   emit   s   
z_TqdmLoggingHandler.emit)__name__
__module____qualname__std_tqdmr   r   __classcell__r   r   r   r   r
      s    r
   c                 C   s   t | tjo| jtjtjhv S r   )
isinstanceloggingStreamHandlerr   sysstdoutstderr)handlerr   r   r   _is_console_logging_handler%   s   r+   c                 C   s   | D ]
}t |r|  S qd S r   r+   )handlersr*   r   r   r   (_get_first_found_console_logging_handler*   s
   r.   c              	   c   s    | du r	t jg} dd | D }z;| D ]%}t|}t|j}|dur,||j |j|_dd |jD |g |_qdV  W t| |D ]\}}||_qBdS t| |D ]\}}||_qQw )a  
    Context manager redirecting console logging to `tqdm.write()`, leaving
    other logging handlers (e.g. log files) unaffected.

    Parameters
    ----------
    loggers  : list, optional
      Which handlers to redirect (default: [logging.root]).
    tqdm_class  : optional

    Example
    -------
    ```python
    import logging
    from tqdm import trange
    from tqdm.contrib.logging import logging_redirect_tqdm

    LOG = logging.getLogger(__name__)

    if __name__ == '__main__':
        logging.basicConfig(level=logging.INFO)
        with logging_redirect_tqdm():
            for i in trange(9):
                if i == 4:
                    LOG.info("console logging redirected to `tqdm.write()`")
        # logging restored
    ```
    Nc                 S   s   g | ]}|j qS r   )r-   ).0loggerr   r   r   
<listcomp>T   s    z)logging_redirect_tqdm.<locals>.<listcomp>c                 S   s   g | ]}t |s|qS r   r,   )r/   r*   r   r   r   r1   \   s    )	r%   rootr
   r.   r-   setFormatter	formatterr   zip)loggersr   Zoriginal_handlers_listr0   Ztqdm_handlerZorig_handlerZoriginal_handlersr   r   r   logging_redirect_tqdm0   s,   "
r7   c               	   o   s    |  }|dd}|dt}|| i |*}t||d |V  W d   n1 s-w   Y  W d   dS W d   dS 1 sEw   Y  dS )ap  
    Convenience shortcut for:
    ```python
    with tqdm_class(*args, **tqdm_kwargs) as pbar:
        with logging_redirect_tqdm(loggers=loggers, tqdm_class=tqdm_class):
            yield pbar
    ```

    Parameters
    ----------
    tqdm_class  : optional, (default: tqdm.std.tqdm).
    loggers  : optional, list.
    **tqdm_kwargs  : passed to `tqdm_class`.
    r6   Nr   )r6   r   )copypopr"   r7   )argskwargsZtqdm_kwargsr6   r   Zpbarr   r   r   tqdm_logging_redirecte   s   "r<   )__doc__
__future__r   r%   r'   
contextlibr   typingr   r   r   r   ImportErrorZstdr	   r"   r&   r
   r+   r.   r7   r<   r   r   r   r   <module>   s(    4