Skip to content

Commit

Permalink
Merge pull request #883 from enricofer/master
Browse files Browse the repository at this point in the history
html template option for diff command
  • Loading branch information
olsen232 committed Jul 5, 2023
2 parents 6f82173 + 8bdf19e commit 69532d2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
3 changes: 3 additions & 0 deletions kart/base_diff_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ def __init__(
target_crs=None,
# used by json-lines diffs only
diff_estimate_accuracy=None,
# used by html diff only
html_template=None
):
self.repo = repo
self.commit_spec = commit_spec
Expand All @@ -93,6 +95,7 @@ def __init__(

self.user_key_filters = user_key_filters
self.repo_key_filter = RepoKeyFilter.build_from_user_patterns(user_key_filters)
self.html_template = html_template

self.spatial_filter = repo.spatial_filter

Expand Down
8 changes: 8 additions & 0 deletions kart/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ def feature_count_diff(
is_flag=True,
help="Show changes to file contents (instead of just showing the object IDs of changed files)",
)
@click.option(
"--html-template",
default=None,
help="Provide a user defined/specific html template for diff representation",
type=click.Path(exists=True),
)
@click.argument(
"args",
metavar="[REVISIONS] [--] [FILTERS]",
Expand All @@ -147,6 +153,7 @@ def diff(
add_feature_count_estimate,
convert_to_dataset_format,
diff_files,
html_template,
args,
):
"""
Expand Down Expand Up @@ -205,6 +212,7 @@ def diff(
json_style=fmt,
target_crs=crs,
diff_estimate_accuracy=add_feature_count_estimate,
html_template=html_template,
)
diff_writer.convert_to_dataset_format(convert_to_dataset_format)
diff_writer.full_file_diffs(diff_files)
Expand Down
8 changes: 5 additions & 3 deletions kart/html_diff_writer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import string
import sys
import os
import webbrowser
from pathlib import Path

Expand Down Expand Up @@ -28,11 +29,12 @@ def _check_output_path(cls, repo, output_path):
return output_path or repo.workdir_path / "DIFF.html"

def write_diff(self, diff_format=DiffFormat.FULL):
template_path = self.html_template or (Path(kart.package_data_path) / "diff-view.html")
if not os.path.exists(template_path):
raise click.UsageError("Html template not found")
if diff_format != DiffFormat.FULL:
raise click.UsageError("Html format only supports full diffs")
with open(
Path(kart.package_data_path) / "diff-view.html", "r", encoding="utf8"
) as ft:
with open(template_path, "r", encoding="utf8") as ft:
template = string.Template(ft.read())

repo_diff = self.get_repo_diff(diff_format=diff_format)
Expand Down
13 changes: 13 additions & 0 deletions tests/test_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import re
import time
from pathlib import Path

import html5lib
import pytest
Expand Down Expand Up @@ -2096,3 +2097,15 @@ def test_attached_files_patch(data_archive, cli_runner):
"+": 'text:<gmd:MD_Metadata xmlns:gco="http://www.isotc211.org/2005/gco" xmlns:gmd="http://www.isotc211.org/2005/gmd" xmlns:gml="http://www.opengis.net/gml" xmlns:gts="http://www.isotc211.org/2005/gts" xmlns:topo="http://www.linz.govt.nz/schemas/topo/data-dictionary" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.isotc211.org/2005/gmd">'
},
}

def test_load_user_provided_html_template(data_archive, cli_runner):
with data_archive("points") as repo_path:
r = cli_runner.invoke(
[
"diff",
f"--output-format=html",
f"--html-template=" + str(Path(__file__).absolute().parent.parent / "kart" / "diff-view.html"),
"HEAD^...",
]
)
assert r.exit_code == 0, r.stderr

0 comments on commit 69532d2

Please sign in to comment.