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