o
    h#                     @   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   d/var/www/html/construction_image-detection-poc/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	   path_1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
        )r9   )r	   r4   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   )cls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'   r5   r   r9   r:   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   r6   r   r   r	   argsr
   r   r   r   r'      r   zNoOpCallback.callN)r>   r?   r@   rA   r'   r   r   r   r   rC      s    rC   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	   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)rF   r   r   r   r   r      s   zDotPrinterCallback.branchc                 K   s   t | jdd dS )zJust outputs a character )endN)printrH   )r	   r
   r   r   r   r'      s   zDotPrinterCallback.call)rG   )r>   r?   r@   rA   r   r   r'   __classcell__r   r   rK   r   rF      s
    rF   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)rS   ImportErrorr   	_tqdm_cls_tqdm_kwargsrI   r   )r	   tqdm_kwargsrE   r
   rS   excerK   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   )rS   rV   r   rW   rZ   r/   r   nrD   r   r   r   r'   5  s   

zTqdmCallback.callc                 C   s"   | j d ur| j   d | _ d S d S r   )rS   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@   rA   r   r'   r   r\   rQ   r   r   rK   r   rR      s    9rR   N)	functoolsr   r   rC   rF   rR   r   _DEFAULT_CALLBACKr   r   r   r   <module>   s     L	W