Passed
Pull Request — master (#177)
by Jan
04:08
created

Graph._get_html_body()   A

Complexity

Conditions 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 11
nop 2
dl 0
loc 14
ccs 2
cts 2
cp 1
crap 1
rs 9.85
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
import lxml.html
12 1
from lxml import etree
13 1
from lxml.builder import E, ElementMaker
14
15
16 1
class Graph():
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
17
18 1
    def __init__(self, parts, verbose, all_in_one):
19 1
        self.parts = parts
20 1
        self.verbose = verbose
21 1
        self.all_in_one = all_in_one
22 1
        self.html_head = self._get_html_head()
23 1
        self.script = self._get_part('script.js')
24 1
        self.search_bar = self._get_search_bar()
25
26 1
    def save_html(self, dict_oval_trees, src):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
27 1
        with open(src, "wb+") as data_file:
28 1
            data_file.writelines(self._get_html(dict_oval_trees))
29 1
        self.print_output_message(src, list(dict_oval_trees.keys()))
30
31 1
    def _get_html(self, dict_of_rules):
32 1
        maker = ElementMaker()
33 1
        html = maker.html(
0 ignored issues
show
Bug introduced by
The Instance of ElementMaker does not seem to have a member named html.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
34
            self.html_head,
35
            self._get_html_body(dict_of_rules))
36 1
        result = etree.tostring(
37
            html,
38
            xml_declaration=True,
39
            doctype=('<!DOCTYPE html>'),
40
            encoding='utf-8',
41
            standalone=False,
42
            with_tail=False,
43
            method='html',
44
            pretty_print=True)
45 1
        return BytesIO(result)
46
47 1
    def _get_html_head(self):
48 1
        return E.head(
49
            E.meta(charset="utf-8"),
50
            E.title("OVAL TREE"),
51
            E.style(self._get_part('css.txt')),
52
            E.style(self._get_part('bootstrapStyle.txt')),
53
            E.style(self._get_part('jsTreeStyle.txt')),
54
            E.script(self._get_part('jQueryScript.txt')),
55
            E.script(self._get_part('bootstrapScript.txt')),
56
            E.script(self._get_part('jsTreeScript.txt')),
57
        )
58
59 1
    def _get_search_bar(self):
60 1
        if self.all_in_one:
61
            return E.div({'class': 'search'},
62
                         E.div({'class': 'form-group has-feedback has-search'},
63
                               E.span(
64
                                   {'class': 'glyphicon glyphicon-search form-control-feedback'}),
65
                               E.input({'id': 'Search',
66
                                        'onkeyup': 'search()',
67
                                        'class': 'form-control',
68
                                        'type': 'text',
69
                                        'placeholder': 'Search rule'})))
70 1
        return E.div()
71
72 1
    def _get_html_body(self, dict_of_rules):
73 1
        return E.body(
74
            E.script(self._get_script_graph_data(dict_of_rules)),
75
            E.div(
76
                self.search_bar,
77
                self._get_titles_and_places_for_graph(dict_of_rules),
78
            ),
79
            E.div({'id': 'modal', 'class': 'modal'},
80
                  E.div({'class': 'modal-content'},
81
                        E.span({'id': 'close', 'class': 'close'}, '×'),
82
                        E.div({'id': 'content'}),
83
                        )
84
                  ),
85
            E.script(self.script),
86
        )
87
88 1
    def _get_script_graph_data(self, dict_of_rules):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
89 1
        json_of_graphs = {
90
            re.sub(
91
                r'[\_\-\.]',
92
                '',
93
                k): v for k,
94
            v in dict_of_rules.items()}
95 1
        return ("var data_of_tree = " +
96
                str(json.dumps(json_of_graphs, sort_keys=False, indent=4)) +
97
                ";")
98
99 1
    def _get_titles_and_places_for_graph(self, dict_of_rules):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
100 1
        out = '<section id="selection-content"><div id="graphs">'
101 1
        for rule in dict_of_rules.keys():
102 1
            out += ('<div class="target"><h1>' +
103
                    rule +
104
                    '</h1><div id="' +
105
                    re.sub(r'[\_\-\.]', '', rule) +
106
                    '"></div></div>')
107 1
        out += '</div>'
108 1
        return lxml.html.fromstring(out)
109
110 1
    def _get_part(self, part):
111 1
        out = ''
112 1
        with open(os.path.join(self.parts, part), "r") as data_file:
113 1
            for line in data_file.readlines():
114 1
                out += line
115 1
        return out
116
117 1
    def print_output_message(self, src, rules):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
118 1
        if not self.verbose:
119 1
            return
120 1
        if len(rules) > 1:
121
            rule_names = "\n"
122
            for rule in rules:
123
                rule_names += rule + '\n'
124
            print('Rules "{}" done!'.format(rule_names), file=sys.stderr)
125
        else:
126 1
            print('Rule "{}" done!'.format(rules.pop()), file=sys.stderr)
127
        print('Result is saved:"{}"'.format(src), file=sys.stderr)
128