Passed
Push — master ( 23c8bb...7778bf )
by Matěj
07:59 queued 10s
created

oval_graph.converter.Converter.__init__()   A

Complexity

Conditions 2

Size

Total Lines 23
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
cc 2
eloc 18
nop 2
dl 0
loc 23
ccs 5
cts 6
cp 0.8333
crap 2.0185
rs 9.5
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.VALUE_TO_ICON = {
19
            "true": "glyphicon glyphicon-ok text-success",
20
            "false": "glyphicon glyphicon-remove text-danger",
21
            "error": "glyphicon glyphicon-question-sign text-dark",
22
            "unknown": "glyphicon glyphicon-question-sign text-dark",
23
            "noteval": "glyphicon glyphicon-question-sign text-dark",
24
            "notappl": "glyphicon glyphicon-question-sign text-dark"
25
        }
26
27 1
        if isinstance(tree, OvalNode):
28 1
            self.tree = tree
29
        else:
30
            raise ValueError('err - this is not tree created from OvalNodes')
31
32 1
    def _get_node_icon(self):
33 1
        values = self._get_node_style()
34 1
        return dict(
35
            color=self.VALUE_TO_BOOTSTRAP_COLOR[values['negation_color']],
36
            icon=self.VALUE_TO_ICON[values['test_value']],
37
        )
38
39 1
    def get_comment(self):
40 1
        if self.tree.comment is not None:
41 1
            return str(self.tree.comment)
42 1
        return ""
43
44 1
    def to_JsTree_dict(self):
45 1
        icons = self._get_node_icon()
46 1
        label = self._get_label()
47 1
        out = {
48
            'text': (str(label['negation'] if label['negation'] else "") +
49
                     ' <strong><span class="' + icons['color'] + '">' +
50
                     label['str'] + '</span></strong>' +
51
                     ' <i>' + self.get_comment() + '</i>'
52
                     ),
53
            "icon": icons['icon'],
54
            "state": {
55
                "opened": True}}
56 1
        if self.tree.children:
57 1
            out['children'] = [Converter(child).to_JsTree_dict()
58
                               for child in self.tree.children]
59 1
        return out
60
61 1
    def _get_node_style(self):
62 1
        value = self.tree.evaluate_tree()
63 1
        out_color = None
64 1
        if value is None:
65 1
            if self.tree.negation:
66 1
                out_color = self.negate_bool(self.tree.value)
67
            else:
68 1
                out_color = self.tree.value
69 1
            value = self.tree.value
70
        else:
71 1
            if self.tree.negation:
72 1
                out_color = self.negate_bool(value)
73
            else:
74 1
                out_color = value
75 1
        return dict(
76
            negation_color=out_color,
77
            test_value=value,
78
        )
79
80 1
    def get_negation_character(self, value):
81 1
        return ('<strong><span class="' +
82
                self.VALUE_TO_BOOTSTRAP_COLOR[value] +
83
                '">&not;</strong></span>')
84
85 1
    def _get_label(self):
86 1
        out = dict(negation=None, str="")
87 1
        if self.tree.node_type == 'value':
88 1
            if self.tree.negation:
89 1
                out['negation'] = self.get_negation_character(self.tree.value)
90 1
            out['str'] = re.sub(
91
                '(oval:ssg-test_|oval:ssg-)|(:def:1|:tst:1)', '', str(self.tree.node_id))
92 1
            return out
93
        else:
94 1
            if str(self.tree.node_id).startswith('xccdf_org'):
95 1
                out['str'] = re.sub(
96
                    '(xccdf_org.ssgproject.content_)', '', str(
97
                        self.tree.node_id))
98 1
                return out
99
            else:
100 1
                if self.tree.negation:
101 1
                    out['negation'] = self.get_negation_character(
102
                        self.tree.evaluate_tree())
103 1
                out['str'] = (self.tree.value).upper()
104 1
                return out
105
106 1
    def negate_bool(self, value):
107 1
        values = {
108
            "true": "false",
109
            "false": "true",
110
        }
111
        return values[str(value)]
112