
    i&4                     V   U d Z ddlZddlZddlZddlZddlZddlmZ ddlm	Z	m
Z
 ddlmZ ddlmZ ddlmZ dd	lmZ i Zeeef         ed
<    G d d          Zd'dedz  ddfdZd'dedz  defdZdededz  fdZ G d de          Ze                     ej                                G d d          Z e	deez  deej!                 fd            Z"ddde#e         dededz  defdZ$dddddddddeded edz  d!ed"ed#ededz  d$e#e         dz  d%edz  dee         f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)Iterator)contextmanagernullcontext)Path)ContextManager)tqdm   )HF_HUB_DISABLE_PROGRESS_BARSprogress_bar_statesc                   :    e Zd ZdZddedz  ddfdZd	dZd
dZdS )disable_progress_barsa&  
    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.

    Works as both a regular call and a context manager:
        disable_progress_bars()           # disables until enable_progress_bars()
        with disable_progress_bars():     # disables for the block, re-enables on exit
            ...

    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.
    Nnamereturnc                 .   | _         t          du rt          j        d           d| _        d S t                     | _        %t                                           dt          d<   d S fdt          D             }|D ]
}t          |= dt          <   d S )NFzlCannot disable progress bars: environment variable `HF_HUB_DISABLE_PROGRESS_BARS=0` is set and has priority._globalc                 D    g | ]}|                      d           |S .
startswith.0keyr   s     [/usr/local/lib/hermes-agent/venv/lib/python3.11/site-packages/huggingface_hub/utils/tqdm.py
<listcomp>z2disable_progress_bars.__init__.<locals>.<listcomp>   s4    ___cCNNVZS]S]S]D^D^_c___    )r   r
   warningswarn_should_reenableare_progress_bars_disabledr   clear)selfr   keys_to_remover   s    `  r   __init__zdisable_progress_bars.__init__   s    	'500M~   %*D!F$>t$D$D D<%%'''-2	***____-@___N% - -',,(-%%%r   c                     | S N r"   s    r   	__enter__zdisable_progress_bars.__enter__       r   c                 @    | j         rt          | j                   d S d S r&   )r   enable_progress_barsr   )r"   excs     r   __exit__zdisable_progress_bars.__exit__   s-      	, +++++	, 	,r   r&   )r   r   )r   N)__name__
__module____qualname____doc__strr$   r)   r.   r'   r   r   r   r   l   sq         *. .S4Z .4 . . . .(   , , , , , ,r   r   r   r   c                      t           du rt          j        d           dS  %t                                           dt          d<   dS  fdt          D             }|D ]
}t          |= d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                 D    g | ]}|                      d           |S r   r   r   s     r   r   z(enable_progress_bars.<locals>.<listcomp>   s1    [[[#RVzzz@Z@Z[#[[[r   )r
   r   r   r   r!   )r   r#   r   s   `  r   r,   r,      s      $t++y	
 	
 	
 	|!!###)-I&&&[[[[)<[[[! 	) 	)C#C(($(D!!!r   c                 "   t           du rdS | t                              dd           S | rI| t          v rt          |           S d                    |                     d          dd                   } | It                              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splitr   s    r   r    r       s     $t++t|&**9d;;;;
 .&&&*4000xx

3,--  .
 #&&y$7777r   	log_levelc                 ^    | t           j        k    rdS t          j        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.
    TTQDM_POSITIONz-1FN)loggingNOTSETosgetenv)r<   s    r   is_tqdm_disabledrC      s5     GN""t	y!!T))u4r   c                   6     e Zd ZdZ fdZdeddf fdZ x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                     |                     dd           }t          |          rd|d<    t                      j        |i | d S )Nr   Tdisable)popr    superr$   )r"   argskwargsr   	__class__s       r   r$   ztqdm.__init__   sR    zz&$''%d++ 	% $F9$)&)))))r   attrr   Nc                 |    	 t                                          |           dS # t          $ r |dk    r Y dS w xY w)zBFix for https://github.com/huggingface/huggingface_hub/issues/1603_lockN)rH   __delattr__AttributeError)r"   rL   rK   s     r   rO   ztqdm.__delattr__   sV    	GG%%%%% 	 	 	w 	s   !& ;;)r/   r0   r1   r2   r$   r3   rO   __classcell__)rK   s   @r   r   r      sn         * * * * *           r   r   c                   B    e Zd ZdZd Zd Zd Zd
