o
    oh                     @   s   d 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`.
    N)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    || _d S N)super__init__
tqdm_class)selfr   	__class__ h/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-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>R   s    z)logging_redirect_tqdm.<locals>.<listcomp>c                 S   s   g | ]}t |s|qS r   r+   )r.   r)   r   r   r   r0   Z   s    )	r$   rootr	   r-   r,   setFormatter	formatterr   zip)loggersr   original_handlers_listr/   tqdm_handlerorig_handleroriginal_handlersr   r   r   logging_redirect_tqdm.   s,   "
r:   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`.
    r5   Nr   )r5   r   )copypopr!   r:   )argskwargstqdm_kwargsr5   r   pbarr   r   r   tqdm_logging_redirectc   s   "rA   )__doc__r$   r&   
contextlibr   typingr   r   r   r   ImportErrorstdr   r!   r%   r	   r*   r-   r:   rA   r   r   r   r   <module>   s&    4