a
    º
þd¶  ã                   @   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p`d	aeed
< ddœdd„Zddœdd„Zedœdd„ZG dd„ deƒZee
eef eej dœ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   s   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   úc/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/huggingface_hub/utils/tqdm.pyÚdisable_progress_barsN   s    ÿr   c                   C   s   t du rt d¡ dS dadS )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_   s    ÿ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_disabledp   s    r   c                       s    e Zd ZdZ‡ 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__)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ú__classcell__r   r   r   r   r   z   s   r   )Úpathr   c                 #   s”   t | tƒrt| ƒ} |  d¡d}|  ¡ j}tdd|d| jd‰|j‰ dt	t
 tdœ‡ ‡fdd	„}||_|V  ˆ ¡  W d
  ƒ n1 s†0    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ÚinitialÚ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(   )Ú
isinstanceÚstrr   ÚopenÚstatÚst_sizer   ÚnameÚreadr   ÚintÚbytesÚclose)r!   ÚfZ
total_sizer.   r   r-   r   Útqdm_stream_file‡   s     

ûr:   )r   Úior   Ú
contextlibr   Úpathlibr   Útypingr   r   r   Z	tqdm.autor   Zold_tqdmÚ	constantsr	   r
   ÚboolÚ__annotations__r   r   r   r0   ÚBufferedReaderr:   r   r   r   r   Ú<module>   s   )

