Passed
Push — master ( 6959e5...009541 )
by Jan
01:39 queued 14s
created

oval_graph.converter.Converter.to_JsTree_dict()   A

Complexity

Conditions 3

Size

Total Lines 22
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 19
nop 2
dl 0
loc 22
ccs 7
cts 7
cp 1
crap 3
rs 9.45
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 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
        out = {'text':
62
               '{negation} <strong><span class="{icon}">{label}</span></strong>'
63
               ' <span class="label {color_tag}">{tag}</span>'
64
               ' <span class="label {color_tag}">{result}</span>'
65
               ' <i>{comment}</i>'
66
               .format(negation=str(
67
                   label['negation'] if label['negation'] else ""),
68
                   icon=icons['color'],
69
                   label=label['str'],
70
                   color_tag=self.BOOTSTRAP_COLOR_TO_LABEL_COLOR[icons['color']],
71
                   tag=self.get_tag(),
72
                   result=self.result,
73
                   comment=self.get_comment()),
74
               "icon": icons['icon'],
75
               "state": {"opened": self._show_node(hide_passing_tests)}}
76 1
        if self.tree.children:
77 1
            out['children'] = [Converter(child).to_JsTree_dict(
78
                hide_passing_tests) for child in self.tree.children]
79 1
        return out
80
81 1
    def _show_node(self, hide_passing_tests):
82 1
        value = self.tree.evaluate_tree()
83 1
        if value is None:
84 1
            value = self.tree.value
85 1
        if value == 'true' and hide_passing_tests:
86
            return False
87 1
        return True
88
89 1
    def _get_node_style(self):
90 1
        value = self.result
91 1
        out_color = None
92 1
        if self.tree.negation and self.is_bool(value):
93 1
            out_color = self.negate_bool(value)
94
        else:
95 1
            out_color = value
96 1
        return dict(
97
            negation_color=out_color,
98
            test_value=value,
99
        )
100
101 1
    def get_negation_character(self, value):
102 1
        return ('<strong><span class="' +
103
                self.VALUE_TO_BOOTSTRAP_COLOR[value] +
104
                '">NOT</strong></span>')
105
106 1
    def _get_label(self):
107 1
        out = dict(negation=None, str="")
108 1
        if self.tree.node_type == 'value':
109 1
            if self.tree.negation:
110 1
                out['negation'] = self.get_negation_character(self.tree.value)
111 1
            out['str'] = re.sub(
112
                '(oval:ssg-test_|oval:ssg-)|(:def:1|:tst:1)', '', str(self.tree.node_id))
113 1
            return out
114
        else:
115 1
            if str(self.tree.node_id).startswith('xccdf_org'):
116 1
                out['str'] = re.sub(
117
                    '(xccdf_org.ssgproject.content_)', '', str(
118
                        self.tree.node_id))
119 1
                return out
120
            else:
121 1
                if self.tree.negation:
122 1
                    out['negation'] = self.get_negation_character(
123
                        self.tree.evaluate_tree())
124 1
                out['str'] = (self.tree.value).upper()
125 1
                return out
126
127 1
    def negate_bool(self, value):
128 1
        values = {
129
            "true": "false",
130
            "false": "true",
131
        }
132 1
        return values[str(value)]
133
134 1
    def is_bool(self, value):
135 1
        if value == "true" or value == "false":
136 1
            return True
137
        return False
138