Skip to content

Commit

Permalink
chore: Run pre-commit hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Wuestengecko committed Nov 29, 2023
1 parent a08b7ac commit c58bad2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 26 deletions.
50 changes: 33 additions & 17 deletions pretty_print.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# Copyright DB Netz AG and contributors
# SPDX-License-Identifier: Apache-2.0

import click
import yaml
from jinja2 import Environment, FileSystemLoader
from diff_match_patch import diff_match_patch
from jinja2 import Environment, FileSystemLoader


def html_constructor(loader, node):
Expand All @@ -17,9 +20,10 @@ def diff_text(previous, current):
dmp.diff_cleanupSemantic(diff)
return dmp.diff_prettyHtml(diff)


def diff_objects(previous, current):
"""return html where previous display_name is shon in red and strike-through,
followed by arrow sign and current display_name in green"""
"""Return html where previous display_name is shon in red and strike-
through, followed by arrow sign and current display_name in green."""
return f"<del>{previous['display_name']}</del> → <ins>{current['display_name']}</ins>"


Expand All @@ -31,7 +35,9 @@ def diff_lists(previous, current):
out.append(f"<li><ins>{item}</ins></li>")
elif item["uuid"] in previous:
if item["display_name"] != previous[item["uuid"]]["display_name"]:
out.append(f"<li>{diff_objects(previous[item['uuid']], item)}</li>")
out.append(
f"<li>{diff_objects(previous[item['uuid']], item)}</li>"
)
else:
out.append(f"<li>{item['display_name']}</li>")
current = {item["uuid"]: item for item in current}
Expand All @@ -41,21 +47,23 @@ def diff_lists(previous, current):
return "<ul>" + "".join(out) + "</ul>"



def traverse_and_diff(data):
"""For every key "name" or "description" if it has
child keys "previous" and "current" we perform HtmlDiff
and store result in a new child key "diff".
"""
"""For every key "name" or "description" if it has child keys "previous"
and "current" we perform HtmlDiff and store result in a new child key
"diff"."""
updates = {}
for key, value in data.items():
if isinstance(value, dict) and "previous" in value and "current" in value:
if (
isinstance(value, dict)
and "previous" in value
and "current" in value
):
prev_type = type(value["previous"])
curr_type = type(value["current"])
if prev_type == curr_type == str:
diff = diff_text(
value["previous"].splitlines(),
value["current"].splitlines()
value["current"].splitlines(),
)
updates[key] = {"diff": diff}
elif prev_type == curr_type == dict:
Expand All @@ -74,10 +82,11 @@ def traverse_and_diff(data):
data[key].update(value)
return data


def compute_diff_stats(data):
"""We need to compute the quantity of created, modified and deleted items and
sum those up for each category and subcategory, like so:
"""We need to compute the quantity of created, modified and deleted items
and sum those up for each category and subcategory, like so:
1. when the child branch has lists of deletions, creations or modifications we return stats with lengths of those lists as values
2. we aggregate (sum) those stats for each category and subcategory
3. we add stats key + value to each category and subcategory
Expand All @@ -95,11 +104,17 @@ def compute_diff_stats(data):
if isinstance(value, dict):
child_stats = compute_diff_stats(value)
if "created" in child_stats:
stats["created"] = stats.get("created", 0) + child_stats["created"]
stats["created"] = (
stats.get("created", 0) + child_stats["created"]
)
if "modified" in child_stats:
stats["modified"] = stats.get("modified", 0) + child_stats["modified"]
stats["modified"] = (
stats.get("modified", 0) + child_stats["modified"]
)
if "deleted" in child_stats:
stats["deleted"] = stats.get("deleted", 0) + child_stats["deleted"]
stats["deleted"] = (
stats.get("deleted", 0) + child_stats["deleted"]
)
data["stats"] = stats
return stats

Expand All @@ -116,5 +131,6 @@ def main(filename):
with open("report.html", "w") as f:
f.write(html)


if __name__ == "__main__":
main()
18 changes: 9 additions & 9 deletions report.html.j2
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,13 @@
{% macro display_basic_changes(key, objects, color) %}
{% if key in objects %}
<h4 style="color: {{color}}">{{key | upper}} ({{ objects[key] | length }})</h4>
<div class="section">
<div class="section">
<ul>
{% for obj in objects[key] %}
<li>{{obj["display_name"]}}</li>
{% endfor %}
</ul>
</div>
</div>
{% endif %}
{% endmacro %}

Expand All @@ -120,18 +120,18 @@
{{ display_basic_changes("created", changes, "#009900") }}
{% if "modified" in changes %}
<h4 style="color: blue">MODIFIED ({{ changes["modified"] | length }})</h4>
<div class="section">
<div class="section">
{% for obj in changes["modified"] %}
<p><b>{{ obj["display_name"] }}</b></p>
<div class="section">
<div class="section">
<ul>
{% for change in obj["attributes"] %}
<li><b>{{ change }}</b>:
<li><b>{{ change }}</b>:
{% if "diff" in obj["attributes"][change] %}
{{ obj["attributes"][change]["diff"] }}
{% else %}
{{ obj["attributes"][change]["previous"] }} -> {{ obj["attributes"][change]["current"] }}
{% endif %}
{% endif %}
</li>
{% endfor %}
</ul>
Expand All @@ -144,7 +144,7 @@
{{ display_basic_changes("deleted", changes, "red") }}
</div>
{% endmacro %}

<h1>Object Changes {{ pretty_stats(data["objects"].stats) }}</h1>
<p><b>Disclaimer:</b> current version of model comparison engine uses a selected list of objects of interest and will not report object changes for object types that were considered as "out of scope". For the objects of interest however we can assure completness and correctness of comparison.</p>
<div class="section">
Expand All @@ -153,7 +153,7 @@
{% set layer_data = data["objects"][layer] %}
{% if layer_data and layer_data.stats %}
<h2>{{LAYER[layer]}} {{ pretty_stats(layer_data.stats) }}</h2>


<div class="section">
{% for obj_type in data["objects"][layer] if obj_type != "stats" %}
Expand Down Expand Up @@ -188,4 +188,4 @@
{% endfor %}
</div>
</body>
</html>
</html>

0 comments on commit c58bad2

Please sign in to comment.