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