Passed
Pull Request — master (#177)
by Jan
05:25 queued 01:22
created

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

Complexity

Conditions 1

Size

Total Lines 7
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

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