3
d                 @   s   d Z ddlmZ ddlZddlZddlmZ yddlmZm	Z	m
Z
mZ W n ek
r\   Y nX 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| j  || _d S )N)superr
   __init__
tqdm_class)selfr   )	__class__ 4/tmp/pip-build-v9q4h5k9/tqdm/tqdm/contrib/logging.pyr      s    z_TqdmLoggingHandler.__init__c             C   s\   y(| j |}| jj|| jd | j  W n. ttfk
rB    Y n   | j| Y nX 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kS )N)
isinstanceloggingStreamHandlerr   sysstdoutstderr)handlerr   r   r   _is_console_logging_handler%   s    r)   c             C   s   x| D ]}t |r|S qW d S )N)r)   )handlersr(   r   r   r   (_get_first_found_console_logging_handler*   s    
r+   c             c   s   | dkrt jg} dd | D }z^xR| D ]J}t|}t|j}|dk	rX|j|j |j|_dd |jD |g |_q&W dV  W dxt| |D ]\}}||_qW X 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   s    z)logging_redirect_tqdm.<locals>.<listcomp>c             S   s   g | ]}t |s|qS r   )r)   )r,   r(   r   r   r   r.   ]   s    )	r#   rootr
   r+   r*   setFormatter	formatterr   zip)loggersr   Zoriginal_handlers_listr-   Ztqdm_handlerZorig_handlerZoriginal_handlersr   r   r   logging_redirect_tqdm0   s    "


r4   c              o   sX   |j  }|jdd}|jdt}|| |$}t||d |V  W dQ R X W dQ R X 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`.
    r3   Nr   )r3   r   )copypopr    r4   )argskwargsZtqdm_kwargsr3   r   Zpbarr   r   r   tqdm_logging_redirecte   s    r9   )__doc__
__future__r   r#   r%   
contextlibr   Ztypingr   r   r   r   ImportErrorZstdr	   r    r$   r
   r)   r+   r4   r9   r   r   r   r   <module>   s    2