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

oval_graph._builder_html_graph   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Test Coverage

Coverage 96.55%

Importance

Changes 0
Metric Value
eloc 106
dl 0
loc 125
ccs 56
cts 58
cp 0.9655
rs 10
c 0
b 0
f 0
wmc 19

12 Methods

Rating   Name   Duplication   Size   Complexity  
A BuilderHtmlGraph._get_part() 0 6 3
A BuilderHtmlGraph._get_script_graph_data() 0 10 1
A BuilderHtmlGraph._get_html_body() 0 14 1
A BuilderHtmlGraph._get_titles_and_places_for_graph() 0 10 2
A BuilderHtmlGraph._get_search_bar() 0 12 2
A BuilderHtmlGraph.save_html_report() 0 3 2
A BuilderHtmlGraph._format_rules_output() 0 5 2
A BuilderHtmlGraph.save_html() 0 3 1
A BuilderHtmlGraph.__init__() 0 7 1
A BuilderHtmlGraph._get_html() 0 15 1
A BuilderHtmlGraph._get_html_head() 0 10 1
A BuilderHtmlGraph.print_output_message() 0 3 2
1 1
import json
2 1
import os
3 1
import re
4 1
import sys
5 1
from io import BytesIO
6
7 1
import lxml.html
8 1
from lxml import etree
9 1
from lxml.builder import E, ElementMaker
10
11
12 1
class BuilderHtmlGraph():
13
14 1
    def __init__(self, parts, verbose, all_in_one):
15 1
        self.parts = parts
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, rules):
23 1
        self.save_html_report(dict_oval_trees, src)
24 1
        self.print_output_message(src, self._format_rules_output(rules))
25
26 1
    def save_html_report(self, dict_of_rules, src):
27 1
        with open(src, "wb+") as data_file:
28 1
            data_file.writelines(self._get_html(dict_of_rules))
29
30 1
    def _get_html(self, dict_of_rules):
31 1
        maker = ElementMaker()
32 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...
33
            self.html_head,
34
            self._get_html_body(dict_of_rules))
35 1
        result = etree.tostring(
36
            html,
37
            xml_declaration=True,
38
            doctype=('<!DOCTYPE html>'),
39
            encoding='utf-8',
40
            standalone=False,
41
            with_tail=False,
42
            method='html',
43
            pretty_print=True)
44 1
        return BytesIO(result)
45
46 1
    def _get_html_head(self):
47 1
        return E.head(
48
            E.meta(charset="utf-8"),
49
            E.title("OVAL TREE"),
50
            E.style(self._get_part('css.txt')),
51
            E.style(self._get_part('bootstrapStyle.txt')),
52
            E.style(self._get_part('jsTreeStyle.txt')),
53
            E.script(self._get_part('jQueryScript.txt')),
54
            E.script(self._get_part('bootstrapScript.txt')),
55
            E.script(self._get_part('jsTreeScript.txt')),
56
        )
57
58 1
    def _get_search_bar(self):
59 1
        if self.all_in_one:
60
            return E.div({'class': 'search'},
61
                         E.div({'class': 'form-group has-feedback has-search'},
62
                               E.span(
63
                                   {'class': 'glyphicon glyphicon-search form-control-feedback'}),
64
                               E.input({'id': 'Search',
65
                                        'onkeyup': 'search()',
66
                                        'class': 'form-control',
67
                                        'type': 'text',
68
                                        'placeholder': 'Search rule'})))
69 1
        return E.div()
70
71 1
    def _get_html_body(self, dict_of_rules):
72 1
        return E.body(
73
            E.script(self._get_script_graph_data(dict_of_rules)),
74
            E.div(
75
                self.search_bar,
76
                self._get_titles_and_places_for_graph(dict_of_rules),
77
            ),
78
            E.div({'id': 'modal', 'class': 'modal'},
79
                  E.div({'class': 'modal-content'},
80
                        E.span({'id': 'close', 'class': 'close'}, '×'),
81
                        E.div({'id': 'content'}),
82
                        )
83
                  ),
84
            E.script(self.script),
85
        )
86
87 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...
88 1
        json_of_graphs = {
89
            re.sub(
90
                r'[\_\-\.]',
91
                '',
92
                k): v for k,
93
            v in dict_of_rules.items()}
94 1
        return ("var data_of_tree = " +
95
                str(json.dumps(json_of_graphs, sort_keys=False, indent=4)) +
96
                ";")
97
98 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...
99 1
        out = '<section id="selection-content"><div id="graphs">'
100 1
        for rule in dict_of_rules.keys():
101 1
            out += ('<div class="target"><h1>' +
102
                    rule +
103
                    '</h1><div id="' +
104
                    re.sub(r'[\_\-\.]', '', rule) +
105
                    '"></div></div>')
106 1
        out += '</div>'
107 1
        return lxml.html.fromstring(out)
108
109 1
    def _get_part(self, part):
110 1
        out = ''
111 1
        with open(os.path.join(self.parts, part), "r") as data_file:
112 1
            for line in data_file.readlines():
113 1
                out += line
114 1
        return out
115
116 1
    def print_output_message(self, src, rule):
0 ignored issues
show
Unused Code introduced by
The argument src seems to be unused.
Loading history...
117 1
        if self.verbose:
118
            print('Rule(s) "{}" done!'.format(rule), file=sys.stderr)
119
120 1
    def _format_rules_output(self, 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...
121 1
        out = ''
122 1
        for rule in rules['rules']:
123 1
            out += rule + '\n'
124
        return out
125