Passed
Push — master ( b5a080...903f35 )
by Jan
06:45 queued 01:03
created

oval_graph.html_builder.graph.Graph.save_html()   A

Complexity

Conditions 3

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

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