-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathcli.py
64 lines (60 loc) · 1.78 KB
/
cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import click
import json as std_json
from . import load_csv, load_json, compare, human_text
@click.command()
@click.version_option()
@click.argument(
"previous",
type=click.Path(exists=True, file_okay=True, dir_okay=False, allow_dash=False),
)
@click.argument(
"current",
type=click.Path(exists=True, file_okay=True, dir_okay=False, allow_dash=False),
)
@click.option(
"--key", type=str, default=None, multiple=True, help="Column to use as a unique ID for each row. This argument may be specified multiple times"
)
@click.option(
"--format",
type=click.Choice(["csv", "tsv", "json"]),
default=None,
help="Explicitly specify input format (csv, tsv, json) instead of auto-detecting",
)
@click.option(
"--json", type=bool, default=False, help="Output changes as JSON", is_flag=True
)
@click.option(
"--singular",
type=str,
default=None,
help="Singular word to use, e.g. 'tree' for '1 tree'",
)
@click.option(
"--plural",
type=str,
default=None,
help="Plural word to use, e.g. 'trees' for '2 trees'",
)
@click.option(
"--show-unchanged",
is_flag=True,
help="Show unchanged fields for rows with at least one change",
)
def cli(previous, current, key, format, json, singular, plural, show_unchanged):
"Diff two CSV or JSON files"
dialect = {
"csv": "excel",
"tsv": "excel-tab",
}
def load(filename):
if format == "json":
return load_json(open(filename), key=key)
else:
return load_csv(
open(filename, newline=""), key=key, dialect=dialect.get(format)
)
diff = compare(load(previous), load(current), show_unchanged)
if json:
print(std_json.dumps(diff, indent=4))
else:
print(human_text(diff, key, singular, plural))