Passed
Pull Request — master (#91)
by Jan
02:10
created

oval_graph.converter   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Test Coverage

Coverage 95.77%

Importance

Changes 0
Metric Value
eloc 116
dl 0
loc 147
ccs 68
cts 71
cp 0.9577
rs 10
c 0
b 0
f 0
wmc 29

11 Methods

Rating   Name   Duplication   Size   Complexity  
A Converter.__init__() 0 32 3
A Converter.get_comment() 0 4 2
A Converter.get_tag() 0 4 2
A Converter._get_node_icon() 0 5 1
A Converter.negate_bool() 0 6 1
A Converter.to_JsTree_dict() 0 31 4
A Converter._show_node() 0 7 4
A Converter.get_negation_character() 0 4 1
A Converter._get_label() 0 20 5
A Converter.is_bool() 0 4 3
A Converter._get_node_style() 0 10 3
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 not self.result:
37 1
                self.result = self.tree.value
38
        else:
39
            raise ValueError('err - this is not tree 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['negation_color']],
45
            icon=self.VALUE_TO_ICON[values['test_value']],
46
        )
47
48 1
    def get_comment(self):
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):
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):
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.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 _show_node(self, hide_passing_tests):
91 1
        value = self.tree.evaluate_tree()
92 1
        if value is None:
93 1
            value = self.tree.value
94 1
        if value == 'true' and hide_passing_tests:
95
            return False
96 1
        return True
97
98 1
    def _get_node_style(self):
99 1
        value = self.result
100 1
        out_color = None
101 1
        if self.tree.negation and self.is_bool(value):
102 1
            out_color = self.negate_bool(value)
103
        else:
104 1
            out_color = value
105 1
        return dict(
106
            negation_color=out_color,
107
            test_value=value,
108
        )
109
110 1
    def get_negation_character(self, value):
111 1
        return ('<strong><span class="' +
112
                self.VALUE_TO_BOOTSTRAP_COLOR[value] +
113
                '">NOT</strong></span>')
114
115 1
    def _get_label(self):
116 1
        out = dict(negation=None, str="")
117 1
        if self.tree.node_type == 'value':
118 1
            if self.tree.negation:
119 1
                out['negation'] = self.get_negation_character(self.tree.value)
120 1
            out['str'] = re.sub(
121
                '(oval:ssg-test_|oval:ssg-)|(:def:1|:tst:1)', '', str(self.tree.node_id))
122 1
            return out
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 1
                return out
129
            else:
130 1
                if self.tree.negation:
131 1
                    out['negation'] = self.get_negation_character(
132
                        self.tree.evaluate_tree())
133 1
                out['str'] = (self.tree.value).upper()
134 1
                return out
135
136 1
    def negate_bool(self, value):
137 1
        values = {
138
            "true": "false",
139
            "false": "true",
140
        }
141 1
        return values[str(value)]
142
143 1
    def is_bool(self, value):
144 1
        if value == "true" or value == "false":
145 1
            return True
146
        return False
147