U
    ”Ê¹j¯)  ã                   @   sT  U d Z ddlZddlZddlZddlZddlmZmZ ddlm	Z	 ddl
mZmZmZmZmZ ddlmZ ddlmZ i Zeeef ed	< dee dd
œdd„Zd ee dd
œdd„Zd!ee ed
œdd„Zeee dœdd„ZG dd„ deƒZeee	ef eej dœdd„ƒZdddddddœeeee eeeee ee ee dœ	dd„Z dS )"u	  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)):
    ...    pass

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

    >>> are_progress_bars_disabled()
    True

    # Re-enable progress bars globally
    >>> enable_progress_bars()

    # Progress bar will be shown !
    >>> for _ in tqdm(range(5)):
    ...   pass
    100%|â–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆ| 5/5 [00:00<00:00, 117817.53it/s]
    ```

Group-based control:
    ```python
    # Disable progress bars for a specific group
    >>> disable_progress_bars("peft.foo")

    # Check state of different groups
    >>> assert not are_progress_bars_disabled("peft"))
    >>> assert not are_progress_bars_disabled("peft.something")
    >>> assert are_progress_bars_disabled("peft.foo"))
    >>> assert are_progress_bars_disabled("peft.foo.bar"))

    # Enable progress bars for a subgroup
    >>> enable_progress_bars("peft.foo.bar")

    # Check if enabling a subgroup affects the parent group
    >>> assert are_progress_bars_disabled("peft.foo"))
    >>> assert not are_progress_bars_disabled("peft.foo.bar"))

    # No progress bar for `name="peft.foo"`
    >>> for _ in tqdm(range(5), name="peft.foo"):
    ...     pass

    # Progress bar will be shown for `name="peft.foo.bar"`
    >>> for _ in tqdm(range(5), name="peft.foo.bar"):
    ...     pass
    100%|â–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆâ–ˆ| 5/5 [00:00<00:00, 117817.53it/s]

    ```
é    N)ÚcontextmanagerÚnullcontext)ÚPath)ÚContextManagerÚDictÚIteratorÚOptionalÚUnion)Útqdmé   )ÚHF_HUB_DISABLE_PROGRESS_BARSÚprogress_bar_states)ÚnameÚreturnc                    s^   t dkrt d¡ dS ˆ dkr0t ¡  dtd< n*‡ fdd„tD ƒ}|D ]
}t|= qFdtˆ < dS )a4  
    Disable progress bars either globally or for a specified group.

    This function updates the state of progress bars based on a group name.
    If no group name is provided, all progress bars are disabled. The operation
    respects the `HF_HUB_DISABLE_PROGRESS_BARS` environment variable's setting.

    Args:
        name (`str`, *optional*):
            The name of the group for which to disable the progress bars. If None,
            progress bars are disabled globally.

    Raises:
        Warning: If the environment variable precludes changes.
    FzlCannot disable progress bars: environment variable `HF_HUB_DISABLE_PROGRESS_BARS=0` is set and has priority.NÚ_globalc                    s    g | ]}|  ˆ › d �¡r|‘qS ©Ú.©Ú
startswith©Ú.0Úkey©r   © úY/var/www/html/bwave/python/venv/lib/python3.8/site-packages/huggingface_hub/utils/tqdm.pyÚ
<listcomp>†   s      z)disable_progress_bars.<locals>.<listcomp>©r   ÚwarningsÚwarnr   Úclear©r   Zkeys_to_remover   r   r   r   Údisable_progress_barsl   s    ÿ
r!   c                    s^   t dkrt d¡ dS ˆ dkr0t ¡  dtd< n*‡ fdd„tD ƒ}|D ]
}t|= qFdtˆ < dS )a  
    Enable progress bars either globally or for a specified group.

    This function sets the progress bars to enabled for the specified group or globally
    if no group is specified. The operation is subject to the `HF_HUB_DISABLE_PROGRESS_BARS`
    environment setting.

    Args:
        name (`str`, *optional*):
            The name of the group for which to enable the progress bars. If None,
            progress bars are enabled globally.

    Raises:
        Warning: If the environment variable precludes changes.
    TzkCannot enable progress bars: environment variable `HF_HUB_DISABLE_PROGRESS_BARS=1` is set and has priority.Nr   c                    s    g | ]}|  ˆ › d �¡r|‘qS r   r   r   r   r   r   r   ¦   s      z(enable_progress_bars.<locals>.<listcomp>r   r    r   r   r   Úenable_progress_barsŒ   s    ÿ
r"   c                 C   s`   t dkrdS | dkr"t dd¡ S | rR| tkr8t|   S d |  d¡dd… ¡} q"t dd¡ S )aæ  
    Check if progress bars are disabled globally or for a specific group.

    This function returns whether progress bars are disabled for a given group or globally.
    It checks the `HF_HUB_DISABLE_PROGRESS_BARS` environment variable first, then the programmatic
    settings.

    Args:
        name (`str`, *optional*):
            The group name to check; if None, checks the global setting.

    Returns:
        `bool`: True if progress bars are disabled, False otherwise.
    TNr   r   éÿÿÿÿ)r   r   ÚgetÚjoinÚsplitr   r   r   r   Úare_progress_bars_disabled¬   s    
r'   )Ú	log_levelr   c                 C   s$   | t jkrdS t d¡dkr dS dS )zí
    Determine if tqdm progress bars should be disabled based on logging level and environment settings.

    see https://github.com/huggingface/huggingface_hub/pull/2000 and https://github.com/huggingface/huggingface_hub/pull/2698.
    TZTQDM_POSITIONz-1FN)ÚloggingÚNOTSETÚosÚgetenv©r(   r   r   r   Úis_tqdm_disabledÉ   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.   |  dd ¡}t|ƒrd|d< tƒ j||Ž d S )Nr   TÚdisable)Úpopr'   ÚsuperÚ__init__)ÚselfÚargsÚkwargsr   ©Ú	__class__r   r   r2   Ý   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)r1   Ú__delattr__ÚAttributeError)r3   r8   r6   r   r   r:   ã   s
    ztqdm.__delattr__)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r2   Ústrr:   Ú__classcell__r   r   r6   r   r
   Ö   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   )ÚunitÚ
unit_scaleÚtotalÚinitialÚdescr#   )Úsizer   c                    s   ˆ | ƒ}ˆ  t|ƒ¡ |S )N)ÚupdateÚlen)rJ   Údata©Zf_readZpbarr   r   Ú_inner_read  s    z%tqdm_stream_file.<locals>._inner_readN)r#   )Ú
isinstancer@   r   ÚopenÚstatÚst_sizer
   r   Úreadr   ÚintÚbytesÚclose)rB   ÚfZ
total_sizerO   r   rN   r   Útqdm_stream_fileì   s     

ûrY   rD   T)rG   rH   rE   rF   r   Ú	_tqdm_bar)	rI   r(   rG   rH   rE   rF   r   rZ   r   c              	   C   s,   |d k	rt |ƒS t||||| t|d�|d�S )Nr-   )rE   rF   rG   rH   rI   r/   r   )r   r
   r.   )rI   r(   rG   rH   rE   rF   r   rZ   r   r   r   Ú_get_progress_bar_context  s    ùr[   )N)N)N)!r?   Úior)   r+   r   Ú
contextlibr   r   Úpathlibr   Útypingr   r   r   r   r	   Z	tqdm.autor
   Zold_tqdmÚ	constantsr   r   r@   ÚboolÚ__annotations__r!   r"   r'   rU   r.   ÚBufferedReaderrY   r[   r   r   r   r   Ú<module>   sD   D   1÷ö