Skip to content

Commit 0f4e8a2

Browse files
authored
fix(ls): support tabulate 0.10.0 preserve_whitespace (#11021)
1 parent 333edb2 commit 0f4e8a2

File tree

3 files changed

+31
-19
lines changed

3 files changed

+31
-19
lines changed

dvc/commands/ls/__init__.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,6 @@ def _build_tree_structure(
109109

110110

111111
def show_tree(entries, with_color=False, with_size=False, with_hash=False):
112-
import tabulate
113-
114112
rows = _build_tree_structure(
115113
entries,
116114
with_color=with_color,
@@ -119,13 +117,7 @@ def show_tree(entries, with_color=False, with_size=False, with_hash=False):
119117
)
120118

121119
colalign = ("right",) if with_size else None
122-
123-
_orig = tabulate.PRESERVE_WHITESPACE
124-
tabulate.PRESERVE_WHITESPACE = True
125-
try:
126-
ui.table(rows, colalign=colalign)
127-
finally:
128-
tabulate.PRESERVE_WHITESPACE = _orig
120+
ui.table(rows, colalign=colalign, preserve_whitespace=True)
129121

130122

131123
class CmdList(CmdBaseNoRepo):

dvc/ui/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ def error_console(self) -> "RichConsole":
289289

290290
return console.Console(stderr=True)
291291

292-
def table(
292+
def table( # noqa: PLR0913
293293
self,
294294
data: "TableData",
295295
headers: Optional["Headers"] = None,
@@ -301,6 +301,7 @@ def table(
301301
row_styles: Optional[Sequence["Styles"]] = None,
302302
borders: Union[bool, str] = False,
303303
colalign: Optional[tuple[str, ...]] = None,
304+
preserve_whitespace: bool = False,
304305
) -> None:
305306
from dvc.ui import table as t
306307

@@ -329,6 +330,7 @@ def table(
329330
pager=pager,
330331
force=force,
331332
colalign=colalign,
333+
preserve_whitespace=preserve_whitespace,
332334
)
333335

334336
def status(self, status: str, **kwargs: Any) -> "Status":

dvc/ui/table.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,37 @@ def plain_table(
3030
pager: bool = False,
3131
force: bool = True,
3232
colalign: Optional[tuple[str, ...]] = None,
33+
preserve_whitespace: bool = False,
3334
) -> None:
35+
import tabulate as tabulate_mod
3436
from funcy import nullcontext
37+
from packaging.version import Version
3538
from tabulate import tabulate
3639

37-
text: str = tabulate(
38-
data,
39-
headers if headers is not None else (),
40-
tablefmt="github" if markdown else "plain",
41-
disable_numparse=True,
42-
# None will be shown as "" by default, overriding
43-
missingval="-",
44-
colalign=colalign,
45-
)
40+
# NOTE: tabulate 0.10.0+ supports preserve_whitespace as a kwarg,
41+
# while 0.9.0 only respects the global PRESERVE_WHITESPACE.
42+
kwargs: dict = {}
43+
_orig = tabulate_mod.PRESERVE_WHITESPACE
44+
if preserve_whitespace:
45+
if Version(tabulate_mod.__version__) >= Version("0.10"):
46+
kwargs["preserve_whitespace"] = True
47+
else:
48+
tabulate_mod.PRESERVE_WHITESPACE = True
49+
50+
try:
51+
text: str = tabulate(
52+
data,
53+
headers if headers is not None else (),
54+
tablefmt="github" if markdown else "plain",
55+
disable_numparse=True,
56+
# None will be shown as "" by default, overriding
57+
missingval="-",
58+
colalign=colalign,
59+
**kwargs,
60+
)
61+
finally:
62+
tabulate_mod.PRESERVE_WHITESPACE = _orig
63+
4664
if markdown:
4765
# NOTE: md table is incomplete without the trailing newline
4866
text += "\n"

0 commit comments

Comments
 (0)