Passed
Push — master ( d0bc7d...7bae51 )
by Matěj
17:10 queued 12s
created

oval_graph.converter.Converter._show_node()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 2
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1 1
import re
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
2
3 1
from .oval_node import OvalNode
4
5
6 1
class Converter():
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
7 1
    def __init__(self, tree):
8 1
        self.VALUE_TO_BOOTSTRAP_COLOR = {
0 ignored issues
show
Coding Style Naming introduced by
Attribute name "VALUE_TO_BOOTSTRAP_COLOR" doesn't conform to '[a-z_][a-z0-9_]2,30$' pattern ('[a-z_][a-z0-9_]2,30$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
9
            "true": "text-success",
10
            "false": "text-danger",
11
            "error": "text-dark",
12
            "unknown": "text-dark",
13
            "noteval": "text-dark",
14
            "notappl": "text-dark"
15
        }
16
17 1
        self.BOOTSTRAP_COLOR_TO_LABEL_COLOR = {
0 ignored issues
show
Coding Style Naming introduced by
Attribute name "BOOTSTRAP_COLOR_TO_LABEL_COLOR" doesn't conform to '[a-z_][a-z0-9_]2,30$' pattern ('[a-z_][a-z0-9_]2,30$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
18
            "text-success": "label-success",
19
            "text-danger": "label-danger",
20
            "text-dark": "label-default"
21
        }
22
23 1
        self.VALUE_TO_ICON = {
0 ignored issues
show
Coding Style Naming introduced by
Attribute name "VALUE_TO_ICON" doesn't conform to '[a-z_][a-z0-9_]2,30$' pattern ('[a-z_][a-z0-9_]2,30$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
24
            "true": "glyphicon glyphicon-ok text-success",
25
            "false": "glyphicon glyphicon-remove text-danger",
26
            "error": "glyphicon glyphicon-question-sign text-dark",
27
            "unknown": "glyphicon glyphicon-question-sign text-dark",
28
            "noteval": "glyphicon glyphicon-question-sign text-dark",
29
            "notappl": "glyphicon glyphicon-question-sign text-dark"
30
        }
31
32 1
        if isinstance(tree, OvalNode):
33 1
            self.tree = tree
34 1
            self.result = self.tree.evaluate_tree()
35 1
            if self.tree.node_type == 'value':
36 1
                self.result = self.tree.value
37
        else:
38
            raise ValueError(
39
                'This converter can process only trees created from OvalNodes.')
40
41 1
    def _get_node_icon(self):
42 1
        values = self._get_node_style()
43 1
        return dict(
44
            color=self.VALUE_TO_BOOTSTRAP_COLOR[values['test_value']],
45
            icon=self.VALUE_TO_ICON[values['negation_color']],
46
        )
47
48 1
    def get_comment(self):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
49 1
        if self.tree.comment is not None:
50 1
            return str(self.tree.comment)
51 1
        return ""
52
53 1
    def get_tag(self):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
54 1
        if self.tree.tag is not None:
55 1
            return str(self.tree.tag)
56 1
        return ""
57
58 1
    def to_JsTree_dict(self, hide_passing_tests=False):
0 ignored issues
show
Coding Style Naming introduced by
Method name "to_JsTree_dict" doesn't conform to '[a-z_][a-z0-9_]2,30$' pattern ('[a-z_][a-z0-9_]2,30$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
introduced by
Missing function or method docstring
Loading history...
59 1
        icons = self._get_node_icon()
60 1
        label = self._get_label()
61 1
        if self.tree.test_result_details:
62 1
            self.tree.test_result_details['result'] = (
63
                ' <span class="label {color_tag}">{result}</span>'
64
                .format(
65
                    color_tag=self.BOOTSTRAP_COLOR_TO_LABEL_COLOR[icons['color']],
66
                    result=self.result,
67
                ))
