o
    Êôjú#  ã                   @   sX   d dl mZ G dd„ dƒZG dd„ deƒZG dd„ deƒZG dd	„ d	eƒZeƒ  ZZd
S )é    )Úwrapsc                   @   s”   e Zd ZdZd#dd„Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zd$dd„Zd%dd„Zdd„ Zdd„ Zdd„ Zdd „ Zed%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                 K   s    || _ || _|p	i | _|| _d S ©N)ÚsizeÚvalueÚhooksÚkw)Úselfr   r   r   Úkwargs© r   úc/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/fsspec/callbacks.pyÚ__init__   s   

zCallback.__init__c                 C   s   | S r   r   ©r	   r   r   r   Ú	__enter__   ó   zCallback.__enter__c                 G   s   |   ¡  d S r   ©Úclose)r	   Úexc_argsr   r   r   Ú__exit__"   s   zCallback.__exit__c                 C   ó   dS )zClose callback.Nr   r   r   r   r   r   %   s    zCallback.closec                 K   s   |   |||¡ | 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	   Zpath_1Zpath_2r
   r   r   r   Úbranched(   s   zCallback.branchedc                    s    t ˆ ƒdtf‡ ‡fdd„ƒ}|S )zI
        Wraps a coroutine, and pass a new child callback to it.
        Úpath2c                 ›   sV   �ˆj | |fi |¤Ž�}ˆ | |fd|i|¤ŽI d H W  d   ƒ S 1 s$w   Y  d S )Nr   )r   )Úpath1r   r
   Úchild©Úfnr	   r   r   ÚfuncN   s   €$ÿz"Callback.branch_coro.<locals>.func)r   Ústr)r	   r    r!   r   r   r   Úbranch_coroI   s   zCallback.branch_coroc                 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   r   r   r   Úset_sizeU   s   zCallback.set_sizec                 C   r$   )z…
        Set the internal value state

        Triggers ``call()``

        Parameters
        ----------
        value: int
        N©r   r%   )r	   r   r   r   r   Úabsolute_updatec   s   
zCallback.absolute_updateé   c                 C   s   |  j |7  _ |  ¡  dS )z‹
        Delta increment the internal counter

        Triggers ``call()``

        Parameters
        ----------
        inc: int
        Nr'   )r	   Úincr   r   r   Úrelative_updatep   s   
zCallback.relative_updatec                 K   sz   | 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 |¤Ž q-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   Úhookr   r   r   r%   }   s   


ÿzCallback.callc                 c   s   � |D ]	}|   ¡  |V  qdS )zÂ
        Wrap an iterable to call ``relative_update`` on each iterations

        Parameters
        ----------
        iterable: Iterable
            The iterable that is being wrapped
        N)r+   )r	   ÚiterableÚitemr   r   r   Úwrap”   s
   €	þzCallback.wrapc                 C   r   )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   r   r   r   r   ¡   s   zCallback.branchc                 O   ó   d S r   r   )r	   Ú_Ú__r   r   r   Úno_op¹   r   zCallback.no_opc                 C   s   | j S )zP
        If undefined methods are called on this class, nothing happens
        )r7   )r	   r2   r   r   r   Ú__getattr__¼   s   zCallback.__getattr__c                 C   s   |du r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.
        N)r   )ÚclsZmaybe_callbackr   r   r   Úas_callbackÂ   s   zCallback.as_callback)Nr   N)r)   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r#   r&   r(   r+   r%   r3   r   r7   r8   Úclassmethodr:   r   r   r   r   r      s$    
!

r   c                   @   s   e Zd ZdZdd„ ZdS )ÚNoOpCallbackz>
    This implementation of Callback does exactly nothing
    c                 O   r4   r   r   ©r	   Úargsr
   r   r   r   r%   Ô   r   zNoOpCallback.callN)r;   r<   r=   r>   r%   r   r   r   r   r@   Ï   s    r@   c                       s2   e Zd ZdZd	‡ fdd„	Zdd„ Zdd„ Z‡  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                    s   || _ tƒ jdi |¤Ž d S )Nr   )ÚchrÚsuperr   )r	   Zchr_to_printr
   ©Ú	__class__r   r   r   à   s   zDotPrinterCallback.__init__c                 C   s   t dƒ|d< dS )z;Mutate kwargs to add new instance with different print charÚ.r   N)rC   r   r   r   r   r   ä   s   zDotPrinterCallback.branchc                 K   s   t | jdd� dS )zJust outputs a characterÚ )ÚendN)ÚprintrE   )r	   r
   r   r   r   r%   è   s   zDotPrinterCallback.call)rD   )r;   r<   r=   r>   r   r   r%   Ú__classcell__r   r   rG   r   rC   Ø   s
    rC   c                       s:   e Zd ZdZd‡ fdd„	Zdd„ Zdd„ Zd	d
„ Z‡  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              
      sh   zddl m } W n ty } ztdƒ|‚d }~ww | d|¡| _|p$i | _d | _ tƒ j|i |¤Ž d S )Nr   )Útqdmz0Using TqdmCallback requires tqdm to be installedÚtqdm_cls)rO   ÚImportErrorr   Ú	_tqdm_clsÚ_tqdm_kwargsrF   r   )r	   Útqdm_kwargsrB   r
   rO   ZexcerG   r   r   r   '  s   ÿþ€ÿ
zTqdmCallback.__init__c                 O   sH   | j d u r| jdd| ji| j¤Ž| _ | j| j _| j  | j| j j ¡ d S )NÚtotalr   )rO   rR   r   rS   rU   r-   r   ÚnrA   r   r   r   r%   5  s   

zTqdmCallback.callc                 C   s"   | j d ur| j  ¡  d | _ d S d S r   )rO   r   r   r   r   r   r   ;  s   


þzTqdmCallback.closec                 C   s   |   ¡ S r   r   r   r   r   r   Ú__del__@  s   zTqdmCallback.__del__r   )	r;   r<   r=   r>   r   r%   r   rW   rM   r   r   rG   r   rN   í   s    9rN   N)Ú	functoolsr   r   r@   rC   rN   r   Z_DEFAULT_CALLBACKr   r   r   r   Ú<module>   s     L	W