Skip to content

Commit

Permalink
cli: add short alias for --show-{csv,json,md} (#6711)
Browse files Browse the repository at this point in the history
As `--csv`/`--json`/`--md`
  • Loading branch information
skshetry committed Sep 30, 2021
1 parent cce6aed commit 9a1d740
Show file tree
Hide file tree
Showing 17 changed files with 111 additions and 54 deletions.
1 change: 1 addition & 0 deletions dvc/command/data_sync.py
Expand Up @@ -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,
Expand Down
11 changes: 7 additions & 4 deletions dvc/command/diff.py
Expand Up @@ -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"]
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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",
Expand All @@ -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(
Expand Down
25 changes: 15 additions & 10 deletions dvc/command/experiments.py
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -473,7 +473,7 @@ def show_experiments(
rich_table=True,
header_styles=styles,
row_styles=row_styles,
show_csv=show_csv,
csv=csv,
)


Expand Down Expand Up @@ -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,
Expand All @@ -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

Expand Down Expand Up @@ -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))
Expand All @@ -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",
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down
7 changes: 5 additions & 2 deletions dvc/command/ls/__init__.py
Expand Up @@ -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)
Expand Down Expand Up @@ -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",
Expand Down
14 changes: 10 additions & 4 deletions dvc/command/metrics.py
Expand Up @@ -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))
Expand All @@ -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,
Expand All @@ -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))
Expand All @@ -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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
7 changes: 5 additions & 2 deletions dvc/command/params.py
Expand Up @@ -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))
Expand All @@ -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,
)
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion dvc/command/status.py
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion dvc/compare.py
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/func/experiments/test_show.py
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/command/ls/test_ls.py
Expand Up @@ -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)
Expand Down
38 changes: 38 additions & 0 deletions 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

0 comments on commit 9a1d740

Please sign in to comment.