f#xddlmZGddZGddeZGddeZGdd eZexZZy ) )wrapsc|eZdZdZddZdZdZdZdZdZ d Z d Z dd Z dd Z d ZdZdZdZeddZy)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)`` Nc D||_||_|xsi|_||_yN)sizevaluehookskw)selfrr r kwargss Y/var/lib/jenkins/workspace/mettalog/venv/lib/python3.12/site-packages/fsspec/callbacks.py__init__zCallback.__init__s"  [b c|Srr s r __enter__zCallback.__enter__s rc$|jyrclose)r exc_argss r__exit__zCallback.__exit__"s  rcy)zClose callback.Nrrs rrzCallback.close%src T|j||||jdtS)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)branchpopDEFAULT_CALLBACKr path_1path_2r s rbranchedzCallback.branched(s&: FFF+zz*&677rc@tdtffd }|S)zI Wraps a coroutine, and pass a new child callback to it. path2cKj||fi|5}||fd|i|d{cdddS7 #1swYyxYww)Nr)r#)path1r%r childfnr s rfuncz"Callback.branch_coro..funcNsWue6v6 H%uGuGGG H HG H Hs$A868 A8AA)rstr)r r)r*s`` r branch_corozCallback.branch_coroIs, r HS H  H rc2||_|jy)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)rcall)r rs rset_sizezCallback.set_sizeUs  rc2||_|jy)z Set the internal value state Triggers ``call()`` Parameters ---------- value: int Nr r.)r r s rabsolute_updatezCallback.absolute_updatecs  rcN|xj|z c_|jy)z Delta increment the internal counter Triggers ``call()`` Parameters ---------- inc: int Nr1)r incs rrelative_updatezCallback.relative_updateps c  rc n|jsy|jj}|j||r:||jvry|j||j|j fi|S|jj xsgD] }||j|j fi|"y)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 copyupdaterr values)r hook_namer r hooks rr.z Callback.call}szz  WW\\^ &  *(4::i(DJJE"E EJJ%%'-2-D DJJ -" -.rc#BK|D]}|j|yw)z Wrap an iterable to call ``relative_update`` on each iterations Parameters ---------- iterable: Iterable The iterable that is being wrapped N)r5)r iterableitems rwrapz Callback.wraps#D  "Jscy)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 ------- Nrr s rrzCallback.branchs,rcyrr)r ___s rno_opzCallback.no_ops rc|jS)zP If undefined methods are called on this class, nothing happens )rD)r r>s r __getattr__zCallback.__getattr__szzrc|tS|S)aTransform 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)clsmaybe_callbacks r as_callbackzCallback.as_callbacks  !# #r)NrN)r)__name__ __module__ __qualname____doc__rrrrr#r,r/r2r5r.r?rrDrF classmethodrJrrrrrsb( 8B    .. 0    rrceZdZdZdZy) NoOpCallbackz> This implementation of Callback does exactly nothing cyrrr argsr s rr.zNoOpCallback.callsrN)rLrMrNrOr.rrrrRrRs rrRc0eZdZdZdfd ZdZdZxZS)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 2||_t|di|y)Nr)chrsuperr)r chr_to_printr __class__s rrzDotPrinterCallback.__init__s "6"rc td|d<y)z;Mutate kwargs to add new instance with different print char.rN)rWr s rrzDotPrinterCallback.branchs/4zrc 2t|jdy)zJust outputs a character)endN)printrY)r r s rr.zDotPrinterCallback.calls dhhBr)#)rLrMrNrOrrr. __classcell__r\s@rrWrWs#5 rrWc6eZdZdZdfd ZdZdZdZxZS) 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 `_. 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) c ddlm}|jd||_|xsi|_d|_t ||i|y#t$r}td|d}~wwxYw)Nr)tqdmz0Using TqdmCallback requires tqdm to be installedtqdm_cls)ri ImportErrorr _tqdm_cls _tqdm_kwargsrZr)r tqdm_kwargsrUr riexcer\s rrzTqdmCallback.__init__'sj  ! J5'-2  $)&) B  sA A AAc$|j-|jdd|ji|j|_|j|j_|jj |j |jjz y)Ntotalr)rirlrrmrqr8r nrTs rr.zTqdmCallback.call5sb 99 &LTYYL$:K:KLDI))  diikk12rc`|j"|jjd|_yyr)rirrs rrzTqdmCallback.close;s& 99 IIOO DI !rc"|jSrrrs r__del__zTqdmCallback.__del__@szz|rr) rLrMrNrOrr.rrurdres@rrgrgs7r *3  rrgN) functoolsrrrRrWrgr_DEFAULT_CALLBACKrrrrxsLHHV8  *T8Tn(4~5$r