|
1
|
1 |
|
import os |
|
2
|
1 |
|
import webbrowser |
|
3
|
1 |
|
import json |
|
4
|
1 |
|
import re |
|
5
|
1 |
|
from lxml import etree |
|
6
|
1 |
|
from lxml.builder import ElementMaker, E |
|
7
|
1 |
|
import lxml.html |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
1 |
|
class BuilderHtmlGraph(): |
|
11
|
1 |
|
def __init__(self, parts, off_webbrowser): |
|
12
|
1 |
|
self.parts = parts |
|
13
|
1 |
|
self.off_webbrowser = off_webbrowser |
|
14
|
|
|
|
|
15
|
1 |
|
def save_html_and_open_html( |
|
16
|
|
|
self, dict_oval_trees, src, rules, out): |
|
17
|
1 |
|
self.save_html_report(dict_oval_trees, src) |
|
18
|
1 |
|
self.print_output_message_and_open_web_browser( |
|
19
|
|
|
src, self._format_rules_output(rules), out) |
|
20
|
|
|
|
|
21
|
1 |
|
def save_html_report(self, dict_of_rules, src): |
|
22
|
1 |
|
with open(src, "w+") as data_file: |
|
23
|
1 |
|
data_file.writelines(self._get_html(dict_of_rules)) |
|
24
|
|
|
|
|
25
|
1 |
|
def _get_html(self, dict_of_rules): |
|
26
|
1 |
|
maker = ElementMaker(namespace=None, |
|
27
|
|
|
nsmap={None: "http://www.w3.org/1999/xhtml"}) |
|
28
|
1 |
|
html = maker.html( |
|
29
|
|
|
self._get_html_head(), |
|
30
|
|
|
self._get_html_body(dict_of_rules)) |
|
31
|
1 |
|
result = etree.tostring( |
|
32
|
|
|
html, |
|
33
|
|
|
xml_declaration=True, |
|
34
|
|
|
doctype=('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"' |
|
35
|
|
|
' "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">'), |
|
36
|
|
|
encoding='utf-8', |
|
37
|
|
|
standalone=False, |
|
38
|
|
|
with_tail=False, |
|
39
|
|
|
method='html', |
|
40
|
|
|
pretty_print=True) |
|
41
|
1 |
|
return result.decode('UTF-8') |
|
42
|
|
|
|
|
43
|
1 |
|
def _get_html_head(self): |
|
44
|
1 |
|
return E.head( |
|
45
|
|
|
E.title("OVAL TREE"), |
|
46
|
|
|
E.style(self._get_part('css.txt')), |
|
47
|
|
|
E.style(self._get_part('bootstrapStyle.txt')), |
|
48
|
|
|
E.style(self._get_part('jsTreeStyle.txt')), |
|
49
|
|
|
E.script(self._get_part('jQueryScript.txt')), |
|
50
|
|
|
E.script(self._get_part('bootstrapScript.txt')), |
|
51
|
|
|
E.script(self._get_part('jsTreeScript.txt')), |
|
52
|
|
|
) |
|
53
|
|
|
|
|
54
|
1 |
|
def _get_html_body(self, dict_of_rules): |
|
55
|
1 |
|
return E.body( |
|
56
|
|
|
E.script(self._get_script_graph_data(dict_of_rules)), |
|
57
|
|
|
self._get_titles_and_places_for_graph(dict_of_rules), |
|
58
|
|
|
E.div({'id': 'data'}), |
|
59
|
|
|
E.div({'id': 'modal', 'class': 'modal'}, |
|
60
|
|
|
E.div({'class': 'modal-content'}, |
|
61
|
|
|
E.span({'id': 'close', 'class': 'close'}, '×'), |
|
62
|
|
|
E.div({'id': 'content'}), |
|
63
|
|
|
) |
|
64
|
|
|
), |
|
65
|
|
|
E.script(self._get_part('script.js')), |
|
66
|
|
|
) |
|
67
|
|
|
|
|
68
|
1 |
|
def _get_script_graph_data(self, dict_of_rules): |
|
69
|
1 |
|
json_of_graphs = { |
|
70
|
|
|
re.sub( |
|
71
|
|
|
r'[\_\-\.]', |
|
72
|
|
|
'', |
|
73
|
|
|
k): v for k, |
|
74
|
|
|
v in dict_of_rules.items()} |
|
75
|
1 |
|
return ("var data_of_tree = " + |
|
76
|
|
|
str(json.dumps(json_of_graphs, sort_keys=False, indent=4)) + |
|
77
|
|
|
";") |
|
78
|
|
|
|
|
79
|
1 |
|
def _get_titles_and_places_for_graph(self, dict_of_rules): |
|
80
|
1 |
|
out = '' |
|
81
|
1 |
|
for rule in dict_of_rules.keys(): |
|
82
|
1 |
|
out += ('<h1>' + |
|
83
|
|
|
rule + |
|
84
|
|
|
'</h1><div id="' + |
|
85
|
|
|
re.sub(r'[\_\-\.]', '', rule) + |
|
86
|
|
|
'"></div>') |
|
87
|
1 |
|
return lxml.html.fromstring(out) |
|
88
|
|
|
|
|
89
|
1 |
|
def _get_part(self, part): |
|
90
|
1 |
|
out = '' |
|
91
|
1 |
|
with open(os.path.join(self.parts, part), "r") as data_file: |
|
92
|
1 |
|
for line in data_file.readlines(): |
|
93
|
1 |
|
out += line |
|
94
|
1 |
|
return out |
|
95
|
|
|
|
|
96
|
1 |
|
def print_output_message_and_open_web_browser(self, src, rule, out): |
|
97
|
1 |
|
print('Rule(s) "{}" done!'.format(rule)) |
|
98
|
1 |
|
out.append(src) |
|
99
|
1 |
|
self.open_web_browser(src) |
|
100
|
|
|
|
|
101
|
1 |
|
def open_web_browser(self, src): |
|
102
|
1 |
|
if not self.off_webbrowser: |
|
103
|
|
|
try: |
|
104
|
|
|
webbrowser.get('firefox').open_new_tab(src) |
|
105
|
|
|
except BaseException: |
|
106
|
|
|
webbrowser.open_new_tab(src) |
|
107
|
|
|
|
|
108
|
1 |
|
def _format_rules_output(self, rules): |
|
109
|
1 |
|
out = '' |
|
110
|
1 |
|
for rule in rules['rules']: |
|
111
|
1 |
|
out += rule + '\n' |
|
112
|
|
|
return out |
|
113
|
|
|
|