-
Notifications
You must be signed in to change notification settings - Fork 21
/
report.py
117 lines (98 loc) · 3.06 KB
/
report.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import os
import sys
import time
# HTML templates
top = """<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.collapsible {
background-color: #777;
color: white;
cursor: pointer;
padding: 18px;
width: 100%;
border: none;
text-align: left;
outline: none;
font-size: 15px;
}
.active, .collapsible:hover {
background-color: #555;
}
.collapsible:after {
content: '+';
color: white;
font-weight: bold;
float: right;
margin-left: 5px;
}
.active:after {
content: "-";
}
.content {
padding: 0 18px;
max-height: 0;
overflow: hidden;
transition: max-height 0.2s ease-out;
background-color: #f1f1f1;
}
</style>
</head>
<body>"""
title = """<h2>pyCobaltHound report - """
button = """<button type="button" class="collapsible">{query_name}</button>
<div class="content">
<ul>"""
bot = """<script>
var coll = document.getElementsByClassName("collapsible");
var i;
for (i = 0; i < coll.length; i++) {
coll[i].addEventListener("click", function() {
this.classList.toggle("active");
var content = this.nextElementSibling;
if (content.style.maxHeight){
content.style.maxHeight = null;
} else {
content.style.maxHeight = content.scrollHeight + "px";
}
});
}
</script>
</body>
</html>"""
def parse_results(user_results, computer_results):
html = '\n'
if all(len(result['result']) == 0 for result in user_results) == False:
html = html + "<h3>User results</h3>" + '\n'
for result in user_results:
if len(result['result']) != 0:
html = html + button.format(query_name=(result['report'].format(number=len(result['result'])))) + '\n'
for object in result['result']:
html = html + '<li>' + object + '</li>' + '\n'
html = html + '</ul>' + '\n' + '</div>' + '\n'
if all(len(result['result']) == 0 for result in computer_results) == False:
html = html + "<h3>Computer results</h3>" + '\n'
for result in computer_results:
if len(result['result']) != 0:
html = html + button.format(query_name=(result['report'].format(number=len(result['result'])))) + '\n'
for object in result['result']:
html = html + '<li>' + object + '</li>' + '\n'
html = html + '</ul>' + '\n' + '</div>' + '\n'
return html
def generate_html_report(user_results, computer_results):
if all(len(result['result']) == 0 for result in user_results) == False or all(len(result['result']) == 0 for result in computer_results) == False:
inner_html = parse_results(user_results, computer_results)
report = top + title + time.strftime("%Y-%m-%d %H:%M:%S") + '</h3>' + inner_html + bot
return report
def generate_report(user_results, computer_results):
reportdir = os.path.realpath(os.path.dirname(__file__)) + '/reports'
if os.path.isdir(reportdir) == False:
os.makedirs(reportdir)
reportdest = reportdir + "/pycobalthound-report-" + time.strftime("%Y%m%d-%H%M%S") + ".html"
report = generate_html_report(user_results, computer_results)
f = open(reportdest, "w")
f.write(report)
f.close()
return reportdest