Passed
Push — master ( b5a080...903f35 )
by Jan
06:45 queued 01:03
created

Converter._negate_bool()   A

Complexity

Conditions 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.2963

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 7
rs 10
c 0
b 0
f 0
ccs 1
cts 3
cp 0.3333
cc 1
nop 1
crap 1.2963
1 1
import re
2
3 1
from .oval_node import OvalNode
4
5 1
VALUE_TO_BOOTSTRAP_COLOR = {
6
    "true": "text-success",
7
    "false": "text-danger",
8
    "error": "text-dark",
9
    "unknown": "text-dark",
10
    "noteval": "text-dark",
11
    "notappl": "text-dark"
12
}
13
14 1
BOOTSTRAP_COLOR_TO_LABEL_COLOR = {
15
    "text-success": "label-success",
16
    "text-danger": "label-danger",
17
    "text-dark": "label-default"
18
}
19
20 1
VALUE_TO_ICON = {
21
    "true": "glyphicon glyphicon-ok text-success",
22
    "false": "glyphicon glyphicon-remove text-danger",
23
    "error": "glyphicon glyphicon-question-sign text-dark",
24
    "unknown": "glyphicon glyphicon-question-sign text-dark",
25
    "noteval": "glyphicon glyphicon-question-sign text-dark",
26
    "notappl": "glyphicon glyphicon-question-sign text-dark"
27
}
28
29
30 1
class Converter():
31
    """The Converter object converts OVAL tree to dict according JSON
32
       for graphic representation with JsTree.
33
34
    Attributes:
35
        tree (OvalNode): OVAL tree
36
        result (str): result of node
37
    """
38
39 1
    def __init__(self, tree):
40 1
        if isinstance(tree, OvalNode):
41 1
            self.tree = tree
42 1
            self.result = self.tree.evaluate_tree()
43 1
            if self.tree.node_type == 'value':
44 1
                self.result = self.tree.value
45
        else:
46
            raise ValueError(
47
                'This converter can process only trees created from OvalNodes.')
48
49 1
    def _get_node_icon(self):
50 1
        values = self._get_node_style()
51 1
        return dict(
52
            color=VALUE_TO_BOOTSTRAP_COLOR[values['test_value']],
53
            icon=VALUE_TO_ICON[values['negation_color']],
54
        )
55
56 1
    def _get_comment(self):
57 1
        if self.tree.comment is not None:
58 1
            return str(self.tree.comment)
59
        return ""
60
61 1
    def _get_tag(self):
62 1
        if self.tree.tag is not None:
63 1
            return str(self.tree.tag)
64
        return ""
65
66 1
    def _get_not_negate_result(self):
67 1
        if self.tree.negation and self.tree.node_type == 'operator' and self._is_bool(
68
                self.result):
69
            return self._negate_bool(self.result)
70 1
        return self.result
71
72 1
    def _show_node(self, hide_passing_tests):
73 1
        return not(self.result == 'true' and hide_passing_tests)
74
75 1
    def _get_node_style(self):
76 1
        value = self._get_not_negate_result()
77 1
        out_color = None
78 1
        if self.tree.negation and self._is_bool(value):
79
            out_color = self._negate_bool(value)
80
        else:
81 1
            out_color = value
82 1
        return dict(
83
            negation_color=out_color,
84
            test_value=value,
85
        )
86
87 1
    @staticmethod
88
    def _get_negation_character(value):
89
        return (
90
            '<strong>'
91
            '<span class="' + VALUE_TO_BOOTSTRAP_COLOR[value] + '">NOT</strong>'
92
            '</span>'
93
        )
94
95 1
    def _get_label(self):
96 1
        out = dict(negation=None, str="")
97 1
        if self.tree.node_type == 'value':
98 1
            out['negation'] = self._get_negation_label()
99 1
            out['str'] = re.sub(
100
                '(oval:ssg-test_|oval:ssg-)|(:def:1|:tst:1)', '', str(self.tree.node_id))
101
        else:
102 1
            if str(self.tree.node_id).startswith('xccdf_org'):
103 1
                out['str'] = re.sub(
104
                    '(xccdf_org.ssgproject.content_)', '', str(
105
                        self.tree.node_id))
106
            else:
107 1
                out['negation'] = self._get_negation_label()
108 1
                out['str'] = (self.tree.value).upper()
109 1
        return out
110
111 1
    def _get_negation_label(self):
112 1
        if self.tree.negation and self._is_bool(self.tree.value):
113
            return self._get_negation_character(self._negate_bool(self.tree.value))
114 1
        if self.tree.negation and self._is_bool(self.result):
115
            return self._get_negation_character(self.result)
116 1
        return None
117
118 1
    @staticmethod
119
    def _negate_bool(value):
120
        values = {
121
            "true": "false",
122
            "false": "true",
123
        }
124
        return values[str(value)]
125
126 1
    @staticmethod
127
    def _is_bool(value):
128
        return value in ("true", "false")
129
130 1
    def to_js_tree_dict(self, hide_passing_tests=False):
131
        """Converts the OVAL tree to dict according JSON
132
           for graphic representation with JsTree.
133
134
        Args:
135
            hide_passing_tests (bool): bool switch witch enable hiding passing tests
136
137
        Returns:
138
            dict. Dictionary representing OVAL tree
139
        """
140 1
        icons = self._get_node_icon()
141 1
        label = self._get_label()
142 1
        if self.tree.test_result_details:
143 1
            self.tree.test_result_details['result'] = (
144
                ' <span class="label {color_tag}">{result}</span>'
145
                .format(
146
                    color_tag=BOOTSTRAP_COLOR_TO_LABEL_COLOR[icons['color']],
147
                    result=self.result,
148
                ))
149 1
        out = {'text':
150
               '{negation} <strong><span class="{icon}">{label}</span></strong>'
151
               ' <span class="label {color_tag}">{tag}</span>'
152
               ' <span class="label {color_tag}">{result}</span>'
153
               ' <i>{comment}</i>'
154
               .format(
155
                   negation=str(label['negation'] if label['negation'] else ""),
156
                   icon=icons['color'],
157
                   label=label['str'],
158
                   color_tag=BOOTSTRAP_COLOR_TO_LABEL_COLOR[icons['color']],
159
                   tag=self._get_tag(),
160
                   result=self._get_not_negate_result(),
161
                   comment=self._get_comment()),
162
               "icon": icons['icon'],
163
               "state": {"opened": self._show_node(hide_passing_tests)},
164
               "info": self.tree.test_result_details,
165
               }
166 1
        if self.tree.children:
167 1
            out['children'] = [Converter(child).to_js_tree_dict(
168
                hide_passing_tests) for child in self.tree.children]
169 1
        return out
170
171 1
    def to_dict(self):
172 1
        node = self.tree
173 1
        if not node.children:
174 1
            return {
175
                'node_id': node.node_id,
176
                'type': node.node_type,
177
                'value': node.value,
178
                'negation': node.negation,
179
                'comment': node.comment,
180
                'tag': node.tag,
181
                'test_result_details': node.test_result_details,
182
                'child': None
183
            }
184 1
        return {
185
            'node_id': node.node_id,
186
            'type': node.node_type,
187
            'value': node.value,
188
            'negation': node.negation,
189
            'comment': node.comment,
190
            'tag': node.tag,
191
            'test_result_details': node.test_result_details,
192
            'child': [Converter(child).to_dict() for child in node.children]
193
        }
194