Test Failed
Pull Request — master (#187)
by Jan
02:35
created

oval_graph.html_builder.graph   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 97
dl 0
loc 116
ccs 0
cts 57
cp 0
rs 10
c 0
b 0
f 0
wmc 17

11 Methods

Rating   Name   Duplication   Size   Complexity  
A Graph.print_output_message() 0 8 2
A Graph._get_titles_and_places_for_graph() 0 8 2
A Graph._get_html_body() 0 14 1
A Graph._get_html() 0 14 1
A Graph._remove_unfit_chars() 0 3 1
A Graph.save_html() 0 5 3
A Graph._get_part() 0 5 2
A Graph.__init__() 0 7 1
A Graph._get_html_head() 0 10 1
A Graph._get_data_of_graphs_in_js() 0 5 1
A Graph._get_search_bar() 0 12 2
1
import json
2
import os
3
import re
4
import sys
5
from io import BytesIO
6
7
from lxml import etree
8
from lxml.builder import E
9
10
11
class Graph():
12
13
    def __init__(self, parts, verbose, all_in_one):
14
        self.parts = parts
15
        self.verbose = verbose
16
        self.all_in_one = all_in_one
17
        self.html_head = self._get_html_head()
18
        self.script = self._get_part('script.js')
19
        self.search_bar = self._get_search_bar()
20
21
    def save_html(self, dict_oval_trees, src):
22
        with open(src, "wb+") as data_file:
23
            data_file.writelines(self._get_html(dict_oval_trees))
24
        if self.verbose:
25
            self.print_output_message(src, list(dict_oval_trees.keys()))
26
27
    def _get_html(self, dict_of_rules):
28
        html = E.html(
29
            self.html_head,
30
            self._get_html_body(dict_of_rules))
31
        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
        return BytesIO(result)
41
42
    def _get_html_head(self):
43
        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
    def _get_search_bar(self):
55
        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
        return E.div()
66
67
    def _get_html_body(self, dict_of_rules):
68
        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
    @staticmethod
84
    def _remove_unfit_chars(string):
85
        return re.sub(r'[_\-\.]', '', string)
86
87
    def _get_data_of_graphs_in_js(self, dict_of_rules):
88
        json_of_graphs = {self._remove_unfit_chars(key): value
89
                          for key, value in dict_of_rules.items()}
90
        data = str(json.dumps(json_of_graphs))
91
        return "var data_of_tree = {};".format(data)
92
93
    def _get_titles_and_places_for_graph(self, dict_of_rules):
94
        rules_html = E.div({'id': 'graphs'})
95
        for rule in dict_of_rules.keys():
96
            rule_id_h1 = E.h1(rule)
97
            space_for_graph = E.div({'id': self._remove_unfit_chars(rule)})
98
            space_for_whole_rule = E.div({'class': 'target'}, rule_id_h1, space_for_graph)
99
            rules_html.append(space_for_whole_rule)
100
        return E.selection({'id': 'selection-content'}, rules_html)
101
102
    def _get_part(self, part):
103
        out = ''
104
        with open(os.path.join(self.parts, part), "r") as data_file:
105
            out = ''.join(data_file.readlines())
106
        return out
107
108
    @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