Passed
Push — master ( ea6c64...c4e6f9 )
by Jan
01:23 queued 11s
created

oval_graph.converter.Converter.__init__()   A

Complexity

Conditions 2

Size

Total Lines 28
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
cc 2
eloc 22
nop 2
dl 0
loc 28
ccs 6
cts 7
cp 0.8571
crap 2.0116
rs 9.352
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 1
        self.VALUE_TO_ICON = {
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
        else:
35
            raise ValueError('err - this is not tree created from OvalNodes')
36
37 1
    def _get_node_icon(self):
38 1
        values = self._get_node_style()
39 1
        return dict(
40
            color=self.VALUE_TO_BOOTSTRAP_COLOR[values['negation_color']],
41
            icon=self.VALUE_TO_ICON[values['test_value']],
42
        )
43
44 1
    def get_comment(self):
45 1
        if self.tree.comment is not None:
46 1
            return str(self.tree.comment)
47 1
        return ""
48
49 1
    def get_tag(self):
50 1
        if self.tree.tag is not None:
51 1
            return str(self.tree.tag)
52 1
        return ""
53
54 1
    def to_JsTree_dict(self):
55 1
        icons = self._get_node_icon()
56 1
        label = self._get_label()
57 1
        out = {'text':
58
               '{negation} <strong><span class="{icon}">{label}</span></strong>'
59
               ' <span class="label {color_tag}">{tag}</span> <i>{comment}</i>'
60
               .format(negation=str(
61
                   label['negation'] if label['negation'] else ""),
62
                   icon=icons['color'],
63
                   label=label['str'],
64
                   color_tag=self.BOOTSTRAP_COLOR_TO_LABEL_COLOR[icons['color']],
65
                   tag=self.get_tag(),
66
                   comment=self.get_comment()),
67
               "icon": icons['icon'],
68
               "state": {"opened": True}}
69 1
        if self.tree.children:
70 1
            out['children'] = [Converter(child).to_JsTree_dict()
71
                               for child in self.tree.children]
72 1
        return out
73
74 1
    def _get_node_style(self):
75 1
        value = self.tree.evaluate_tree()
76 1
        out_color = None
77 1
        if value is None:
78 1
            if self.tree.negation:
79 1
                out_color = self.negate_bool(self.tree.value)
80
            else:
81 1
                out_color = self.tree.value
82 1
            value = self.tree.value
83
        else:
84 1
            if self.tree.negation:
85 1
                out_color = self.negate_bool(value)
86
            else:
87 1
                out_color = value
88 1
        return dict(
89
            negation_color=out_color,
90
            test_value=value,
91
        )
92
93 1
    def get_negation_character(self, value):
94 1
        return ('<strong><span class="' +
95
                self.VALUE_TO_BOOTSTRAP_COLOR[value] +
96
                '">NOT</strong></span>')
97
98 1
    def _get_label(self):
99 1
        out = dict(negation=None, str="")
100 1
        if self.tree.node_type == 'value':
101 1
            if self.tree.negation:
102 1
                out['negation'] = self.get_negation_character(self.tree.value)
103 1
            out['str'] = re.sub(
104
                '(oval:ssg-test_|oval:ssg-)|(:def:1|:tst:1)', '', str(self.tree.node_id))
105 1
            return out
106
        else:
107 1
            if str(self.tree.node_id).startswith('xccdf_org'):
108 1
                out['str'] = re.sub(
109
                    '(xccdf_org.ssgproject.content_)', '', str(
110
                        self.tree.node_id))
111 1
                return out
112
            else:
113 1
                if self.tree.negation:
114 1
                    out['negation'] = self.get_negation_character(
115
                        self.tree.evaluate_tree())
116 1
                out['str'] = (self.tree.value).upper()
117 1
                return out
118
119 1
    def negate_bool(self, value):
120 1
        values = {
121
            "true": "false",
122
            "false": "true",
123
        }
124
        return values[str(value)]
125