From 9a1d74048a7fa4ed8aaf8e3da62be9c72ffd3047 Mon Sep 17 00:00:00 2001 From: Saugat Pachhai Date: Thu, 30 Sep 2021 20:38:32 +0545 Subject: [PATCH] cli: add short alias for --show-{csv,json,md} (#6711) As `--csv`/`--json`/`--md` --- dvc/command/data_sync.py | 1 + dvc/command/diff.py | 11 +++++--- dvc/command/experiments.py | 25 ++++++++++------- dvc/command/ls/__init__.py | 7 +++-- dvc/command/metrics.py | 14 +++++++--- dvc/command/params.py | 7 +++-- dvc/command/status.py | 2 +- dvc/compare.py | 2 +- tests/func/experiments/test_show.py | 2 +- tests/unit/command/ls/test_ls.py | 2 +- tests/unit/command/test_compat_flag.py | 38 ++++++++++++++++++++++++++ tests/unit/command/test_diff.py | 30 ++++++++++---------- tests/unit/command/test_experiments.py | 6 ++-- tests/unit/command/test_metrics.py | 6 ++-- tests/unit/command/test_params.py | 8 ++---- tests/unit/command/test_status.py | 2 +- tests/unit/test_compare.py | 2 +- 17 files changed, 111 insertions(+), 54 deletions(-) create mode 100644 tests/unit/command/test_compat_flag.py diff --git a/dvc/command/data_sync.py b/dvc/command/data_sync.py index 43c0c7f943..b31f04982a 100644 --- a/dvc/command/data_sync.py +++ b/dvc/command/data_sync.py @@ -393,6 +393,7 @@ def add_parser(subparsers, _parent_parser): help="Show status of all stages in the specified directory.", ) status_parser.add_argument( + "--json", "--show-json", action="store_true", default=False, diff --git a/dvc/command/diff.py b/dvc/command/diff.py index d717bf2fd8..b6023e364f 100644 --- a/dvc/command/diff.py +++ b/dvc/command/diff.py @@ -16,7 +16,7 @@ def _digest(checksum): return "{}..{}".format(checksum["old"][0:8], checksum["new"][0:8]) -def _show_md(diff, show_hash=False, hide_missing=False): +def _show_markdown(diff, show_hash=False, hide_missing=False): headers = ["Status", "Hash", "Path"] if show_hash else ["Status", "Path"] rows = [] statuses = ["added", "deleted", "renamed", "modified"] @@ -142,10 +142,10 @@ def run(self): del entry["hash"] diff[key] = entries - if self.args.show_json: + if self.args.json: ui.write(json.dumps(diff)) - elif self.args.show_md: - _show_md(diff, show_hash, hide_missing) + elif self.args.markdown: + _show_markdown(diff, show_hash, hide_missing) elif diff: self._show_diff(diff, hide_missing) @@ -188,6 +188,7 @@ def add_parser(subparsers, parent_parser): nargs="?", ) diff_parser.add_argument( + "--json", "--show-json", help="Format the output into a JSON", action="store_true", @@ -200,9 +201,11 @@ def add_parser(subparsers, parent_parser): default=False, ) diff_parser.add_argument( + "--md", "--show-md", help="Show tabulated output in the Markdown format (GFM).", action="store_true", + dest="markdown", default=False, ) diff_parser.add_argument( diff --git a/dvc/command/experiments.py b/dvc/command/experiments.py index 94c1e61535..98a79306f0 100644 --- a/dvc/command/experiments.py +++ b/dvc/command/experiments.py @@ -383,7 +383,7 @@ def baseline_styler(typ): def show_experiments( - all_experiments, pager=True, no_timestamp=False, show_csv=False, **kwargs + all_experiments, pager=True, no_timestamp=False, csv=False, **kwargs ): from funcy.seqs import flatten as flatten_list @@ -439,7 +439,7 @@ def show_experiments( row_styles = lmap(baseline_styler, td.column("typ")) - if not show_csv: + if not csv: merge_headers = ["Experiment", "rev", "typ", "parent"] td.column("Experiment")[:] = map( prepare_exp_id, td.as_dict(merge_headers) @@ -473,7 +473,7 @@ def show_experiments( rich_table=True, header_styles=styles, row_styles=row_styles, - show_csv=show_csv, + csv=csv, ) @@ -506,18 +506,18 @@ def run(self): logger.exception("failed to show experiments") return 1 - if self.args.show_json: + if self.args.json: import json ui.write(json.dumps(all_experiments, default=_format_json)) else: precision = ( self.args.precision or None - if self.args.show_csv + if self.args.csv else DEFAULT_PRECISION ) - fill_value = "" if self.args.show_csv else FILL_VALUE - iso = True if self.args.show_csv else False + fill_value = "" if self.args.csv else FILL_VALUE + iso = True if self.args.csv else False show_experiments( all_experiments, @@ -532,7 +532,7 @@ def run(self): fill_value=fill_value, iso=iso, pager=not self.args.no_pager, - show_csv=self.args.show_csv, + csv=self.args.csv, ) return 0 @@ -561,7 +561,7 @@ def run(self): logger.exception("failed to show experiments diff") return 1 - if self.args.show_json: + if self.args.json: import json ui.write(json.dumps(diff)) @@ -579,7 +579,7 @@ def run(self): show_diff( diff[key], title=title, - markdown=self.args.show_md, + markdown=self.args.markdown, no_path=self.args.no_path, old=self.args.old, on_empty_diff="diff not supported", @@ -1085,12 +1085,14 @@ def add_parser(subparsers, parent_parser): help="Always show git commit SHAs instead of branch/tag names.", ) experiments_show_parser.add_argument( + "--json", "--show-json", action="store_true", default=False, help="Print output in JSON format instead of a human-readable table.", ) experiments_show_parser.add_argument( + "--csv", "--show-csv", action="store_true", default=False, @@ -1159,15 +1161,18 @@ def add_parser(subparsers, parent_parser): help="Show only params that are stage dependencies.", ) experiments_diff_parser.add_argument( + "--json", "--show-json", action="store_true", default=False, help="Show output in JSON format.", ) experiments_diff_parser.add_argument( + "--md", "--show-md", action="store_true", default=False, + dest="markdown", help="Show tabulated output in the Markdown format (GFM).", ) experiments_diff_parser.add_argument( diff --git a/dvc/command/ls/__init__.py b/dvc/command/ls/__init__.py index fb9b6463e9..d8bfe02413 100644 --- a/dvc/command/ls/__init__.py +++ b/dvc/command/ls/__init__.py @@ -34,7 +34,7 @@ def run(self): recursive=self.args.recursive, dvc_only=self.args.dvc_only, ) - if self.args.show_json: + if self.args.json: ui.write_json(entries) elif entries: entries = _prettify(entries, with_color=True) @@ -69,7 +69,10 @@ def add_parser(subparsers, parent_parser): "--dvc-only", action="store_true", help="Show only DVC outputs." ) list_parser.add_argument( - "--show-json", action="store_true", help="Show output in JSON format." + "--json", + "--show-json", + action="store_true", + help="Show output in JSON format.", ) list_parser.add_argument( "--rev", diff --git a/dvc/command/metrics.py b/dvc/command/metrics.py index f8c5223e64..57261b0d8d 100644 --- a/dvc/command/metrics.py +++ b/dvc/command/metrics.py @@ -31,7 +31,7 @@ def run(self): logger.exception("") return 1 - if self.args.show_json: + if self.args.json: import json ui.write(json.dumps(metrics, default=encode_exception)) @@ -40,7 +40,7 @@ def run(self): show_metrics( metrics, - markdown=self.args.show_md, + markdown=self.args.markdown, all_branches=self.args.all_branches, all_tags=self.args.all_tags, all_commits=self.args.all_commits, @@ -65,7 +65,7 @@ def run(self): logger.exception("failed to show metrics diff") return 1 - if self.args.show_json: + if self.args.json: import json ui.write(json.dumps(diff)) @@ -75,7 +75,7 @@ def run(self): show_diff( diff, title="Metric", - markdown=self.args.show_md, + markdown=self.args.markdown, no_path=self.args.no_path, precision=self.args.precision or DEFAULT_PRECISION, round_digits=True, @@ -140,15 +140,18 @@ def add_parser(subparsers, parent_parser): help="Show metrics for all commits.", ) metrics_show_parser.add_argument( + "--json", "--show-json", action="store_true", default=False, help="Show output in JSON format.", ) metrics_show_parser.add_argument( + "--md", "--show-md", action="store_true", default=False, + dest="markdown", help="Show tabulated output in the Markdown format (GFM).", ) metrics_show_parser.add_argument( @@ -220,15 +223,18 @@ def add_parser(subparsers, parent_parser): help="Show unchanged metrics as well.", ) metrics_diff_parser.add_argument( + "--json", "--show-json", action="store_true", default=False, help="Show output in JSON format.", ) metrics_diff_parser.add_argument( + "--md", "--show-md", action="store_true", default=False, + dest="markdown", help="Show tabulated output in the Markdown format (GFM).", ) metrics_diff_parser.add_argument( diff --git a/dvc/command/params.py b/dvc/command/params.py index 4f87d00fb5..199427c0e2 100644 --- a/dvc/command/params.py +++ b/dvc/command/params.py @@ -25,7 +25,7 @@ def run(self): logger.exception("failed to show params diff") return 1 - if self.args.show_json: + if self.args.json: import json ui.write(json.dumps(diff)) @@ -35,7 +35,7 @@ def run(self): show_diff( diff, title="Param", - markdown=self.args.show_md, + markdown=self.args.markdown, no_path=self.args.no_path, show_changes=False, ) @@ -103,15 +103,18 @@ def add_parser(subparsers, parent_parser): help="Show only params that are stage dependencies.", ) params_diff_parser.add_argument( + "--json", "--show-json", action="store_true", default=False, help="Show output in JSON format.", ) params_diff_parser.add_argument( + "--md", "--show-md", action="store_true", default=False, + dest="markdown", help="Show tabulated output in the Markdown format (GFM).", ) params_diff_parser.add_argument( diff --git a/dvc/command/status.py b/dvc/command/status.py index 5252b0c5e8..1b74f096bf 100644 --- a/dvc/command/status.py +++ b/dvc/command/status.py @@ -69,7 +69,7 @@ def run(self): if self.args.quiet: return bool(st) - if self.args.show_json: + if self.args.json: import json ui.write(json.dumps(st)) diff --git a/dvc/compare.py b/dvc/compare.py index ff51c2ce1a..27da4904a9 100644 --- a/dvc/compare.py +++ b/dvc/compare.py @@ -168,7 +168,7 @@ def row_from_dict(self, d: Mapping[str, "CellT"]) -> None: def render(self, **kwargs: Any): from dvc.ui import ui - if kwargs.pop("show_csv", False): + if kwargs.pop("csv", False): ui.write(self.to_csv(), end="") else: ui.table(self, headers=self.keys(), **kwargs) diff --git a/tests/func/experiments/test_show.py b/tests/func/experiments/test_show.py index 2e87018cf1..2620e37f47 100644 --- a/tests/func/experiments/test_show.py +++ b/tests/func/experiments/test_show.py @@ -513,7 +513,7 @@ def _get_rev_isotimestamp(rev): ref_info2 = first(exp_refs_by_rev(scm, rev2)) capsys.readouterr() - assert main(["exp", "show", "--show-csv"]) == 0 + assert main(["exp", "show", "--csv"]) == 0 cap = capsys.readouterr() assert ( "Experiment,rev,typ,Created,parent,metrics.yaml:foo,params.yaml:foo" diff --git a/tests/unit/command/ls/test_ls.py b/tests/unit/command/ls/test_ls.py index 7e7970c01e..a0540707e2 100644 --- a/tests/unit/command/ls/test_ls.py +++ b/tests/unit/command/ls/test_ls.py @@ -57,7 +57,7 @@ def test_list_outputs_only(mocker): def test_show_json(mocker, capsys): - cli_args = parse_args(["list", "local_dir", "--show-json"]) + cli_args = parse_args(["list", "local_dir", "--json"]) assert cli_args.func == CmdList cmd = cli_args.func(cli_args) diff --git a/tests/unit/command/test_compat_flag.py b/tests/unit/command/test_compat_flag.py new file mode 100644 index 0000000000..1c559a017a --- /dev/null +++ b/tests/unit/command/test_compat_flag.py @@ -0,0 +1,38 @@ +from itertools import takewhile + +import pytest + +from dvc.cli import parse_args + + +def _id_gen(val) -> str: + if isinstance(val, list): + return "-".join(takewhile(lambda v: not v.startswith("-"), val)) + return str(val) + + +@pytest.mark.parametrize( + "args, key", + [ + (["diff", "--show-json"], "json"), + (["diff", "--show-md"], "markdown"), + (["experiments", "diff", "--show-json"], "json"), + (["experiments", "diff", "--show-md"], "markdown"), + (["experiments", "show", "--show-json"], "json"), + (["experiments", "show", "--show-csv"], "csv"), + (["ls", "--show-json", "."], "json"), + (["metrics", "diff", "--show-json"], "json"), + (["metrics", "diff", "--show-md"], "markdown"), + (["metrics", "show", "--show-json"], "json"), + (["metrics", "show", "--show-md"], "markdown"), + (["params", "diff", "--show-json"], "json"), + (["params", "diff", "--show-md"], "markdown"), + (["status", "--show-json"], "json"), + ], + ids=_id_gen, +) +def test_backward_compat_flags(args, key): + """Test support for --show-csv/--show-json/--show-md flags.""" + cli_args = parse_args(args) + d = vars(cli_args) + assert d[key] is True diff --git a/tests/unit/command/test_diff.py b/tests/unit/command/test_diff.py index ea1a1bed4e..d626ec253d 100644 --- a/tests/unit/command/test_diff.py +++ b/tests/unit/command/test_diff.py @@ -4,7 +4,7 @@ import pytest from dvc.cli import parse_args -from dvc.command.diff import _digest, _show_md +from dvc.command.diff import _digest, _show_markdown @pytest.mark.parametrize( @@ -102,7 +102,7 @@ def test_show_hash(mocker, capsys): def test_show_json(mocker, capsys): - args = parse_args(["diff", "--show-json"]) + args = parse_args(["diff", "--json"]) cmd = args.func(args) diff = { "added": [ @@ -124,7 +124,7 @@ def test_show_json(mocker, capsys): def test_show_json_and_hash(mocker, capsys): - args = parse_args(["diff", "--show-json", "--show-hash"]) + args = parse_args(["diff", "--json", "--show-hash"]) cmd = args.func(args) diff = { @@ -161,7 +161,7 @@ def test_show_json_and_hash(mocker, capsys): def test_show_json_hide_missing(mocker, capsys): - args = parse_args(["diff", "--show-json", "--hide-missing"]) + args = parse_args(["diff", "--json", "--hide-missing"]) cmd = args.func(args) diff = { "added": [ @@ -190,22 +190,22 @@ def test_show_json_hide_missing(mocker, capsys): @pytest.mark.parametrize("show_hash", [None, True, False]) -def test_diff_show_md_and_hash(mocker, show_hash): - options = ["diff", "--show-md"] + (["--show-hash"] if show_hash else []) +def test_diff_show_markdown_and_hash(mocker, show_hash): + options = ["diff", "--md"] + (["--show-hash"] if show_hash else []) args = parse_args(options) cmd = args.func(args) diff = {} show_hash = show_hash if show_hash else False - mock_show_md = mocker.patch("dvc.command.diff._show_md") + mock_show_markdown = mocker.patch("dvc.command.diff._show_markdown") mocker.patch("dvc.repo.Repo.diff", return_value=diff.copy()) assert 0 == cmd.run() - mock_show_md.assert_called_once_with(diff, show_hash, False) + mock_show_markdown.assert_called_once_with(diff, show_hash, False) def test_no_changes(mocker, capsys): - args = parse_args(["diff", "--show-json"]) + args = parse_args(["diff", "--json"]) cmd = args.func(args) mocker.patch("dvc.repo.Repo.diff", return_value={}) @@ -220,7 +220,7 @@ def test_no_changes(mocker, capsys): assert not out -def test_show_md(capsys): +def test_show_markdown(capsys): diff = { "deleted": [ {"path": "zoo"}, @@ -234,7 +234,7 @@ def test_show_md(capsys): "not in cache": [{"path": "file2"}], } - _show_md(diff) + _show_markdown(diff) out, _ = capsys.readouterr() assert out == ( "| Status | Path |\n" @@ -251,7 +251,7 @@ def test_show_md(capsys): ).format(sep=os.path.sep) -def test_show_md_with_hash(capsys): +def test_show_markdown_with_hash(capsys): diff = { "deleted": [ {"path": "zoo", "hash": "22222"}, @@ -272,7 +272,7 @@ def test_show_md_with_hash(capsys): "not in cache": [{"path": "file2", "hash": "12345678"}], } - _show_md(diff, show_hash=True) + _show_markdown(diff, show_hash=True) out, _ = capsys.readouterr() assert out == ( @@ -290,7 +290,7 @@ def test_show_md_with_hash(capsys): ).format(sep=os.path.sep) -def test_show_md_hide_missing(capsys): +def test_show_markdown_hide_missing(capsys): diff = { "deleted": [ {"path": "zoo"}, @@ -304,7 +304,7 @@ def test_show_md_hide_missing(capsys): "not in cache": [{"path": "file2"}], } - _show_md(diff, hide_missing=True) + _show_markdown(diff, hide_missing=True) out, _ = capsys.readouterr() assert out == ( diff --git a/tests/unit/command/test_experiments.py b/tests/unit/command/test_experiments.py index 2fb23ac030..ca9e6bbd19 100644 --- a/tests/unit/command/test_experiments.py +++ b/tests/unit/command/test_experiments.py @@ -42,8 +42,8 @@ def test_experiments_diff(dvc, scm, mocker): "HEAD~1", "--all", "--param-deps", - "--show-json", - "--show-md", + "--json", + "--md", "--old", "--precision", "10", @@ -381,7 +381,7 @@ def test_experiments_remove(dvc, scm, mocker, queue, clear_all, remote): def test_show_experiments(capsys): show_experiments( - all_experiments, precision=None, fill_value="", iso=True, show_csv=True + all_experiments, precision=None, fill_value="", iso=True, csv=True ) cap = capsys.readouterr() assert ( diff --git a/tests/unit/command/test_metrics.py b/tests/unit/command/test_metrics.py index 4b233abe87..63b09778b1 100644 --- a/tests/unit/command/test_metrics.py +++ b/tests/unit/command/test_metrics.py @@ -13,7 +13,7 @@ def test_metrics_diff(dvc, mocker, capsys): "HEAD~1", "-R", "--all", - "--show-md", + "--md", "--targets", "target1", "target2", @@ -59,7 +59,7 @@ def test_metrics_diff_json(dvc, mocker, capsys): "HEAD~1", "-R", "--all", - "--show-json", + "--json", "--targets", "target1", "target2", @@ -139,7 +139,7 @@ def test_metrics_show_json(dvc, mocker, capsys): [ "metrics", "show", - "--show-json", + "--json", "-R", "--all-tags", "--all-branches", diff --git a/tests/unit/command/test_params.py b/tests/unit/command/test_params.py index 49428d1910..a9ec7e55e4 100644 --- a/tests/unit/command/test_params.py +++ b/tests/unit/command/test_params.py @@ -12,8 +12,8 @@ def test_params_diff(dvc, mocker): "--targets", "target", "--all", - "--show-json", - "--show-md", + "--json", + "--md", "--no-path", "--deps", ] @@ -61,9 +61,7 @@ def test_params_diff_from_cli(dvc, mocker): def test_params_diff_show_json(dvc, mocker, capsys): - cli_args = parse_args( - ["params", "diff", "HEAD~10", "HEAD~1", "--show-json"] - ) + cli_args = parse_args(["params", "diff", "HEAD~10", "HEAD~1", "--json"]) cmd = cli_args.func(cli_args) mocker.patch( "dvc.repo.params.diff.diff", return_value={"params.yaml": {"a": "b"}} diff --git a/tests/unit/command/test_status.py b/tests/unit/command/test_status.py index 23dd9502d3..8f7844b639 100644 --- a/tests/unit/command/test_status.py +++ b/tests/unit/command/test_status.py @@ -46,7 +46,7 @@ def test_cloud_status(tmp_dir, dvc, mocker): @pytest.mark.parametrize("status", [{}, {"a": "b", "c": [1, 2, 3]}, [1, 2, 3]]) def test_status_show_json(dvc, mocker, capsys, status): - cli_args = parse_args(["status", "--show-json"]) + cli_args = parse_args(["status", "--json"]) assert cli_args.func == CmdDataStatus cmd = cli_args.func(cli_args) diff --git a/tests/unit/test_compare.py b/tests/unit/test_compare.py index 98cf31e1fb..8d896cbcdc 100644 --- a/tests/unit/test_compare.py +++ b/tests/unit/test_compare.py @@ -516,7 +516,7 @@ def test_metrics_show_default(capsys): ) -def test_metrics_show_md(capsys): +def test_metrics_show_markdown(capsys): show_metrics( metrics={ "branch_1": {