1
|
1 |
|
import os |
|
|
|
|
2
|
1 |
|
import subprocess |
3
|
1 |
|
import tempfile |
4
|
1 |
|
import webbrowser |
5
|
|
|
|
6
|
1 |
|
from .._builder_html_graph import BuilderHtmlGraph |
7
|
1 |
|
from ..exceptions import NotChecked |
8
|
1 |
|
from .client import Client |
9
|
|
|
|
10
|
|
|
|
11
|
1 |
|
class ClientHtmlOutput(Client): |
|
|
|
|
12
|
1 |
|
def __init__(self, args): |
13
|
1 |
|
super().__init__(args) |
14
|
1 |
|
self.out = self.arg.output |
15
|
1 |
|
self.part = self.get_src('../parts') |
16
|
1 |
|
if hasattr(self.arg, 'all_in_one'): |
17
|
1 |
|
self.all_in_one = self.arg.all_in_one |
18
|
1 |
|
self.all_rules = True if self.all_in_one else self.arg.all |
19
|
1 |
|
self.html_builder = BuilderHtmlGraph(self.part, self.arg.verbose, self.all_in_one) |
20
|
1 |
|
self.display_html = True if self.out is None else self.arg.display |
21
|
1 |
|
self.web_browsers = [] |
22
|
|
|
|
23
|
1 |
|
@staticmethod |
24
|
|
|
def get_src(src): |
|
|
|
|
25
|
1 |
|
_dir = os.path.dirname(os.path.realpath(__file__)) |
26
|
1 |
|
return str(os.path.join(_dir, src)) |
27
|
|
|
|
28
|
1 |
|
@staticmethod |
29
|
|
|
def get_start_of_file_name(): |
|
|
|
|
30
|
1 |
|
return 'graph-of-' |
31
|
|
|
|
32
|
1 |
|
def prepare_data(self, rules): |
|
|
|
|
33
|
1 |
|
out_src = [] |
34
|
1 |
|
oval_tree_dict = dict() |
35
|
1 |
|
self._prepare_data(rules, oval_tree_dict, out_src) |
36
|
1 |
|
self.open_results_in_web_browser(out_src) |
37
|
1 |
|
return out_src |
38
|
|
|
|
39
|
1 |
|
def _put_to_dict_oval_trees(self, dict_oval_trees, rule): |
40
|
|
|
""" |
41
|
|
|
The function inserts into dict_oval_trees a dictionary from |
42
|
|
|
the rule with the key as the rule id. |
43
|
|
|
""" |
44
|
|
|
raise NotImplementedError |
45
|
|
|
|
46
|
1 |
|
def _prepare_data(self, rules, dict_oval_trees, out_src): |
47
|
1 |
|
for rule in rules['rules']: |
48
|
1 |
|
try: |
49
|
1 |
|
self._put_to_dict_oval_trees(dict_oval_trees, rule) |
50
|
1 |
|
if not self.all_in_one: |
51
|
1 |
|
self._build_and_save_html( |
52
|
|
|
dict_oval_trees, |
53
|
|
|
self._get_src_for_one_graph(rule), |
54
|
|
|
dict(rules=[rule]), |
55
|
|
|
out_src |
56
|
|
|
) |
57
|
1 |
|
dict_oval_trees = {} |
58
|
1 |
|
except NotChecked as error: |
59
|
|
|
start_red_color = '\033[91m' |
60
|
|
|
end_red_color = '\033[0m' |
61
|
|
|
print(start_red_color + str(error) + end_red_color) |
62
|
1 |
|
if self.all_in_one: |
63
|
|
|
self._build_and_save_html( |
64
|
|
|
dict_oval_trees, self.get_save_src( |
65
|
|
|
'rules' + self._get_date()), rules, out_src) |
66
|
|
|
|
67
|
1 |
|
def _build_and_save_html(self, dict_oval_trees, src, rules, out_src): |
68
|
1 |
|
self.html_builder.save_html(dict_oval_trees, src, rules) |
69
|
1 |
|
out_src.append(src) |
70
|
|
|
|
71
|
1 |
|
def _get_src_for_one_graph(self, rule): |
72
|
1 |
|
return self.get_save_src(rule + self._get_date()) |
73
|
|
|
|
74
|
1 |
|
def get_save_src(self, rule): |
|
|
|
|
75
|
1 |
|
if self.out is not None: |
76
|
1 |
|
os.makedirs(self.out, exist_ok=True) |
77
|
1 |
|
return os.path.join( |
78
|
|
|
self.out, |
79
|
|
|
self.get_start_of_file_name() + rule + '.html') |
80
|
1 |
|
return os.path.join( |
81
|
|
|
tempfile.gettempdir(), |
82
|
|
|
self.get_start_of_file_name() + rule + '.html') |
83
|
|
|
|
84
|
1 |
|
def open_results_in_web_browser(self, paths_to_results): |
|
|
|
|
85
|
1 |
|
if self.display_html: |
86
|
1 |
|
try: |
87
|
1 |
|
self.web_browsers.append( |
88
|
|
|
subprocess.Popen(["firefox", *paths_to_results])) |
89
|
|
|
except subprocess.CalledProcessError: |
90
|
|
|
default_web_browser_name = webbrowser.get().name |
91
|
|
|
self.web_browsers.append( |
92
|
|
|
subprocess.Popen([default_web_browser_name, *paths_to_results])) |
93
|
|
|
|
94
|
1 |
|
def kill_web_browsers(self): |
|
|
|
|
95
|
1 |
|
for web_browser in self.web_browsers: |
96
|
1 |
|
web_browser.kill() |
97
|
|
|
|
98
|
1 |
|
@staticmethod |
99
|
|
|
def arg_all_in_one(parser): |
|
|
|
|
100
|
1 |
|
parser.add_argument( |
101
|
|
|
'-i', |
102
|
|
|
'--all-in-one', |
103
|
|
|
action="store_true", |
104
|
|
|
default=False, |
105
|
|
|
help="Processes all rules into one file.") |
106
|
|
|
|
107
|
1 |
|
@staticmethod |
108
|
|
|
def arg_display(parser): |
|
|
|
|
109
|
1 |
|
parser.add_argument( |
110
|
|
|
'-d', |
111
|
|
|
'--display', |
112
|
|
|
action="store_true", |
113
|
|
|
default=False, |
114
|
|
|
help="Enables opening a web browser with a graph, when is used --output.") |
115
|
|
|
|