forked from linkedin/qark
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreport.py
More file actions
73 lines (52 loc) · 2.82 KB
/
report.py
File metadata and controls
73 lines (52 loc) · 2.82 KB
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
from __future__ import absolute_import
from os import path
from jinja2 import Environment, PackageLoader, select_autoescape, Template
from qark.issue import (Issue, Severity, issue_json) # noqa:F401 These are expected to be used later.
from qark.utils import create_directories_to_path
DEFAULT_REPORT_PATH = path.join(path.dirname(path.realpath(__file__)), 'report', '')
jinja_env = Environment(
loader=PackageLoader('qark', 'templates'),
autoescape=select_autoescape(['html', 'xml'])
)
jinja_env.filters['issue_json'] = issue_json
class Report(object):
"""An object to store issues against and to generate reports in different formats.
There is one instance created per QARK run and it uses a classic Singleton pattern
to make it easy to get a reference to that instance anywhere in QARK.
"""
# The one instance to rule them all
# http://python-3-patterns-idioms-test.readthedocs.io/en/latest/Singleton.html#the-singleton
__instance = None
def __new__(cls, issues=None, report_path=None, keep_report=False, report_name = None):
if Report.__instance is None:
Report.__instance = object.__new__(cls)
return Report.__instance
def __init__(self, issues=None, report_path=None, keep_report=False, report_name = None):
"""This will give you an instance of a report, with a default report path which is local
to where QARK is on the file system.
:param report_path: The path to the report directory where all generated report files will be written.
:type report_path: str or None
"""
self.issues = issues if issues else []
self.report_path = report_path or DEFAULT_REPORT_PATH
self.keep_report = keep_report
def generate(self, file_type='html', template_file=None, report_name = 'report'):
"""This method uses Jinja2 to generate a standalone HTML version of the report.
:param str file_type: The type of file for the report. Defaults to 'html'.
:param str template_file: The path to an optional template file to override the default.
:return: Path to the written report
:rtype: str
"""
create_directories_to_path(self.report_path)
full_report_path = path.join(self.report_path, '{report_name}_QARK.{file_type}'.format(report_name = report_name, file_type=file_type))
open_flag = 'w'
if self.keep_report:
open_flag = 'a'
with open(full_report_path, mode=open_flag) as report_file:
if not template_file:
template = jinja_env.get_template('{file_type}_report.jinja'.format(file_type=file_type))
else:
template = Template(template_file)
report_file.write(template.render(issues=list(self.issues)))
report_file.write('\n')
return full_report_path