68 1
        out = {'text':
69
               '{negation} <strong><span class="{icon}">{label}</span></strong>'
70
               ' <span class="label {color_tag}">{tag}</span>'
71
               ' <span class="label {color_tag}">{result}</span>'
72
               ' <i>{comment}</i>'
73
               .format(negation=str(
74
                   label['negation'] if label['negation'] else ""),
75
                   icon=icons['color'],
76
                   label=label['str'],
77
                   color_tag=self.BOOTSTRAP_COLOR_TO_LABEL_COLOR[icons['color']],
78
                   tag=self.get_tag(),
79
                   result=self._get_not_negate_result(),
80
                   comment=self.get_comment()),
81
               "icon": icons['icon'],
82
               "state": {"opened": self._show_node(hide_passing_tests)},
83
               "info": self.tree.test_result_details,
84
               }
85 1
        if self.tree.children:
86 1
            out['children'] = [Converter(child).to_JsTree_dict(
87
                hide_passing_tests) for child in self.tree.children]
88 1
        return out
89
90 1
    def _get_not_negate_result(self):
91 1
        if self.tree.negation and self.tree.node_type == 'operator' and self.is_bool(
92
                self.result):
93 1
            return self.negate_bool(self.result)
94 1
        return self.result
95
96 1
    def _show_node(self, hide_passing_tests):
97 1
        return not(self.result == 'true' and hide_passing_tests)
98
99 1
    def _get_node_style(self):
100 1
        value = self._get_not_negate_result()
101 1
        out_color = None
102 1
        if self.tree.negation and self.is_bool(value):
103 1
            out_color = self.negate_bool(value)
104
        else:
105 1
            out_color = value
106 1
        return dict(
107
            negation_color=out_color,
108
            test_value=value,
109
        )
110
111 1
    def get_negation_character(self, value):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
112 1
        return ('<strong><span class="' +
113
                self.VALUE_TO_BOOTSTRAP_COLOR[value] +
114
                '">NOT</strong></span>')
115
116 1
    def _get_label(self):
117 1
        out = dict(negation=None, str="")
118 1
        if self.tree.node_type == 'value':
119 1
            out['negation'] = self._get_negation_for_label_of_value()
120 1
            out['str'] = re.sub(
121
                '(oval:ssg-test_|oval:ssg-)|(:def:1|:tst:1)', '', str(self.tree.node_id))
122
        else:
123 1
            if str(self.tree.node_id).startswith('xccdf_org'):
124 1
                out['str'] = re.sub(
125
                    '(xccdf_org.ssgproject.content_)', '', str(
126
                        self.tree.node_id))
127
            else:
128 1
                out['negation'] = self._get_negation_for_label_of_operator()
129 1
                out['str'] = (self.tree.value).upper()
130 1
        return out
131
132 1
    def _get_negation_for_label_of_value(self):
0 ignored issues
show
Coding Style Naming introduced by
Method name "_get_negation_for_label_of_value" doesn't conform to '[a-z_][a-z0-9_]2,30$' pattern ('[a-z_][a-z0-9_]2,30$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Unused Code introduced by
Either all return statements in a function should return an expression, or none of them should.
Loading history...
133 1
        if self.tree.negation and self.is_bool(self.tree.value):
134 1
            return self.get_negation_character(
135
                self.negate_bool(self.tree.value))
136
137 1
    def _get_negation_for_label_of_operator(self):
0 ignored issues
show
Coding Style Naming introduced by
Method name "_get_negation_for_label_of_operator" doesn't conform to '[a-z_][a-z0-9_]2,30$' pattern ('[a-z_][a-z0-9_]2,30$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Unused Code introduced by
Either all return statements in a function should return an expression, or none of them should.
Loading history...
138 1
        if self.tree.negation and self.is_bool(self.result):
139 1
            return self.get_negation_character(self.result)
140
141 1
    def negate_bool(self, value):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
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...
142 1
        values = {
143
            "true": "false",
144
            "false": "true",
145
        }
146 1
        return values[str(value)]
147
148 1
    def is_bool(self, value):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
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...
149
        return value == "true" or value == "false"
0 ignored issues
show
Unused Code introduced by
Consider merging these comparisons with "in" to "value in ('true', 'false')"
Loading history...
150