cli: add short alias for --show-{csv,json,md} (#6711)

As `--csv`/`--json`/`--md`
This commit is contained in:
Saugat Pachhai
2021-09-30 20:38:32 +05:45
committed by GitHub
parent cce6aed378
commit 9a1d74048a
17 changed files with 111 additions and 54 deletions
+1
View File
@@ -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,
+7 -4
View File
@@ -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 @@ class CmdDiff(CmdBase):
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(
+15 -10
View File
@@ -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 @@ class CmdExperimentsShow(CmdBase):
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 @@ class CmdExperimentsShow(CmdBase):
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 @@ class CmdExperimentsDiff(CmdBase):
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 @@ class CmdExperimentsDiff(CmdBase):
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(
+5 -2
View File
@@ -34,7 +34,7 @@ class CmdList(CmdBaseNoRepo):
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",
+10 -4
View File
@@ -31,7 +31,7 @@ class CmdMetricsShow(CmdMetricsBase):
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 @@ class CmdMetricsShow(CmdMetricsBase):
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 @@ class CmdMetricsDiff(CmdMetricsBase):
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 @@ class CmdMetricsDiff(CmdMetricsBase):
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(
+5 -2
View File
@@ -25,7 +25,7 @@ class CmdParamsDiff(CmdBase):
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 @@ class CmdParamsDiff(CmdBase):
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(
+1 -1
View File
@@ -69,7 +69,7 @@ class CmdDataStatus(CmdDataBase):
if self.args.quiet:
return bool(st)
if self.args.show_json:
if self.args.json:
import json
ui.write(json.dumps(st))
+1 -1
View File
@@ -168,7 +168,7 @@ class TabularData(MutableSequence[Sequence["CellT"]]):
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)
+1 -1
View File
@@ -513,7 +513,7 @@ def test_show_csv(tmp_dir, scm, dvc, exp_stage, capsys):
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"
+1 -1
View File
@@ -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)
+38
View File
@@ -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
+15 -15
View File
@@ -4,7 +4,7 @@ import os
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 == (
+3 -3
View File
@@ -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 @@ all_experiments = {
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 (
+3 -3
View File
@@ -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",
+3 -5
View File
@@ -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"}}
+1 -1
View File
@@ -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)
+1 -1
View File
@@ -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": {