|
11 | 11 | import click |
12 | 12 |
|
13 | 13 | from . import gpkg |
14 | | -from .output_util import dump_json_output, resolve_output_path |
| 14 | +from .output_util import ( |
| 15 | + dump_json_output, |
| 16 | + resolve_output_path, |
| 17 | +) |
15 | 18 |
|
16 | 19 |
|
17 | 20 | @contextlib.contextmanager |
@@ -50,82 +53,82 @@ def diff_output_text(*, output_path, **kwargs): |
50 | 53 | In particular, geometry WKT is abbreviated and null values are represented |
51 | 54 | by a unicode "␀" character. |
52 | 55 | """ |
53 | | - fp = resolve_output_path(output_path) |
54 | | - pecho = {'file': fp, 'color': fp.isatty()} |
55 | 56 | if isinstance(output_path, Path) and output_path.is_dir(): |
56 | 57 | raise click.BadParameter( |
57 | 58 | "Directory is not valid for --output with --text", param_hint="--output" |
58 | 59 | ) |
59 | 60 |
|
60 | 61 | def _out(dataset, diff): |
61 | | - path = dataset.path |
62 | | - pk_field = dataset.primary_key |
63 | | - prefix = f"{path}:" |
64 | | - repr_excl = [pk_field] |
65 | | - |
66 | | - for k, (v_old, v_new) in diff["META"].items(): |
67 | | - click.secho( |
68 | | - f"--- {prefix}meta/{k}\n+++ {prefix}meta/{k}", bold=True, **pecho |
69 | | - ) |
70 | | - |
71 | | - s_old = set(v_old.items()) |
72 | | - s_new = set(v_new.items()) |
73 | | - |
74 | | - diff_add = dict(s_new - s_old) |
75 | | - diff_del = dict(s_old - s_new) |
76 | | - all_keys = set(diff_del.keys()) | set(diff_add.keys()) |
77 | | - |
78 | | - for k in all_keys: |
79 | | - if k in diff_del: |
80 | | - click.secho( |
81 | | - text_row({k: diff_del[k]}, prefix="- ", exclude=repr_excl), |
82 | | - fg="red", |
83 | | - **pecho, |
84 | | - ) |
85 | | - if k in diff_add: |
86 | | - click.secho( |
87 | | - text_row({k: diff_add[k]}, prefix="+ ", exclude=repr_excl), |
88 | | - fg="green", |
89 | | - **pecho, |
90 | | - ) |
91 | | - |
92 | | - prefix = f"{path}:{pk_field}=" |
| 62 | + with resolve_output_path(output_path) as fp: |
| 63 | + pecho = {'file': fp, 'color': getattr(fp, 'color', fp.isatty())} |
| 64 | + path = dataset.path |
| 65 | + pk_field = dataset.primary_key |
| 66 | + prefix = f"{path}:" |
| 67 | + repr_excl = [pk_field] |
| 68 | + |
| 69 | + for k, (v_old, v_new) in diff["META"].items(): |
| 70 | + click.secho( |
| 71 | + f"--- {prefix}meta/{k}\n+++ {prefix}meta/{k}", bold=True, **pecho |
| 72 | + ) |
93 | 73 |
|
94 | | - for k, v_old in diff["D"].items(): |
95 | | - click.secho(f"--- {prefix}{k}", bold=True, **pecho) |
96 | | - click.secho( |
97 | | - text_row(v_old, prefix="- ", exclude=repr_excl), fg="red", **pecho |
98 | | - ) |
| 74 | + s_old = set(v_old.items()) |
| 75 | + s_new = set(v_new.items()) |
| 76 | + |
| 77 | + diff_add = dict(s_new - s_old) |
| 78 | + diff_del = dict(s_old - s_new) |
| 79 | + all_keys = set(diff_del.keys()) | set(diff_add.keys()) |
| 80 | + |
| 81 | + for k in all_keys: |
| 82 | + if k in diff_del: |
| 83 | + click.secho( |
| 84 | + text_row({k: diff_del[k]}, prefix="- ", exclude=repr_excl), |
| 85 | + fg="red", |
| 86 | + **pecho, |
| 87 | + ) |
| 88 | + if k in diff_add: |
| 89 | + click.secho( |
| 90 | + text_row({k: diff_add[k]}, prefix="+ ", exclude=repr_excl), |
| 91 | + fg="green", |
| 92 | + **pecho, |
| 93 | + ) |
| 94 | + |
| 95 | + prefix = f"{path}:{pk_field}=" |
| 96 | + |
| 97 | + for k, v_old in diff["D"].items(): |
| 98 | + click.secho(f"--- {prefix}{k}", bold=True, **pecho) |
| 99 | + click.secho( |
| 100 | + text_row(v_old, prefix="- ", exclude=repr_excl), fg="red", **pecho |
| 101 | + ) |
99 | 102 |
|
100 | | - for o in diff["I"]: |
101 | | - click.secho(f"+++ {prefix}{o[pk_field]}", bold=True, **pecho) |
102 | | - click.secho( |
103 | | - text_row(o, prefix="+ ", exclude=repr_excl), fg="green", **pecho |
104 | | - ) |
| 103 | + for o in diff["I"]: |
| 104 | + click.secho(f"+++ {prefix}{o[pk_field]}", bold=True, **pecho) |
| 105 | + click.secho( |
| 106 | + text_row(o, prefix="+ ", exclude=repr_excl), fg="green", **pecho |
| 107 | + ) |
105 | 108 |
|
106 | | - for _, (v_old, v_new) in diff["U"].items(): |
107 | | - click.secho( |
108 | | - f"--- {prefix}{v_old[pk_field]}\n+++ {prefix}{v_new[pk_field]}", |
109 | | - bold=True, |
110 | | - **pecho, |
111 | | - ) |
| 109 | + for _, (v_old, v_new) in diff["U"].items(): |
| 110 | + click.secho( |
| 111 | + f"--- {prefix}{v_old[pk_field]}\n+++ {prefix}{v_new[pk_field]}", |
| 112 | + bold=True, |
| 113 | + **pecho, |
| 114 | + ) |
112 | 115 |
|
113 | | - s_old = set(v_old.items()) |
114 | | - s_new = set(v_new.items()) |
| 116 | + s_old = set(v_old.items()) |
| 117 | + s_new = set(v_new.items()) |
115 | 118 |
|
116 | | - diff_add = dict(s_new - s_old) |
117 | | - diff_del = dict(s_old - s_new) |
118 | | - all_keys = sorted(set(diff_del.keys()) | set(diff_add.keys())) |
| 119 | + diff_add = dict(s_new - s_old) |
| 120 | + diff_del = dict(s_old - s_new) |
| 121 | + all_keys = sorted(set(diff_del.keys()) | set(diff_add.keys())) |
119 | 122 |
|
120 | | - for k in all_keys: |
121 | | - if k in diff_del: |
122 | | - rk = text_row({k: diff_del[k]}, prefix="- ", exclude=repr_excl) |
123 | | - if rk: |
124 | | - click.secho(rk, fg="red", **pecho) |
125 | | - if k in diff_add: |
126 | | - rk = text_row({k: diff_add[k]}, prefix="+ ", exclude=repr_excl) |
127 | | - if rk: |
128 | | - click.secho(rk, fg="green", **pecho) |
| 123 | + for k in all_keys: |
| 124 | + if k in diff_del: |
| 125 | + rk = text_row({k: diff_del[k]}, prefix="- ", exclude=repr_excl) |
| 126 | + if rk: |
| 127 | + click.secho(rk, fg="red", **pecho) |
| 128 | + if k in diff_add: |
| 129 | + rk = text_row({k: diff_add[k]}, prefix="+ ", exclude=repr_excl) |
| 130 | + if rk: |
| 131 | + click.secho(rk, fg="green", **pecho) |
129 | 132 |
|
130 | 133 | yield _out |
131 | 134 |
|
@@ -367,20 +370,19 @@ def diff_output_html(*, output_path, repo, base, target, dataset_count, **kwargs |
367 | 370 |
|
368 | 371 | if not output_path: |
369 | 372 | output_path = Path(repo.path) / "DIFF.html" |
370 | | - fo = resolve_output_path(output_path) |
371 | | - |
372 | | - # Read all the geojson back in, and stick them in a dict |
373 | | - all_datasets_geojson = {} |
374 | | - for filename in os.listdir(tempdir): |
375 | | - with open(tempdir / filename) as json_file: |
376 | | - all_datasets_geojson[os.path.splitext(filename)[0]] = json.load( |
377 | | - json_file |
| 373 | + with resolve_output_path(output_path) as fo: |
| 374 | + # Read all the geojson back in, and stick them in a dict |
| 375 | + all_datasets_geojson = {} |
| 376 | + for filename in os.listdir(tempdir): |
| 377 | + with open(tempdir / filename) as json_file: |
| 378 | + all_datasets_geojson[os.path.splitext(filename)[0]] = json.load( |
| 379 | + json_file |
| 380 | + ) |
| 381 | + fo.write( |
| 382 | + template.substitute( |
| 383 | + {"title": title, "geojson_data": json.dumps(all_datasets_geojson)} |
378 | 384 | ) |
379 | | - fo.write( |
380 | | - template.substitute( |
381 | | - {"title": title, "geojson_data": json.dumps(all_datasets_geojson)} |
382 | 385 | ) |
383 | | - ) |
384 | | - if fo != sys.stdout: |
385 | | - fo.close() |
386 | | - webbrowser.open_new(f"file://{output_path.resolve()}") |
| 386 | + if fo != sys.stdout: |
| 387 | + fo.close() |
| 388 | + webbrowser.open_new(f"file://{output_path.resolve()}") |
0 commit comments