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