o
    Zh                     @   s   U d Z ddlZddlZddlmZ ddlmZ ddlmZm	Z	m
Z
 ddlmZ ddlmZ ep0d	aeed
< dddZdddZdefddZG dd deZede
eef deej fddZdS )a  Utility helpers to handle progress bars in `huggingface_hub`.

Example:
    1. Use `huggingface_hub.utils.tqdm` as you would use `tqdm.tqdm` or `tqdm.auto.tqdm`.
    2. To disable progress bars, either use `disable_progress_bars()` helper or set the
       environment variable `HF_HUB_DISABLE_PROGRESS_BARS` to 1.
    3. To re-enable progress bars, use `enable_progress_bars()`.
    4. To check whether progress bars are disabled, use `are_progress_bars_disabled()`.

NOTE: Environment variable `HF_HUB_DISABLE_PROGRESS_BARS` has the priority.

Example:
    ```py
    from huggingface_hub.utils import (
        are_progress_bars_disabled,
        disable_progress_bars,
        enable_progress_bars,
        tqdm,
    )

    # Disable progress bars globally
    disable_progress_bars()

    # Use as normal `tqdm`
    for _ in tqdm(range(5)):
       do_something()

    # Still not showing progress bars, as `disable=False` is overwritten to `True`.
    for _ in tqdm(range(5), disable=False):
       do_something()

    are_progress_bars_disabled() # True

    # Re-enable progress bars globally
    enable_progress_bars()

    # Progress bar will be shown !
    for _ in tqdm(range(5)):
       do_something()
    ```
    N)contextmanager)Path)IteratorOptionalUnion)tqdm   )HF_HUB_DISABLE_PROGRESS_BARSF_hf_hub_progress_bars_disabledreturnc                   C      t du rtd dS dadS )z
    Disable globally progress bars used in `huggingface_hub` except if `HF_HUB_DISABLE_PROGRESS_BARS` environment
    variable has been set.

    Use [`~utils.enable_progress_bars`] to re-enable them.
    FzlCannot disable progress bars: environment variable `HF_HUB_DISABLE_PROGRESS_BARS=0` is set and has priority.NTr	   warningswarnr
    r   r   Q/var/www/html/lang_env/lib/python3.10/site-packages/huggingface_hub/utils/tqdm.pydisable_progress_barsO      r   c                   C   r   )z
    Enable globally progress bars used in `huggingface_hub` except if `HF_HUB_DISABLE_PROGRESS_BARS` environment
    variable has been set.

    Use [`~utils.disable_progress_bars`] to disable them.
    TzkCannot enable progress bars: environment variable `HF_HUB_DISABLE_PROGRESS_BARS=1` is set and has priority.NFr   r   r   r   r   enable_progress_bars`   r   r   c                   C   s   t S )a$  Return whether progress bars are globally disabled or not.

    Progress bars used in `huggingface_hub` can be enable or disabled globally using [`~utils.enable_progress_bars`]
    and [`~utils.disable_progress_bars`] or by setting `HF_HUB_DISABLE_PROGRESS_BARS` as environment variable.
    )r
   r   r   r   r   are_progress_bars_disabledq   s   r   c                       s6   e Zd ZdZ fddZdeddf fddZ  ZS )	r   z
    Class to override `disable` argument in case progress bars are globally disabled.

    Taken from https://github.com/tqdm/tqdm/issues/619#issuecomment-619639324.
    c                    s$   t  rd|d< t j|i | d S )NTdisable)r   super__init__)selfargskwargs	__class__r   r   r      s   ztqdm.__init__attrr   Nc                    s2   z	t  | W dS  ty   |dkr Y dS w )zBFix for https://github.com/huggingface/huggingface_hub/issues/1603_lockN)r   __delattr__AttributeError)r   r   r   r   r   r       s   ztqdm.__delattr__)__name__
__module____qualname____doc__r   strr    __classcell__r   r   r   r   r   {   s    r   pathc                 #   s    t | tr
t| } | d4}|  j}tdd|d| jd|j ddt	t
 dtf fd	d
}||_|V    W d   dS 1 sFw   Y  dS )uT  
    Open a file as binary and wrap the `read` method to display a progress bar when it's streamed.

    First implemented in `transformers` in 2019 but removed when switched to git-lfs. Used in `huggingface_hub` to show
    progress bar when uploading an LFS file to the Hub. See github.com/huggingface/transformers/pull/2078#discussion_r354739608
    for implementation details.

    Note: currently implementation handles only files stored on disk as it is the most common use case. Could be
          extended to stream any `BinaryIO` object but we might have to debug some corner cases.

    Example:
    ```py
    >>> with tqdm_stream_file("config.json") as f:
    >>>     requests.put(url, data=f)
    config.json: 100%|█████████████████████████| 8.19k/8.19k [00:02<00:00, 3.72kB/s]
    ```
    rbBTr   )unitZ
unit_scaletotalinitialZdescsizer   c                    s    | } t| |S )N)updatelen)r/   dataZf_readZpbarr   r   _inner_read   s   z%tqdm_stream_file.<locals>._inner_readN)r.   )
isinstancer&   r   openstatst_sizer   namereadr   intbytesclose)r(   f
total_sizer4   r   r3   r   tqdm_stream_file   s$   


"r@   )r   N)r%   ior   
contextlibr   pathlibr   typingr   r   r   Z	tqdm.autor   Zold_tqdm	constantsr	   r
   bool__annotations__r   r   r   r&   BufferedReaderr@   r   r   r   r   <module>   s   *



&