Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions dvc/command/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@
logger = logging.getLogger(__name__)


def _show_md(diff):
from dvc.utils.diff import table

rows = []
for status in ["added", "deleted", "modified"]:
entries = diff.get(status, [])
if not entries:
continue
paths = sorted([entry["path"] for entry in entries])
for path in paths:
rows.append([status, path])

return table(["Status", "Path"], rows, True)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the output's column be swapped?

Path Status
model.pkl Added

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^ and also capitalized?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the output's column be swapped?

Currently, it is in the order in which it was requested. And, which matches git status.

^ and also capitalized?

not capitalized because it was requested in that form and matches git status.



class CmdDiff(CmdBase):
@staticmethod
def _format(diff):
Expand Down Expand Up @@ -103,6 +118,8 @@ def run(self):

if self.args.show_json:
logger.info(json.dumps(diff))
elif self.args.show_md:
logger.info(_show_md(diff))
elif diff:
logger.info(self._format(diff))

Expand Down Expand Up @@ -147,4 +164,10 @@ def add_parser(subparsers, parent_parser):
action="store_true",
default=False,
)
diff_parser.add_argument(
"--show-md",
help="Show tabulated output in the Markdown format (GFM).",
action="store_true",
default=False,
)
diff_parser.set_defaults(func=CmdDiff)
12 changes: 9 additions & 3 deletions dvc/command/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def run(self):
return 0


def _show_diff(diff):
def _show_diff(diff, markdown=False):
from collections import OrderedDict

from dvc.utils.diff import table
Expand All @@ -105,7 +105,7 @@ def _show_diff(diff):
]
)

return table(["Path", "Metric", "Value", "Change"], rows)
return table(["Path", "Metric", "Value", "Change"], rows, markdown)


class CmdMetricsDiff(CmdBase):
Expand All @@ -124,7 +124,7 @@ def run(self):

logger.info(json.dumps(diff))
else:
table = _show_diff(diff)
table = _show_diff(diff, self.args.show_md)
if table:
logger.info(table)

Expand Down Expand Up @@ -263,6 +263,12 @@ def add_parser(subparsers, parent_parser):
default=False,
help="Show output in JSON format.",
)
metrics_diff_parser.add_argument(
"--show-md",
action="store_true",
default=False,
help="Show tabulated output in the Markdown format (GFM).",
)
metrics_diff_parser.set_defaults(func=CmdMetricsDiff)

METRICS_REMOVE_HELP = "Remove metric mark on a DVC-tracked file."
Expand Down
12 changes: 9 additions & 3 deletions dvc/command/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
logger = logging.getLogger(__name__)


def _show_diff(diff):
def _show_diff(diff, markdown=False):
from dvc.utils.diff import table

rows = []
Expand All @@ -17,7 +17,7 @@ def _show_diff(diff):
for param, change in sorted_pdiff.items():
rows.append([fname, param, change["old"], change["new"]])

return table(["Path", "Param", "Old", "New"], rows)
return table(["Path", "Param", "Old", "New"], rows, markdown)


class CmdParamsDiff(CmdBase):
Expand All @@ -34,7 +34,7 @@ def run(self):

logger.info(json.dumps(diff))
else:
table = _show_diff(diff)
table = _show_diff(diff, self.args.show_md)
if table:
logger.info(table)

Expand Down Expand Up @@ -94,4 +94,10 @@ def add_parser(subparsers, parent_parser):
default=False,
help="Show output in JSON format.",
)
params_diff_parser.add_argument(
"--show-md",
action="store_true",
default=False,
help="Show tabulated output in the Markdown format (GFM).",
)
params_diff_parser.set_defaults(func=CmdParamsDiff)
26 changes: 11 additions & 15 deletions dvc/utils/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,24 +85,20 @@ def diff(old, new, with_unchanged=False):
return dict(res)


def table(header, rows):
from texttable import Texttable
def table(header, rows, markdown=False):
from tabulate import tabulate

if not rows:
if not rows and not markdown:
return ""

t = Texttable()

# disable automatic formatting
t.set_cols_dtype(["t"] * len(header))

# remove borders to make it easier for users to copy stuff
t.set_chars([""] * len(header))
t.set_deco(0)

t.add_rows([header] + rows)

return t.draw()
return tabulate(
rows,
header,
tablefmt="github" if markdown else "plain",
disable_numparse=True,
# None will be shown as "" by default, overriding
missingval="None",
)


def format_dict(d):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def run(self):
"pydot>=1.2.4",
"speedcopy>=2.0.1; python_version < '3.8' and sys_platform == 'win32'",
"flatten_json>=0.1.6",
"texttable>=0.5.2",
"tabulate>=0.8.7",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not support aligning headers. It does have colalign but it aligns the whole column, and, I don't think it's a deal-breaker.

"pygtrie==2.3.2",
"dpath>=2.0.1,<3",
]
Expand Down
30 changes: 30 additions & 0 deletions tests/unit/command/test_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os

from dvc.cli import parse_args
from dvc.command.diff import _show_md


def test_default(mocker, caplog):
Expand Down Expand Up @@ -110,3 +111,32 @@ def info():
cmd = args.func(args)
assert 0 == cmd.run()
assert not info()


