| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# This program is free software; you can redistribute it and/or modify |
| 3 |
# it under the terms of the GNU General Public License as published by |
| 4 |
# the Free Software Foundation; either version 2 of the License, or |
| 5 |
# (at your option) any later version. |
| 6 |
# |
| 7 |
# This program is distributed in the hope that it will be useful, |
| 8 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 |
# GNU General Public License for more details. |
| 11 |
# |
| 12 |
# You should have received a copy of the GNU General Public License |
| 13 |
# along with this program; if not, write to the Free Software |
| 14 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 15 |
# MA 02110-1301, USA. |
| 16 |
# |
| 17 |
# Author: Mauro Soria |
| 18 |
|
| 19 |
import json |
| 20 |
import os |
| 21 |
|
| 22 |
from jinja2 import Environment, FileSystemLoader |
| 23 |
|
| 24 |
from lib.core.decorators import locked |
| 25 |
from lib.core.settings import COMMAND, START_TIME |
| 26 |
from lib.report.factory import BaseReport, FileReportMixin |
| 27 |
|
| 28 |
|
| 29 |
class HTMLReport(FileReportMixin, BaseReport): |
| 30 |
__format__ = "html" |
| 31 |
__extension__ = "html" |
| 32 |
|
| 33 |
def new(self): |
| 34 |
return self.generate([]) |
| 35 |
|
| 36 |
def parse(self, file): |
| 37 |
with open(file) as fh: |
| 38 |
while 1: |
| 39 |
line = fh.readline() |
| 40 |
# Gotta be the worst way to parse it but I don't know a better way:P |
| 41 |
if line.startswith(" resources: "): |
| 42 |
return json.loads(line[19:-2]) |
| 43 |
|
| 44 |
@locked |
| 45 |
def save(self, file, result): |
| 46 |
results = self.parse(file) |
| 47 |
results.append({ |
| 48 |
"url": result.url, |
| 49 |
"status": result.status, |
| 50 |
"contentLength": result.length, |
| 51 |
"contentType": result.type, |
| 52 |
"redirect": result.redirect, |
| 53 |
}) |
| 54 |
self.write(file, self.generate(results)) |
| 55 |
|
| 56 |
def generate(self, results): |
| 57 |
file_loader = FileSystemLoader( |
| 58 |
os.path.dirname(os.path.realpath(__file__)) + "/templates/" |
| 59 |
) |
| 60 |
env = Environment(loader=file_loader) |
| 61 |
template = env.get_template("html_report_template.html") |
| 62 |
return template.render( |
| 63 |
metadata={"command": COMMAND, "date": START_TIME}, |
| 64 |
results=results, |
| 65 |
) |