
    çg#                         d dl mZ  G d d          Z G d de          Z G d de          Z G d d	e          Z e            xZZd
S )    )wrapsc                       e Zd ZdZddZd Zd Zd Zd Zd	 Z	d
 Z
d ZddZddZd Zd Zd Zd Zedd            ZdS )Callbacka  
    Base class and interface for callback mechanism

    This class can be used directly for monitoring file transfers by
    providing ``callback=Callback(hooks=...)`` (see the ``hooks`` argument,
    below), or subclassed for more specialised behaviour.

    Parameters
    ----------
    size: int (optional)
        Nominal quantity for the value that corresponds to a complete
        transfer, e.g., total number of tiles or total number of
        bytes
    value: int (0)
        Starting internal counter value
    hooks: dict or None
        A dict of named functions to be called on each update. The signature
        of these must be ``f(size, value, **kwargs)``
    Nr   c                 B    || _         || _        |pi | _        || _        d S N)sizevaluehookskw)selfr   r	   r
   kwargss        L/var/www/html/ai-engine/env/lib/python3.11/site-packages/fsspec/callbacks.py__init__zCallback.__init__   s&    	
[b
    c                     | S r    r   s    r   	__enter__zCallback.__enter__   s    r   c                 .    |                                   d S r   close)r   exc_argss     r   __exit__zCallback.__exit__"   s    

r   c                     dS )zClose callback.Nr   r   s    r   r   zCallback.close%   s      r   c                 f    |                      |||           |                    dt                    S )a  
        Return callback for child transfers

        If this callback is operating at a higher level, e.g., put, which may
        trigger transfers that can also be monitored. The function returns a callback
        that has to be passed to the child method, e.g., put_file,
        as `callback=` argument.

        The implementation uses `callback.branch` for compatibility.
        When implementing callbacks, it is recommended to override this function instead
        of `branch` and avoid calling `super().branched(...)`.

        Prefer using this function over `branch`.

        Parameters
        ----------
        path_1: str
            Child's source path
        path_2: str
            Child's destination path
        **kwargs:
            Arbitrary keyword arguments

        Returns
        -------
        callback: Callback
            A callback instance to be passed to the child method
        callback)branchpopDEFAULT_CALLBACKr   path_1path_2r   s       r   branchedzCallback.branched(   s/    : 	FFF+++zz*&6777r   c                 P     t                    dt          f fd            }|S )zI
        Wraps a coroutine, and pass a new child callback to it.
        path2c                 ~   K    j         | |fi |5 } | |fd|i| d {V cd d d            S # 1 swxY w Y   d S )Nr   )r#   )path1r%   r   childfnr   s       r   funcz"Callback.branch_coro.<locals>.funcN   s      ue66v66 H%RuGGuGGGGGGGGGH H H H H H H H H H H H H H H H H Hs   266)r   str)r   r)   r*   s   `` r   branch_corozCallback.branch_coroI   sO    
 
r	HS 	H 	H 	H 	H 	H 	H 
	H r   c                 <    || _         |                                  dS )z
        Set the internal maximum size attribute

        Usually called if not initially set at instantiation. Note that this
        triggers a ``call()``.

        Parameters
        ----------
        size: int
        N)r   call)r   r   s     r   set_sizezCallback.set_sizeU   s     			r   c                 <    || _         |                                  dS )z
        Set the internal value state

        Triggers ``call()``

        Parameters
        ----------
        value: int
        Nr	   r.   )r   r	   s     r   absolute_updatezCallback.absolute_updatec   s     
		r      c                 N    | xj         |z  c_         |                                  dS )z
        Delta increment the internal counter

        Triggers ``call()``

        Parameters
        ----------
        inc: int
        Nr1   )r   incs     r   relative_updatezCallback.relative_updatep   s%     	

c

		r   c                 0   | j         sdS | j                                        }|                    |           |r*|| j         vrdS  | j         |         | j        | j        fi |S | j                                         pg D ]} || j        | j        fi | dS )a  
        Execute hook(s) with current state

        Each function is passed the internal size and current value

        Parameters
        ----------
        hook_name: str or None
            If given, execute on this hook
        kwargs: passed on to (all) hook(s)
        N)r
   r   copyupdater   r	   values)r   	hook_namer   r   hooks        r   r.   zCallback.call}   s     z 	FW\\^^
		& 	F