def test_show_md_empty():
assert _show_md({}) == ("| Status | Path |\n" "|----------|--------|")


def test_show_md():
diff = {
"deleted": [
{"path": "zoo", "hash": "22222"},
{"path": os.path.join("data", ""), "hash": "XXXXXXXX.dir"},
{"path": os.path.join("data", "foo"), "hash": "11111111"},
{"path": os.path.join("data", "bar"), "hash": "00000000"},
],
"modified": [
{"path": "file", "hash": {"old": "AAAAAAAA", "new": "BBBBBBBB"}}
],
"added": [{"path": "file", "hash": "00000000"}],
}
assert _show_md(diff) == (
"| Status | Path |\n"
"|----------|----------|\n"
"| added | file |\n"
"| deleted | data{sep} |\n"
"| deleted | data{sep}bar |\n"
"| deleted | data{sep}foo |\n"
"| deleted | zoo |\n"
"| modified | file |"
).format(sep=os.path.sep)
85 changes: 62 additions & 23 deletions tests/unit/command/test_metrics.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import textwrap

from dvc.cli import parse_args
from dvc.command.metrics import CmdMetricsDiff, CmdMetricsShow, _show_diff

Expand Down Expand Up @@ -37,25 +39,30 @@ def test_metrics_diff(dvc, mocker):
def test_metrics_show_json_diff():
assert _show_diff(
{"metrics.json": {"a.b.c": {"old": 1, "new": 2, "diff": 3}}}
) == (
" Path Metric Value Change\n"
"metrics.json a.b.c 2 3 "
) == textwrap.dedent(
"""\
Path Metric Value Change
metrics.json a.b.c 2 3"""
)


def test_metrics_show_raw_diff():
assert _show_diff({"metrics": {"": {"old": "1", "new": "2"}}}) == (
" Path Metric Value Change \n"
"metrics 2 diff not supported"
assert _show_diff(
{"metrics": {"": {"old": "1", "new": "2"}}}
) == textwrap.dedent(
"""\
Path Metric Value Change
metrics 2 diff not supported"""
)


def test_metrics_diff_no_diff():
assert _show_diff(
{"other.json": {"a.b.d": {"old": "old", "new": "new"}}}
) == (
" Path Metric Value Change \n"
"other.json a.b.d new diff not supported"
) == textwrap.dedent(
"""\
Path Metric Value Change
other.json a.b.d new diff not supported"""
)


Expand All @@ -66,18 +73,20 @@ def test_metrics_diff_no_changes():
def test_metrics_diff_new_metric():
assert _show_diff(
{"other.json": {"a.b.d": {"old": None, "new": "new"}}}
) == (
" Path Metric Value Change \n"
"other.json a.b.d new diff not supported"
) == textwrap.dedent(
"""\
Path Metric Value Change
other.json a.b.d new diff not supported"""
)


def test_metrics_diff_deleted_metric():
assert _show_diff(
{"other.json": {"a.b.d": {"old": "old", "new": None}}}
) == (
" Path Metric Value Change \n"
"other.json a.b.d None diff not supported"
) == textwrap.dedent(
"""\
Path Metric Value Change
other.json a.b.d None diff not supported"""
)


Expand Down Expand Up @@ -114,9 +123,10 @@ def test_metrics_show(dvc, mocker):
def test_metrics_diff_prec():
assert _show_diff(
{"other.json": {"a.b": {"old": 0.0042, "new": 0.0043, "diff": 0.0001}}}
) == (
" Path Metric Value Change\n"
"other.json a.b 0.0043 0.0001"
) == textwrap.dedent(
"""\
Path Metric Value Change
other.json a.b 0.0043 0.0001"""
)


Expand All @@ -129,9 +139,38 @@ def test_metrics_diff_sorted():
"a.b.c": {"old": 1, "new": 2, "diff": 1},
}
}
) == (
" Path Metric Value Change\n"
"metrics.yaml a.b.c 2 1 \n"
"metrics.yaml a.d.e 4 1 \n"
"metrics.yaml x.b 6 1 "
) == textwrap.dedent(
"""\
Path Metric Value Change
metrics.yaml a.b.c 2 1
metrics.yaml a.d.e 4 1
metrics.yaml x.b 6 1"""
)


def test_metrics_diff_markdown_empty():
assert _show_diff({}, markdown=True) == textwrap.dedent(
"""\
| Path | Metric | Value | Change |
|--------|----------|---------|----------|"""
)


def test_metrics_diff_markdown():
assert _show_diff(
{
"metrics.yaml": {
"x.b": {"old": 5, "new": 6},
"a.d.e": {"old": 3, "new": 4, "diff": 1},
"a.b.c": {"old": 1, "new": 2, "diff": 1},
}
},
markdown=True,
) == textwrap.dedent(
"""\
| Path | Metric | Value | Change |
|--------------|----------|---------|--------------------|
| metrics.yaml | a.b.c | 2 | 1 |
| metrics.yaml | a.d.e | 4 | 1 |
| metrics.yaml | x.b | 6 | diff not supported |"""
)
Loading