|
1 | | -from typing import BinaryIO, Callable, Optional, Union |
| 1 | +import logging |
| 2 | +import sys |
| 3 | +from typing import Any, BinaryIO, Callable, ClassVar, Optional, Union |
2 | 4 |
|
3 | | -from dvc_objects.fs.callbacks import TqdmCallback |
4 | | -from fsspec.callbacks import DEFAULT_CALLBACK, Callback |
| 5 | +from fsspec.callbacks import DEFAULT_CALLBACK, Callback, TqdmCallback |
| 6 | +from tqdm import tqdm |
5 | 7 |
|
6 | 8 | from scmrepo.progress import GitProgressEvent |
7 | 9 |
|
8 | 10 |
|
| 11 | +class _Tqdm(tqdm): |
| 12 | + """ |
| 13 | + maximum-compatibility tqdm-based progressbars |
| 14 | + """ |
| 15 | + |
| 16 | + BAR_FMT_DEFAULT = ( |
| 17 | + "{percentage:3.0f}% {desc}|{bar}|" |
| 18 | + "{postfix[info]}{n_fmt}/{total_fmt}" |
| 19 | + " [{elapsed}<{remaining}, {rate_fmt:>11}]" |
| 20 | + ) |
| 21 | + # nested bars should have fixed bar widths to align nicely |
| 22 | + BAR_FMT_DEFAULT_NESTED = ( |
| 23 | + "{percentage:3.0f}%|{bar:10}|{desc:{ncols_desc}.{ncols_desc}}" |
| 24 | + "{postfix[info]}{n_fmt}/{total_fmt}" |
| 25 | + " [{elapsed}<{remaining}, {rate_fmt:>11}]" |
| 26 | + ) |
| 27 | + BAR_FMT_NOTOTAL = "{desc}{bar:b}|{postfix[info]}{n_fmt} [{elapsed}, {rate_fmt:>11}]" |
| 28 | + BYTES_DEFAULTS: ClassVar[dict[str, Any]] = { |
| 29 | + "unit": "B", |
| 30 | + "unit_scale": True, |
| 31 | + "unit_divisor": 1024, |
| 32 | + "miniters": 1, |
| 33 | + } |
| 34 | + |
| 35 | + def __init__( # noqa: PLR0913 |
| 36 | + self, |
| 37 | + iterable=None, |
| 38 | + disable=None, |
| 39 | + level=logging.ERROR, |
| 40 | + desc=None, |
| 41 | + leave=False, |
| 42 | + bar_format=None, |
| 43 | + bytes=False, # noqa: A002 |
| 44 | + file=None, |
| 45 | + total=None, |
| 46 | + postfix=None, |
| 47 | + **kwargs, |
| 48 | + ): |
| 49 | + kwargs = kwargs.copy() |
| 50 | + if bytes: |
| 51 | + kwargs = {**self.BYTES_DEFAULTS, **kwargs} |
| 52 | + else: |
| 53 | + kwargs.setdefault("unit_scale", total > 999 if total else True) |
| 54 | + if file is None: |
| 55 | + file = sys.stderr |
| 56 | + super().__init__( |
| 57 | + iterable=iterable, |
| 58 | + disable=disable, |
| 59 | + leave=leave, |
| 60 | + desc=desc, |
| 61 | + bar_format="!", |
| 62 | + lock_args=(False,), |
| 63 | + total=total, |
| 64 | + **kwargs, |
| 65 | + ) |
| 66 | + self.postfix = postfix or {"info": ""} |
| 67 | + if bar_format is None: |
| 68 | + if self.__len__(): |
| 69 | + self.bar_format = ( |
| 70 | + self.BAR_FMT_DEFAULT_NESTED if self.pos else self.BAR_FMT_DEFAULT |
| 71 | + ) |
| 72 | + else: |
| 73 | + self.bar_format = self.BAR_FMT_NOTOTAL |
| 74 | + else: |
| 75 | + self.bar_format = bar_format |
| 76 | + self.refresh() |
| 77 | + |
| 78 | + def update_to(self, current, total=None): |
| 79 | + if total: |
| 80 | + self.total = total |
| 81 | + self.update(current - self.n) |
| 82 | + |
| 83 | + def close(self): |
| 84 | + self.postfix["info"] = "" |
| 85 | + # remove ETA (either unknown or zero); remove completed bar |
| 86 | + self.bar_format = self.bar_format.replace("<{remaining}", "").replace( |
| 87 | + "|{bar:10}|", " " |
| 88 | + ) |
| 89 | + super().close() |
| 90 | + |
| 91 | + @property |
| 92 | + def format_dict(self): |
| 93 | + """inject `ncols_desc` to fill the display width (`ncols`)""" |
| 94 | + d = super().format_dict |
| 95 | + ncols = d["ncols"] or 80 |
| 96 | + # assumes `bar_format` has max one of ("ncols_desc" & "ncols_info") |
| 97 | + |
| 98 | + meter = self.format_meter( # type: ignore[call-arg] |
| 99 | + ncols_desc=1, ncols_info=1, **d |
| 100 | + ) |
| 101 | + ncols_left = ncols - len(meter) + 1 |
| 102 | + ncols_left = max(ncols_left, 0) |
| 103 | + if ncols_left: |
| 104 | + d["ncols_desc"] = d["ncols_info"] = ncols_left |
| 105 | + else: |
| 106 | + # work-around for zero-width description |
| 107 | + d["ncols_desc"] = d["ncols_info"] = 1 |
| 108 | + d["prefix"] = "" |
| 109 | + return d |
| 110 | + |
| 111 | + |
9 | 112 | class LFSCallback(Callback): |
10 | 113 | """Callback subclass to generate Git/LFS style progress.""" |
11 | 114 |
|
@@ -37,7 +140,11 @@ def _update_git(self): |
37 | 140 | def branched(self, path_1: Union[str, BinaryIO], path_2: str, **kwargs): |
38 | 141 | if self.git_progress: |
39 | 142 | return TqdmCallback( |
40 | | - bytes=True, desc=path_1 if isinstance(path_1, str) else path_2 |
| 143 | + tqdm_kwargs={ |
| 144 | + "desc": path_1 if isinstance(path_1, str) else path_2, |
| 145 | + "bytes": True, |
| 146 | + }, |
| 147 | + tqdm_cls=_Tqdm, |
41 | 148 | ) |
42 | 149 | return DEFAULT_CALLBACK |
43 | 150 |
|
|
0 commit comments