a
    -=ic                     @   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yZ   Y n0 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__ e/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/tqdm/contrib/logging.pyr      s    z_TqdmLoggingHandler.__init__c              	   C   sZ   z(|  |}| jj|| jd |   W n, ttfy@    Y n   | | Y n0 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 }zt| D ]J}t|}t|j}|durV||j |j|_dd |jD |g |_q$dV  W t| |D ]\}}||_qnt| |D ]\}}||_q0 dS )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       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(    "

r8   c               	   o   s   |  }|dd}|dt}|| i |B}t||d |V  W d   n1 sX0    Y  W d   n1 sv0    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`.
    r7   Nr   )r7   r   )copypopr"   r8   )argskwargsZtqdm_kwargsr7   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.   r8   r=   r   r   r   r   <module>   s$   4