| 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 |
from urllib.parse import urlparse |
| 20 |
|
| 21 |
from lib.core.data import options |
| 22 |
from lib.core.settings import STANDARD_PORTS, START_TIME |
| 23 |
from lib.report.csv_report import CSVReport |
| 24 |
from lib.report.html_report import HTMLReport |
| 25 |
from lib.report.json_report import JSONReport |
| 26 |
from lib.report.markdown_report import MarkdownReport |
| 27 |
from lib.report.mysql_report import MySQLReport |
| 28 |
from lib.report.plain_text_report import PlainTextReport |
| 29 |
from lib.report.postgresql_report import PostgreSQLReport |
| 30 |
from lib.report.simple_report import SimpleReport |
| 31 |
from lib.report.sqlite_report import SQLiteReport |
| 32 |
from lib.report.xml_report import XMLReport |
| 33 |
|
| 34 |
output_handlers = { |
| 35 |
"simple": (SimpleReport, [options["output_file"]]), |
| 36 |
"plain": (PlainTextReport, [options["output_file"]]), |
| 37 |
"json": (JSONReport, [options["output_file"]]), |
| 38 |
"xml": (XMLReport, [options["output_file"]]), |
| 39 |
"md": (MarkdownReport, [options["output_file"]]), |
| 40 |
"csv": (CSVReport, [options["output_file"]]), |
| 41 |
"html": (HTMLReport, [options["output_file"]]), |
| 42 |
"sqlite": (SQLiteReport, [options["output_file"], options["output_table"]]), |
| 43 |
"mysql": (MySQLReport, [options["mysql_url"], options["output_table"]]), |
| 44 |
"postgresql": (PostgreSQLReport, [options["postgres_url"], options["output_table"]]), |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
class ReportManager: |
| 49 |
def __init__(self, formats): |
| 50 |
self.reports = [] |
| 51 |
|
| 52 |
for format in formats: |
| 53 |
# No output location provided |
| 54 |
if any(not _ for _ in output_handlers[format][1]): |
| 55 |
continue |
| 56 |
self.reports.append((output_handlers[format][0](), output_handlers[format][1])) |
| 57 |
|
| 58 |
def prepare(self, target): |
| 59 |
for reporter, sources in self.reports: |
| 60 |
reporter.initiate( |
| 61 |
*map( |
| 62 |
lambda s: self.format(s, target, reporter), |
| 63 |
sources, |
| 64 |
) |
| 65 |
) |
| 66 |
|
| 67 |
def save(self, result): |
| 68 |
for reporter, sources in self.reports: |
| 69 |
reporter.save( |
| 70 |
*map( |
| 71 |
lambda s: self.format(s, result.url, reporter), |
| 72 |
sources, |
| 73 |
), |
| 74 |
result, |
| 75 |
) |
| 76 |
|
| 77 |
def finish(self): |
| 78 |
for reporter, sources in self.reports: |
| 79 |
reporter.finish() |
| 80 |
|
| 81 |
def format(self, string, target, handler): |
| 82 |
parsed = urlparse(target) |
| 83 |
|
| 84 |
return string.format( |
| 85 |
datetime=START_TIME.replace(" ", "_"), |
| 86 |
date=START_TIME.split()[0], |
| 87 |
host=parsed.hostname, |
| 88 |
scheme=parsed.scheme, |
| 89 |
port=parsed.port or STANDARD_PORTS[parsed.scheme], |
| 90 |
format=handler.__format__, |
| 91 |
extension=handler.__extension__, |
| 92 |
) |