diff --git a/pretty_print.py b/pretty_print.py index 934860a..52b633b 100644 --- a/pretty_print.py +++ b/pretty_print.py @@ -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): @@ -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"{previous['display_name']}{current['display_name']}" @@ -31,7 +35,9 @@ def diff_lists(previous, current): out.append(f"
  • {item}
  • ") elif item["uuid"] in previous: if item["display_name"] != previous[item["uuid"]]["display_name"]: - out.append(f"
  • {diff_objects(previous[item['uuid']], item)}
  • ") + out.append( + f"
  • {diff_objects(previous[item['uuid']], item)}
  • " + ) else: out.append(f"
  • {item['display_name']}
  • ") current = {item["uuid"]: item for item in current} @@ -41,21 +47,23 @@ def diff_lists(previous, current): return "" - 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: @@ -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 @@ -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 @@ -116,5 +131,6 @@ def main(filename): with open("report.html", "w") as f: f.write(html) + if __name__ == "__main__": main() diff --git a/report.html.j2 b/report.html.j2 index ee54608..4afca10 100644 --- a/report.html.j2 +++ b/report.html.j2 @@ -104,13 +104,13 @@ {% macro display_basic_changes(key, objects, color) %} {% if key in objects %}

    {{key | upper}} ({{ objects[key] | length }})

    -
    +
    -
    +
    {% endif %} {% endmacro %} @@ -120,18 +120,18 @@ {{ display_basic_changes("created", changes, "#009900") }} {% if "modified" in changes %}

    MODIFIED ({{ changes["modified"] | length }})

    -
    +
    {% for obj in changes["modified"] %}

    {{ obj["display_name"] }}

    -
    +
      {% for change in obj["attributes"] %} -
    • {{ change }}: +
    • {{ change }}: {% if "diff" in obj["attributes"][change] %} {{ obj["attributes"][change]["diff"] }} {% else %} {{ obj["attributes"][change]["previous"] }} -> {{ obj["attributes"][change]["current"] }} - {% endif %} + {% endif %}
    • {% endfor %}
    @@ -144,7 +144,7 @@ {{ display_basic_changes("deleted", changes, "red") }}
    {% endmacro %} - +

    Object Changes {{ pretty_stats(data["objects"].stats) }}

    Disclaimer: 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.

    @@ -153,7 +153,7 @@ {% set layer_data = data["objects"][layer] %} {% if layer_data and layer_data.stats %}

    {{LAYER[layer]}} {{ pretty_stats(layer_data.stats) }}

    - +
    {% for obj_type in data["objects"][layer] if obj_type != "stats" %} @@ -188,4 +188,4 @@ {% endfor %}
    - \ No newline at end of file +