From f336afa8611b2b08420edfbe537e6c393595bed5 Mon Sep 17 00:00:00 2001 From: Casper da Costa-Luis Date: Tue, 27 Aug 2019 20:23:55 +0100 Subject: [PATCH] remove Tqdm.truncate => add {ncols_desc} --- dvc/progress.py | 43 ++++++++++++++---------------------- dvc/remote/azure.py | 8 ++----- dvc/remote/http.py | 2 +- dvc/remote/oss.py | 8 ++----- dvc/remote/s3.py | 10 ++------- dvc/remote/ssh/connection.py | 4 ++-- dvc/utils/__init__.py | 7 ++---- 7 files changed, 28 insertions(+), 54 deletions(-) diff --git a/dvc/progress.py b/dvc/progress.py index 64c021653..a2331fc93 100644 --- a/dvc/progress.py +++ b/dvc/progress.py @@ -36,11 +36,13 @@ class Tqdm(tqdm): """ BAR_FMT_DEFAULT = ( - "{percentage:3.0f}%|{bar:10}|{desc} {bar:-10b}{n}/{total}" + "{percentage:3.0f}%|{bar:10}|" + "{desc:{ncols_desc}.{ncols_desc}}{n}/{total}" " [{elapsed}<{remaining}, {rate_fmt:>11}{postfix}]" ) BAR_FMT_NOTOTAL = ( - "{desc} {bar:b}{n} [{elapsed}11}{postfix}]" + "{desc:{ncols_desc}.{ncols_desc}}{n}" + " [{elapsed}11}{postfix}]" ) def __init__( @@ -49,7 +51,6 @@ class Tqdm(tqdm): disable=None, level=logging.ERROR, desc=None, - desc_truncate=None, leave=False, bar_format=None, bytes=False, # pylint: disable=W0622 @@ -59,7 +60,6 @@ class Tqdm(tqdm): bytes : shortcut for `unit='B', unit_scale=True, unit_divisor=1024, miniters=1` desc : persists after `close()` - desc_truncate : like `desc` but will `truncate()` and not persist level : effective logging level for determining `disable`; used only if `disable` is unspecified kwargs : anything accepted by `tqdm.tqdm()` @@ -72,8 +72,6 @@ class Tqdm(tqdm): kwargs, ) self.desc_persist = desc - if desc_truncate is not None: - desc = self.truncate(desc_truncate) if disable is None: disable = logger.getEffectiveLevel() > level super(Tqdm, self).__init__( @@ -81,7 +79,7 @@ class Tqdm(tqdm): disable=disable, leave=leave, desc=desc, - bar_format="", + bar_format="!", **kwargs ) if bar_format is None: @@ -92,13 +90,11 @@ class Tqdm(tqdm): self.bar_format = bar_format self.refresh() - def update_desc(self, desc, n=1, truncate=True): + def update_desc(self, desc, n=1): """ - Calls `set_description_str(truncate(desc))` and `update(n)` + Calls `set_description_str(desc)` and `update(n)` """ - self.set_description_str( - self.truncate(desc) if truncate else desc, refresh=False - ) + self.set_description_str(desc, refresh=False) self.update(n) def update_to(self, current, total=None): @@ -107,20 +103,15 @@ class Tqdm(tqdm): self.update(current - self.n) def close(self): - if self.desc_persist: + if self.desc_persist is not None: self.set_description_str(self.desc_persist, refresh=False) super(Tqdm, self).close() - @classmethod - def truncate(cls, s, max_len=25, end=True, fill="..."): - """ - Guarantee len(output) < max_lenself. - >>> truncate("hello", 4) - '...o' - """ - if len(s) <= max_len: - return s - if len(fill) > max_len: - return fill[-max_len:] if end else fill[:max_len] - i = max_len - len(fill) - return (fill + s[-i:]) if end else (s[:i] + fill) + @property + def format_dict(self): + """inject `ncols_desc` to fill the display width (`ncols`)""" + d = super(Tqdm, self).format_dict + ncols = d.get("ncols", 80) + ncols_desc = ncols - len(self.format_meter(ncols_desc=1, **d)) + 1 + d["ncols_desc"] = max(ncols_desc, 0) + return d diff --git a/dvc/remote/azure.py b/dvc/remote/azure.py index 2527b66ba..e891c345d 100644 --- a/dvc/remote/azure.py +++ b/dvc/remote/azure.py @@ -115,9 +115,7 @@ class RemoteAZURE(RemoteBASE): def _upload( self, from_file, to_info, name=None, no_progress_bar=False, **_kwargs ): - with Tqdm( - desc_truncate=name, disable=no_progress_bar, bytes=True - ) as pbar: + with Tqdm(desc=name, disable=no_progress_bar, bytes=True) as pbar: self.blob_service.create_blob_from_path( to_info.bucket, to_info.path, @@ -128,9 +126,7 @@ class RemoteAZURE(RemoteBASE): def _download( self, from_info, to_file, name=None, no_progress_bar=False, **_kwargs ): - with Tqdm( - desc_truncate=name, disable=no_progress_bar, bytes=True - ) as pbar: + with Tqdm(desc=name, disable=no_progress_bar, bytes=True) as pbar: self.blob_service.get_blob_to_path( from_info.bucket, from_info.path, diff --git a/dvc/remote/http.py b/dvc/remote/http.py index 6f430b0e7..c12c6e940 100644 --- a/dvc/remote/http.py +++ b/dvc/remote/http.py @@ -38,7 +38,7 @@ class RemoteHTTP(RemoteBASE): total=None if no_progress_bar else self._content_length(from_info), leave=False, bytes=True, - desc_truncate=from_info.url if name is None else name, + desc=from_info.url if name is None else name, disable=no_progress_bar, ) as pbar: with open(to_file, "wb") as fd: diff --git a/dvc/remote/oss.py b/dvc/remote/oss.py index 57e900087..ca6d5a4e0 100644 --- a/dvc/remote/oss.py +++ b/dvc/remote/oss.py @@ -107,9 +107,7 @@ class RemoteOSS(RemoteBASE): def _upload( self, from_file, to_info, name=None, no_progress_bar=False, **_kwargs ): - with Tqdm( - desc_truncate=name, disable=no_progress_bar, bytes=True - ) as pbar: + with Tqdm(desc=name, disable=no_progress_bar, bytes=True) as pbar: self.oss_service.put_object_from_file( to_info.path, from_file, progress_callback=pbar.update_to ) @@ -117,9 +115,7 @@ class RemoteOSS(RemoteBASE): def _download( self, from_info, to_file, name=None, no_progress_bar=False, **_kwargs ): - with Tqdm( - desc_truncate=name, disable=no_progress_bar, bytes=True - ) as pbar: + with Tqdm(desc=name, disable=no_progress_bar, bytes=True) as pbar: self.oss_service.get_object_to_file( from_info.path, to_file, progress_callback=pbar.update_to ) diff --git a/dvc/remote/s3.py b/dvc/remote/s3.py index 938a9a2ae..7de02f152 100644 --- a/dvc/remote/s3.py +++ b/dvc/remote/s3.py @@ -208,10 +208,7 @@ class RemoteS3(RemoteBASE): def _upload(self, from_file, to_info, name=None, no_progress_bar=False): total = os.path.getsize(from_file) with Tqdm( - disable=no_progress_bar, - total=total, - bytes=True, - desc_truncate=name, + disable=no_progress_bar, total=total, bytes=True, desc=name ) as pbar: self.s3.upload_file( from_file, @@ -229,10 +226,7 @@ class RemoteS3(RemoteBASE): Bucket=from_info.bucket, Key=from_info.path )["ContentLength"] with Tqdm( - disable=no_progress_bar, - total=total, - bytes=True, - desc_truncate=name, + disable=no_progress_bar, total=total, bytes=True, desc=name ) as pbar: self.s3.download_file( from_info.bucket, from_info.path, to_file, Callback=pbar.update diff --git a/dvc/remote/ssh/connection.py b/dvc/remote/ssh/connection.py index fbb0c2b7b..43b380ada 100644 --- a/dvc/remote/ssh/connection.py +++ b/dvc/remote/ssh/connection.py @@ -169,7 +169,7 @@ class SSHConnection: def download(self, src, dest, no_progress_bar=False, progress_title=None): with Tqdm( - desc_truncate=progress_title or os.path.basename(src), + desc=progress_title or os.path.basename(src), disable=no_progress_bar, bytes=True, ) as pbar: @@ -186,7 +186,7 @@ class SSHConnection: progress_title = posixpath.basename(dest) with Tqdm( - desc_truncate=progress_title, disable=no_progress_bar, bytes=True + desc=progress_title, disable=no_progress_bar, bytes=True ) as pbar: self.sftp.put(src, tmp_file, callback=pbar.update_to) diff --git a/dvc/utils/__init__.py b/dvc/utils/__init__.py index 9878a8c2e..59e943707 100644 --- a/dvc/utils/__init__.py +++ b/dvc/utils/__init__.py @@ -59,7 +59,7 @@ def file_md5(fname): name = relpath(fname) with Tqdm( - desc_truncate=name, + desc=name, disable=no_progress_bar, total=size, bytes=True, @@ -132,10 +132,7 @@ def copyfile(src, dest, no_progress_bar=False, name=None): System.reflink(src, dest) except DvcException: with Tqdm( - desc_truncate=name, - disable=no_progress_bar, - total=total, - bytes=True, + desc=name, disable=no_progress_bar, total=total, bytes=True ) as pbar: with open(src, "rb") as fsrc, open(dest, "wb+") as fdest: while True: