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 |
|
if self.verbose: |
25
|
|
|
self.print_output_message(src, list(dict_oval_trees.keys())) |
26
|
|
|
|
27
|
1 |
|
def _get_html(self, dict_of_rules): |
28
|
1 |
|
html = E.html( |
29
|
|
|
self.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>'), |
35
|
|
|
encoding='utf-8', |
36
|
|
|
standalone=False, |
37
|
|
|
with_tail=False, |
38
|
|
|
method='html', |
39
|
|
|
pretty_print=True) |
40
|
1 |
|
return BytesIO(result) |
41
|
|
|
|
42
|
1 |
|
def _get_html_head(self): |
43
|
1 |
|
return E.head( |
44
|
|
|
E.meta(charset="utf-8"), |
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_search_bar(self): |
55
|
1 |
|
if self.all_in_one: |
56
|
|
|
return E.div({'class': 'search'}, |
57
|
|
|
E.div({'class': 'form-group has-feedback has-search'}, |
58
|
|
|
E.span( |
59
|
|
|
{'class': 'glyphicon glyphicon-search form-control-feedback'}), |
60
|
|
|
E.input({'id': 'Search', |
61
|
|
|
'onkeyup': 'search()', |
62
|
|
|
'class': 'form-control', |
63
|
|
|
'type': 'text', |
64
|
|
|
'placeholder': 'Search rule'}))) |
65
|
1 |
|
return E.div() |
66
|
|
|
|
67
|
1 |
|
def _get_html_body(self, dict_of_rules): |
68
|
1 |
|
return E.body( |
69
|
|
|
E.script(self._get_data_of_graphs_in_js(dict_of_rules)), |
70
|
|
|
E.div( |
71
|
|
|
self.search_bar, |
72
|
|
|
self._get_titles_and_places_for_graph(dict_of_rules), |
73
|
|
|
), |
74
|
|
|
E.div({'id': 'modal', 'class': 'modal'}, |
75
|
|
|
E.div({'class': 'modal-content'}, |
76
|
|
|
E.span({'id': 'close', 'class': 'close'}, '×'), |
77
|
|
|
E.div({'id': 'content'}), |
78
|
|
|
) |
79
|
|
|
), |
80
|
|
|
E.script(self.script), |
81
|
|
|
) |
82
|
|
|
|
83
|
1 |
|
@staticmethod |
84
|
|
|
def _remove_unfit_chars(string): |
85
|
1 |
|
return re.sub(r'[_\-\.]', '', string) |
86
|
|
|
|
87
|
1 |
|
def _get_data_of_graphs_in_js(self, dict_of_rules): |
88
|
1 |
|
json_of_graphs = {self._remove_unfit_chars(key): value |
89
|
|
|
for key, value in dict_of_rules.items()} |
90
|
1 |
|
data = str(json.dumps(json_of_graphs)) |
91
|
1 |
|
return "var data_of_tree = {};".format(data) |
92
|
|
|
|
93
|
1 |
|
def _get_titles_and_places_for_graph(self, dict_of_rules): |
94
|
1 |
|
rules_html = E.div({'id': 'graphs'}) |
95
|
1 |
|
for rule in dict_of_rules.keys(): |
96
|
1 |
|
rule_id_h1 = E.h1(rule) |
97
|
1 |
|
space_for_graph = E.div({'id': self._remove_unfit_chars(rule)}) |
98
|
1 |
|
space_for_whole_rule = E.div({'class': 'target'}, rule_id_h1, space_for_graph) |
99
|
1 |
|
rules_html.append(space_for_whole_rule) |
100
|
1 |
|
return E.selection({'id': 'selection-content'}, rules_html) |
101
|
|
|
|
102
|
1 |
|
def _get_part(self, part): |
103
|
1 |
|
out = '' |
104
|
1 |
|
with open(os.path.join(self.parts, part), "r") as data_file: |
105
|
1 |
|
out = ''.join(data_file.readlines()) |
106
|
1 |
|
return out |
107
|
|
|
|
108
|
1 |
|
@staticmethod |
109
|
|
|
def print_output_message(src, rules): |
110
|
|
|
if len(rules) > 1: |
111
|
|
|
rule_names = "\n" + "\n".join(rules) |
112
|
|
|
print('Rules "{}" done!'.format(rule_names), file=sys.stderr) |
113
|
|
|
else: |
114
|
|
|
print('Rule "{}" done!'.format(rules.pop()), file=sys.stderr) |
115
|
|
|
print('Result is saved:"{}"'.format(src), file=sys.stderr) |
116
|
|
|
|