|
1
|
1 |
|
import json |
|
2
|
1 |
|
import os |
|
3
|
1 |
|
import re |
|
4
|
1 |
|
import sys |
|
5
|
1 |
|
from io import BytesIO |
|
6
|
|
|
|
|
7
|
1 |
|
from lxml import etree |
|
8
|
1 |
|
from lxml.builder import E |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
1 |
|
class Graph(): |
|
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): |
|
22
|
1 |
|
with open(src, "wb+") as data_file: |
|
23
|
1 |
|
data_file.writelines(self._get_html(dict_oval_trees)) |
|
24
|
1 |
|
self.print_output_message(src, list(dict_oval_trees.keys())) |
|
25
|
|
|
|
|
26
|
1 |
|
def _get_html(self, dict_of_rules): |
|
27
|
1 |
|
html = E.html( |
|
28
|
|
|
self.html_head, |
|
29
|
|
|
self._get_html_body(dict_of_rules)) |
|
30
|
1 |
|
result = etree.tostring( |
|
31
|
|
|
html, |
|
32
|
|
|
xml_declaration=True, |
|
33
|
|
|
doctype=('<!DOCTYPE html>'), |
|
34
|
|
|
encoding='utf-8', |
|
35
|
|
|
standalone=False, |
|
36
|
|
|
with_tail=False, |
|
37
|
|
|
method='html', |
|
38
|
|
|
pretty_print=True) |
|
39
|
1 |
|
return BytesIO(result) |
|
40
|
|
|
|
|
41
|
1 |
|
def _get_html_head(self): |
|
42
|
1 |
|
return E.head( |
|
43
|
|
|
E.meta(charset="utf-8"), |
|
44
|
|
|
E.title("OVAL TREE"), |
|
45
|
|
|
E.style(self._get_part('css.txt')), |
|
46
|
|
|
E.style(self._get_part('bootstrapStyle.txt')), |
|
47
|
|
|
E.style(self._get_part('jsTreeStyle.txt')), |
|
48
|
|
|
E.script(self._get_part('jQueryScript.txt')), |
|
49
|
|
|
E.script(self._get_part('bootstrapScript.txt')), |
|
50
|
|
|
E.script(self._get_part('jsTreeScript.txt')), |
|
51
|
|
|
) |
|
52
|
|
|
|
|
53
|
1 |
|
def _get_search_bar(self): |
|
54
|
1 |
|
if self.all_in_one: |
|
55
|
|
|
return E.div({'class': 'search'}, |
|
56
|
|
|
E.div({'class': 'form-group has-feedback has-search'}, |
|
57
|
|
|
E.span( |
|
58
|
|
|
{'class': 'glyphicon glyphicon-search form-control-feedback'}), |
|
59
|
|
|
E.input({'id': 'Search', |
|
60
|
|
|
'onkeyup': 'search()', |
|
61
|
|
|
'class': 'form-control', |
|
62
|
|
|
'type': 'text', |
|
63
|
|
|
'placeholder': 'Search rule'}))) |
|
64
|
1 |
|
return E.div() |
|
65
|
|
|
|
|
66
|
1 |
|
def _get_html_body(self, dict_of_rules): |
|
67
|
1 |
|
return E.body( |
|
68
|
|
|
E.script(self._get_data_of_graphs(dict_of_rules)), |
|
69
|
|
|
E.div( |
|
70
|
|
|
self.search_bar, |
|
71
|
|
|
self._get_titles_and_places_for_graph(dict_of_rules), |
|
72
|
|
|
), |
|
73
|
|
|
E.div({'id': 'modal', 'class': 'modal'}, |
|
74
|
|
|
E.div({'class': 'modal-content'}, |
|
75
|
|
|
E.span({'id': 'close', 'class': 'close'}, '×'), |
|
76
|
|
|
E.div({'id': 'content'}), |
|
77
|
|
|
) |
|
78
|
|
|
), |
|
79
|
|
|
E.script(self.script), |
|
80
|
|
|
) |
|
81
|
|
|
|
|
82
|
1 |
|
@staticmethod |
|
83
|
|
|
def _remove_unfit_chars(string): |
|
84
|
1 |
|
return re.sub(r'[\_\-\.]', '', string) |
|
85
|
|
|
|
|
86
|
1 |
|
def _get_data_of_graphs(self, dict_of_rules): |
|
87
|
1 |
|
json_of_graphs = {self._remove_unfit_chars(key): value |
|
88
|
|
|
for key, value in dict_of_rules.items()} |
|
89
|
1 |
|
data = str(json.dumps(json_of_graphs)) |
|
90
|
1 |
|
return "var data_of_tree = " + data + ";" |
|
91
|
|
|
|
|
92
|
1 |
|
def _get_titles_and_places_for_graph(self, dict_of_rules): |
|
93
|
1 |
|
rules_html = E.div({'id': 'graphs'}) |
|
94
|
1 |
|
for rule in dict_of_rules.keys(): |
|
95
|
1 |
|
rule_h1 = E.h1(rule) |
|
96
|
1 |
|
rule_graf_place = E.div({'id': self._remove_unfit_chars(rule)}) |
|
97
|
1 |
|
rule_place = E.div({'class': 'target'}, rule_h1, rule_graf_place) |
|
98
|
1 |
|
rules_html.append(rule_place) |
|
99
|
1 |
|
return E.selection({'id': 'selection-content'}, rules_html) |
|
100
|
|
|
|
|
101
|
1 |
|
def _get_part(self, part): |
|
102
|
1 |
|
out = '' |
|
103
|
1 |
|
with open(os.path.join(self.parts, part), "r") as data_file: |
|
104
|
1 |
|
for line in data_file.readlines(): |
|
105
|
1 |
|
out += line |
|
106
|
1 |
|
return out |
|
107
|
|
|
|
|
108
|
1 |
|
def print_output_message(self, src, rules): |
|
109
|
1 |
|
if not self.verbose: |
|
110
|
1 |
|
return |
|
111
|
1 |
|
if len(rules) > 1: |
|
112
|
|
|
rule_names = "\n" |
|
113
|
|
|
for rule in rules: |
|
114
|
|
|
rule_names += rule + '\n' |
|
115
|
|
|
print('Rules "{}" done!'.format(rule_names), file=sys.stderr) |
|
116
|
|
|
else: |
|
117
|
1 |
|
print('Rule "{}" done!'.format(rules.pop()), file=sys.stderr) |
|
118
|
|
|
print('Result is saved:"{}"'.format(src), file=sys.stderr) |
|
119
|
|
|
|