Passed
Pull Request — master (#111)
by Jan
01:49
created

Converter._get_not_negate_result()   A

Complexity

Conditions 4

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 5
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 4
rs 10
c 0
b 0
f 0
1 1
import re
2 1
import uuid
3
4 1
from .oval_node import OvalNode
5
6
7 1
class Converter():
8 1
    def __init__(self, tree):
9 1
        self.VALUE_TO_BOOTSTRAP_COLOR = {
10
            "true": "text-success",
11
            "false": "text-danger",
12
            "error": "text-dark",
13
            "unknown": "text-dark",
14
            "noteval": "text-dark",
15
            "notappl": "text-dark"
16
        }
17
18 1
        self.BOOTSTRAP_COLOR_TO_LABEL_COLOR = {
19
            "text-success": "label-success",
20
            "text-danger": "label-danger",
21
            "text-dark": "label-default"
22
        }
23
24 1
        self.VALUE_TO_ICON = {
25
            "true": "glyphicon glyphicon-ok text-success",
26
            "false": "glyphicon glyphicon-remove text-danger",
27
            "error": "glyphicon glyphicon-question-sign text-dark",
28
            "unknown": "glyphicon glyphicon-question-sign text-dark",
29
            "noteval": "glyphicon glyphicon-question-sign text-dark",
30
            "notappl": "glyphicon glyphicon-question-sign text-dark"
31
        }
32
33 1
        if isinstance(tree, OvalNode):
34 1
            self.tree = tree
35 1
            self.result = self.tree.evaluate_tree()
36 1
            if self.tree.node_type == 'value':
37 1
                self.result = self.tree.value
38
        else:
39
            raise ValueError(
40
                'This converter can process only trees created from OvalNodes.')
41
42 1
    def _get_node_icon(self):
43 1
        values = self._get_node_style()
44 1
        return dict(
45
            color=self.VALUE_TO_BOOTSTRAP_COLOR[values['test_value']],
46
            icon=self.VALUE_TO_ICON[values['negation_color']],
47
        )
48
49 1
    def get_comment(self):
50 1
        if self.tree.comment is not None:
51 1
            return str(self.tree.comment)
52 1
        return ""
53
54 1
    def get_tag(self):
55 1
        if self.tree.tag is not None:
56 1
            return str(self.tree.tag)
57 1
        return ""
58
59 1
    def to_JsTree_dict(self, hide_passing_tests=False):
60 1
        icons = self._get_node_icon()
61 1
        label = self._get_label()
62 1
        if self.tree.test_result_details:
63 1
            self.tree.test_result_details['result'] = (
64
                ' <span class="label {color_tag}">{result}</span>'
65
                .format(
66
                    color_tag=self.BOOTSTRAP_COLOR_TO_LABEL_COLOR[icons['color']],
67
                    result=self.result,
68
                ))
69 1
        out = {'text':
70
               '{negation} <strong><span class="{icon}">{label}</span></strong>'
71
               ' <span class="label {color_tag}">{tag}</span>'
72
               ' <span class="label {color_tag}">{result}</span>'
73
               ' <i>{comment}</i>'
74
               .format(negation=str(
75
                   label['negation'] if label['negation'] else ""),
76
                   icon=icons['color'],
77
                   label=label['str'],
78
                   color_tag=self.BOOTSTRAP_COLOR_TO_LABEL_COLOR[icons['color']],
79
                   tag=self.get_tag(),
80
                   result=self._get_not_negate_result(),
81
                   comment=self.get_comment()),
82
               "icon": icons['icon'],
83
               "state": {"opened": self._show_node(hide_passing_tests)},
84
               "info": self.tree.test_result_details,
85
               }
86 1
        if self.tree.children:
87 1
            out['children'] = [Converter(child).to_JsTree_dict(
88
                hide_passing_tests) for child in self.tree.children]
89 1
        return out
90
91 1
    def _get_not_negate_result(self):
92 1
        if self.tree.negation and self.tree.node_type == 'operator' and self.is_bool(
93
                self.result):
94 1
            return self.negate_bool(self.result)
95 1
        return self.result
96
97 1
    def _show_node(self, hide_passing_tests):
98 1
        return not(self.result == 'true' and hide_passing_tests)
99
100 1
    def _get_node_style(self):
101 1
        value = self._get_not_negate_result()
102 1
        out_color = None
103 1
        if self.tree.negation and self.is_bool(value):
104 1
            out_color = self.negate_bool(value)
105
        else:
106 1
            out_color = value
107 1
        return dict(
108
            negation_color=out_color,
109
            test_value=value,
110
        )
111
112 1
    def get_negation_character(self, value):
113 1
        return ('<strong><span class="' +
114
                self.VALUE_TO_BOOTSTRAP_COLOR[value] +
115
                '">NOT</strong></span>')
116
117 1
    def _get_label(self):
118 1
        out = dict(negation=None, str="")
119 1
        if self.tree.node_type == 'value':
120 1
            out['negation'] = self._get_negation_for_label_of_value()
121 1
            out['str'] = re.sub(
122
                '(oval:ssg-test_|oval:ssg-)|(:def:1|:tst:1)', '', str(self.tree.node_id))
123
        else:
124 1
            if str(self.tree.node_id).startswith('xccdf_org'):
125 1
                out['str'] = re.sub(
126
                    '(xccdf_org.ssgproject.content_)', '', str(
127
                        self.tree.node_id))
128
            else:
129 1
                out['negation'] = self._get_negation_for_label_of_operator()
130 1
                out['str'] = (self.tree.value).upper()
131 1
        return out
132
133 1
    def _get_negation_for_label_of_value(self):
134 1
        if self.tree.negation and self.is_bool(self.tree.value):
135 1
            return self.get_negation_character(
136
                self.negate_bool(self.tree.value))
137
138 1
    def _get_negation_for_label_of_operator(self):
139 1
        if self.tree.negation and self.is_bool(self.result):
140 1
            return self.get_negation_character(self.result)
141
142 1
    def negate_bool(self, value):
143 1
        values = {
144
            "true": "false",
145
            "false": "true",
146
        }
147 1
        return values[str(value)]
148
149 1
    def is_bool(self, value):
150
        return value == "true" or value == "false"
151