U
    9%e                     @   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k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   Y/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/huggingface_hub/utils/tqdm.pydisable_progress_barsN   s    r   c                   C   s   t dk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                       s4   e Zd ZdZ fddZedd 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|| d S )NTdisable)r   super__init__)selfargskwargs	__class__r   r   r      s    ztqdm.__init__N)attrr   c                    s4   zt  | W n tk
r.   |dkr* Y nX dS )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   z   s   r   )pathr   c              	   #   s   t | trt| } | dZ}|  j}tdd|d| jd|j dt	t
 td fdd	}||_|V    W 5 Q R X 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-   )
isinstancer$   r   openstatst_sizer   namereadr   intbytesclose)r&   f
total_sizer3   r   r2   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   r$   BufferedReaderr?   r   r   r   r   <module>   s   )

