-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import click | ||
import yaml | ||
from jinja2 import Environment, FileSystemLoader | ||
from diff_match_patch import diff_match_patch | ||
|
||
|
||
def html_constructor(loader, node): | ||
return str(node.value) | ||
|
||
|
||
yaml.add_constructor("!html", html_constructor, Loader=yaml.FullLoader) | ||
|
||
|
||
def generate_diff(previous, current): | ||
dmp = diff_match_patch() | ||
diff = dmp.diff_main("\n".join(previous), "\n".join(current)) | ||
dmp.diff_cleanupSemantic(diff) | ||
return dmp.diff_prettyHtml(diff) | ||
|
||
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". | ||
""" | ||
updates = {} | ||
for key, value in data.items(): | ||
if key in ["name", "description"] \ | ||
and isinstance(value, dict) \ | ||
and "previous" in value \ | ||
and "current" in value: | ||
diff = generate_diff( | ||
value["previous"].splitlines(), | ||
value["current"].splitlines() | ||
) | ||
updates[key] = {"diff": diff} | ||
elif isinstance(value, list): | ||
for item in value: | ||
traverse_and_diff(item) | ||
elif isinstance(value, dict): | ||
traverse_and_diff(value) | ||
for key, value in updates.items(): | ||
data[key].update(value) | ||
return data | ||
|
||
|
||
@click.command() | ||
@click.argument("filename") | ||
def main(filename): | ||
with open(filename) as f: | ||
data = yaml.load(f, Loader=yaml.FullLoader) | ||
env = Environment(loader=FileSystemLoader(".")) | ||
template = env.get_template("report.html.j2") | ||
html = template.render(data=traverse_and_diff(data)) | ||
with open("report.html", "w") as f: | ||
f.write(html) | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<html> | ||
<head> | ||
<style> | ||
body { | ||
font-family: sans-serif; | ||
font-size: 12pt; | ||
color: #333; | ||
margin: 1em; | ||
} | ||
h1 { | ||
font-size: 24pt; | ||
} | ||
h2 { | ||
font-size: 20pt; | ||
} | ||
h3 { | ||
font-size: 16pt; | ||
} | ||
.created-modified-delited { | ||
margin-left: 1em; | ||
} | ||
.section { | ||
margin-left: 1em; | ||
padding-left: 1em; | ||
border-left: 1px solid #ccc; | ||
} | ||
.deleted { | ||
color: red; | ||
} | ||
.created { | ||
color: #00aa00; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<h1>Object Changes</h1> | ||
<div class="section"> | ||
{% set LAYER = {"oa": "Operational Analysis", "sa": "System Analysis", "la": "Logical Architecture", "pa": "Physical Architecture"}%} | ||
{% for layer in ["oa", "sa", "la", "pa"] %} | ||
<h2>{{LAYER[layer]}}</h2> | ||
<div class="section"> | ||
{% for obj_type in data["objects"][layer] %} | ||
{% set obj_type_items = data["objects"][layer][obj_type] %} | ||
<h3>{{obj_type}}</h3> | ||
<div class="created-modified-delited"> | ||
{% if "created" in obj_type_items %} | ||
<h4 style="color: #009900">Created</h4> | ||
<div class="section"> | ||
<ul> | ||
{% for obj in obj_type_items["created"] %} | ||
<li>{{obj["name"]}}</li> | ||
{% endfor %} | ||
</ul> | ||
</div> | ||
{% endif %} | ||
|
||
{% if "modified" in obj_type_items %} | ||
<h4 style="color: blue">Modified</h4> | ||
<div class="section"> | ||
{% for obj in obj_type_items["modified"] %} | ||
<p><b>{{ obj["display_name"] }}</b></p> | ||
<ul> | ||
{% for change in obj["changes"] %} | ||
<li><b>{{ change }}</b>: | ||
{% if "diff" in obj["changes"][change] %} | ||
{{ obj["changes"][change]["diff"] }} | ||
{% else %} | ||
{{ obj["changes"][change]["previous"] }} -> {{ obj["changes"][change]["current"] }} | ||
{% endif %} | ||
</li> | ||
{% endfor %} | ||
</ul> | ||
{% endfor %} | ||
</div> | ||
{% endif %} | ||
|
||
{% if "deleted" in obj_type_items %} | ||
<h4 style="color: red">Deleted</h4> | ||
<div class="section"> | ||
<ul> | ||
{% for obj in obj_type_items["deleted"] %} | ||
<li>{{obj["name"]}}</li> | ||
{% endfor %} | ||
</ul> | ||
</div> | ||
{% endif %} | ||
</div> | ||
{% endfor %} | ||
</div> | ||
{% endfor %} | ||
</div> | ||
</body> | ||
</html> |