**(4:i(DJEE"EEEJ%%''-2 	. 	.DDDJ--"----	. 	.r   c              #   D   K   |D ]}|                                   |V  dS )z
        Wrap an iterable to call ``relative_update`` on each iterations

        Parameters
        ----------
        iterable: Iterable
            The iterable that is being wrapped
        N)r6   )r   iterableitems      r   wrapzCallback.wrap   s>        	 	D  """JJJJ	 	r   c                     dS )a;  
        Set callbacks for child transfers

        If this callback is operating at a higher level, e.g., put, which may
        trigger transfers that can also be monitored. The passed kwargs are
        to be *mutated* to add ``callback=``, if this class supports branching
        to children.

        Parameters
        ----------
        path_1: str
            Child's source path
        path_2: str
            Child's destination path
        kwargs: dict
            arguments passed to child method, e.g., put_file.

        Returns
        -------

        Nr   r    s       r   r   zCallback.branch   s	    , tr   c                     d S r   r   )r   ___s      r   no_opzCallback.no_op   s    r   c                     | j         S )zP
        If undefined methods are called on this class, nothing happens
        )rE   )r   r?   s     r   __getattr__zCallback.__getattr__   s     zr   c                     |t           S |S )a  Transform callback=... into Callback instance

        For the special value of ``None``, return the global instance of
        ``NoOpCallback``. This is an alternative to including
        ``callback=DEFAULT_CALLBACK`` directly in a method signature.
        )r   )clsmaybe_callbacks     r   as_callbackzCallback.as_callback   s     !##r   )Nr   N)r3   r   )__name__
__module____qualname____doc__r   r   r   r   r#   r,   r/   r2   r6   r.   r@   r   rE   rG   classmethodrK   r   r   r   r   r      s         (         8 8 8B
 
 
       . . . ..    0     	 	 	 [	 	 	r   r   c                       e Zd ZdZd ZdS )NoOpCallbackz>
    This implementation of Callback does exactly nothing
    c                     d S r   r   r   argsr   s      r   r.   zNoOpCallback.call   s    tr   N)rL   rM   rN   rO   r.   r   r   r   rR   rR      s-             r   rR   c                   0     e Zd ZdZd fd	Zd Zd Z xZS )DotPrinterCallbackz
    Simple example Callback implementation

    Almost identical to Callback with a hook that prints a char; here we
    demonstrate how the outer layer may print "#" and the inner layer "."
    #c                 H    || _          t                      j        di | d S )Nr   )chrsuperr   )r   chr_to_printr   	__class__s      r   r   zDotPrinterCallback.__init__   s-    ""6"""""r   c                 *    t          d          |d<   dS )z;Mutate kwargs to add new instance with different print char.r   N)rW   r    s       r   r   zDotPrinterCallback.branch   s    /44zr   c                 2    t          | j        d           dS )zJust outputs a character )endN)printrZ   )r   r   s     r   r.   zDotPrinterCallback.call   s    dhBr   )rX   )rL   rM   rN   rO   r   r   r.   __classcell__r]   s   @r   rW   rW      se         # # # # # #5 5 5             r   rW   c                   6     e Zd ZdZd fd	Zd Zd Zd Z xZS )TqdmCallbacka  
    A callback to display a progress bar using tqdm

    Parameters
    ----------
    tqdm_kwargs : dict, (optional)
        Any argument accepted by the tqdm constructor.
        See the `tqdm doc <https://tqdm.github.io/docs/tqdm/#__init__>`_.
        Will be forwarded to `tqdm_cls`.
    tqdm_cls: (optional)
        subclass of `tqdm.tqdm`. If not passed, it will default to `tqdm.tqdm`.

    Examples
    --------
    >>> import fsspec
    >>> from fsspec.callbacks import TqdmCallback
    >>> fs = fsspec.filesystem("memory")
    >>> path2distant_data = "/your-path"
    >>> fs.upload(
            ".",
            path2distant_data,
            recursive=True,
            callback=TqdmCallback(),
        )

    You can forward args to tqdm using the ``tqdm_kwargs`` parameter.

    >>> fs.upload(
            ".",
            path2distant_data,
            recursive=True,
            callback=TqdmCallback(tqdm_kwargs={"desc": "Your tqdm description"}),
        )

    You can also customize the progress bar by passing a subclass of `tqdm`.

    .. code-block:: python

        class TqdmFormat(tqdm):
            '''Provides a `total_time` format parameter'''
            @property
            def format_dict(self):
                d = super().format_dict
                total_time = d["elapsed"] * (d["total"] or 0) / max(d["n"], 1)
                d.update(total_time=self.format_interval(total_time) + " in total")
                return d

    >>> with TqdmCallback(
            tqdm_kwargs={
                "desc": "desc",
                "bar_format": "{total_time}: {percentage:.0f}%|{bar}{r_bar}",
            },
            tqdm_cls=TqdmFormat,
        ) as callback:
            fs.upload(".", path2distant_data, recursive=True, callback=callback)
    Nc                     	 ddl m } n"# t          $ r}t          d          |d }~ww xY w|                    d|          | _        |pi | _        d | _          t                      j        |i | d S )Nr   )tqdmz0Using TqdmCallback requires tqdm to be installedtqdm_cls)ri   ImportErrorr   	_tqdm_cls_tqdm_kwargsr[   r   )r   tqdm_kwargsrU   r   ri   excer]   s         r   r   zTqdmCallback.__init__'  s    	!!!!!!! 	 	 	B 	
  J55'-2	$)&)))))s   
 
)$)c                     | j          | j        dd| j        i| j        | _         | j        | j         _        | j                             | j        | j         j        z
             d S )Ntotalr   )ri   rl   r   rm   rq   r9   r	   nrT   s      r   r.   zTqdmCallback.call5  s_    9&LLTYL$:KLLDI)		dik122222r   c                 X    | j         "| j                                          d | _         d S d S r   )ri   r   r   s    r   r   zTqdmCallback.close;  s/    9 IOODIII ! r   c                 *    |                                  S r   r   r   s    r   __del__zTqdmCallback.__del__@  s    zz||r   r   )	rL   rM   rN   rO   r   r.   r   ru   rd   re   s   @r   rg   rg      su        7 7r* * * * * *3 3 3  
      r   rg   N)	functoolsr   r   rR   rW   rg   r   _DEFAULT_CALLBACKr   r   r   <module>rx      s         H H H H H H H HV    8                  *T T T T T8 T T Tn (4|~~ 5 $$$r   