/
pretty_format.py
executable file
·73 lines (59 loc) · 2.32 KB
/
pretty_format.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import json
import sys
class ChangeSet:
changes = None
def __init__(self, changes):
self.changes = changes
def action(self):
action = self.changes['ResourceChange']['Action']
color = ""
if self.changes['ResourceChange']['Action'] == "Modify":
color = "<img src=\"https://placehold.it/12/0073bb/0073bb?text=+\" />"
elif self.changes['ResourceChange']['Action'] == "Add":
color = "<img src=\"https://placehold.it/12/1d8102/1d8102?text=+\" />"
elif self.changes['ResourceChange']['Action'] == "Remove":
color = "<img src=\"https://placehold.it/12/d13212/d13212?text=+\" />"
return "%s %s" % (color, action)
def logical_resource_id(self):
return self.changes['ResourceChange']['LogicalResourceId']
def physical_resource_id(self):
if 'PhysicalResourceId' in self.changes['ResourceChange']:
return self.changes['ResourceChange']['PhysicalResourceId']
else:
return "-"
def resource_type(self):
return self.changes['ResourceChange']['ResourceType']
def replacement(self):
if 'Replacement' in self.changes['ResourceChange']:
return self.changes['ResourceChange']['Replacement']
else:
return "-"
def details(self):
arr = []
for d in self.changes['ResourceChange']['Details']:
if d['Target']['Attribute'] != 'Properties':
continue
arr.append("- %s" % d['Target']['Name'])
return "<br>".join(arr)
if __name__ == '__main__':
data = {}
with open("%s.json" % sys.argv[1]) as f:
data = json.load(f)
body = "<h1>Change set</h1><h2>Stack Name: %s</h2><br>" % sys.argv[2]
if len(data['Changes']) > 0:
body += "<table><tr><th>Action </th><th>ID</th><th>Type</th><th>Replacement</th><th>Changed Properties</th></tr>"
for c in data['Changes']:
body += "<tr>"
change_set = ChangeSet(c)
body += "<td>%s</td>" % change_set.action()
body += "<td>%s</td>" % change_set.logical_resource_id()
# cols.append(change_set.physical_resource_id())
body += "<td>%s</td>" % change_set.resource_type()
body += "<td>%s</td>" % change_set.replacement()
body += "<td>%s</td>" % change_set.details()
body += "</tr>"
body += "</table>"
else:
body += "no change."
with open("%s.html" % sys.argv[1], mode='w') as f:
f.write(body)