deez  dz  ddfd	Z	dS )silent_tqdmz#Fake tqdm object that does nothing.c                     d S r&   r'   )r"   rI   rJ   s      r   r$   zsilent_tqdm.__init__      r   c                     | S r&   r'   r(   s    r   r)   zsilent_tqdm.__enter__  r*   r   c                     d S r&   r'   )r"   exc_type	exc_value	tracebacks       r   r.   zsilent_tqdm.__exit__  rU   r      nNr   c                     d S r&   r'   )r"   r\   s     r   updatezsilent_tqdm.update  rU   r   )r[   )
r/   r0   r1   r2   r$   r)   r.   intfloatr^   r'   r   r   rS   rS     sw        --       ed* 4      r   rS   pathc              #     K   t          | t                    rt          |           } |                     d          5 }|                                 j        }t          dd|d| j                  |j        ddt          dz  d	t          ffd
}||_        |V                                   ddd           dS # 1 swxY w Y   dS )uQ  
    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:
    >>>     httpx.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descr7   sizeNr   c                 b     |           }                     t          |                     |S r&   )r^   len)rj   dataf_readpbars     r   _inner_readz%tqdm_stream_file.<locals>._inner_read7  s-    6$<<DKKD		"""Kr   )r7   )
isinstancer3   r   openstatst_sizer   r   readr_   bytesclose)ra   f
total_sizerp   rn   ro   s       @@r   tqdm_stream_filerz     s/     & $ Dzz	4 AYY[[(

 
 
 	 	cDj 	% 	 	 	 	 	 	 	
 

+                 s   A2B==CCr;   clsc                     t          | t                    rt          | t                    s | di |S t	          |          } | d||d|S )a  Create a progress bar.

    For our `tqdm` subclass (or subclasses of it): respects all disable signals
    (`HF_HUB_DISABLE_PROGRESS_BARS`, `disable_progress_bars()`, log level) and uses
    `disable=None` for TTY auto-detection (see https://github.com/huggingface/huggingface_hub/pull/2000),
    unless `TQDM_POSITION=-1` forces bars on (https://github.com/huggingface/huggingface_hub/pull/2698).

    For other classes: does not inject `disable` or `name`. the custom class is fully
    responsible for its own behavior. Vanilla tqdm defaults to `disable=False` (bar shows).
    Omits `name` which vanilla tqdm rejects with `TqdmKeyError`. See https://github.com/huggingface/huggingface_hub/issues/4050.
    )rF   r   r'   )rq   type
issubclassr   rC   )r{   r<   r   rJ   rF   s        r   _create_progress_barr   C  se     sD!! jd&;&; s}}V}} y))G34wT44V444r   rd   T)rg   rh   re   rf   r   
tqdm_class	_tqdm_barri   rg   rh   re   rf   r   r   c        	   
      `    |t          |          S t          |pt          |||||||           S )N)r{   r<   r   re   rf   rg   rh   ri   )r   r   r   )	ri   r<   rg   rh   re   rf   r   r   r   s	            r   _get_progress_bar_contextr   Y  sN     9%%%
  $	 	 	 	r   r&   )&r2   ior?   rA   	threadingr   collections.abcr   
contextlibr   r   pathlibr   typingr   	tqdm.autor   old_tqdm	constantsr
   r   dictr3   bool__annotations__r   r,   r    r_   rC   set_lockRLockrS   BufferedReaderrz   r}   r   r   r'   r   r   <module>r      s-  B B BH 
			  				      $ $ $ $ $ $ 2 2 2 2 2 2 2 2       ! ! ! ! ! ! & & & & & & 4 4 4 4 4 4 (* T#t)_ ) ) )/, /, /, /, /, /, /, /,d) )sTz )T ) ) ) )@8 8S4Z 84 8 8 8 8:
 
t 
 
 
 
    8   4 oio               *4#: *(23D*E * * * *Z UY 5 5 5h 5C 5sTz 5go 5 5 5 54 (,!  
  :	
    * X% d{ D     r   