1
|
1 |
|
import re |
|
|
|
|
2
|
|
|
|
3
|
1 |
|
from .oval_node import OvalNode |
4
|
|
|
|
5
|
|
|
|
6
|
1 |
|
class Converter(): |
|
|
|
|
7
|
1 |
|
def __init__(self, tree): |
8
|
1 |
|
self.VALUE_TO_BOOTSTRAP_COLOR = { |
|
|
|
|
9
|
|
|
"true": "text-success", |
10
|
|
|
"false": "text-danger", |
11
|
|
|
"error": "text-dark", |
12
|
|
|
"unknown": "text-dark", |
13
|
|
|
"noteval": "text-dark", |
14
|
|
|
"notappl": "text-dark" |
15
|
|
|
} |
16
|
|
|
|
17
|
1 |
|
self.BOOTSTRAP_COLOR_TO_LABEL_COLOR = { |
|
|
|
|
18
|
|
|
"text-success": "label-success", |
19
|
|
|
"text-danger": "label-danger", |
20
|
|
|
"text-dark": "label-default" |
21
|
|
|
} |
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
|
1 |
|
self.result = self.tree.evaluate_tree() |
35
|
1 |
|
if self.tree.node_type == 'value': |
36
|
1 |
|
self.result = self.tree.value |
37
|
|
|
else: |
38
|
|
|
raise ValueError( |
39
|
|
|
'This converter can process only trees 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['test_value']], |
45
|
|
|
icon=self.VALUE_TO_ICON[values['negation_color']], |
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._get_not_negate_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 _get_not_negate_result(self): |
91
|
1 |
|
if self.tree.negation and self.tree.node_type == 'operator' and self.is_bool( |
92
|
|
|
self.result): |
93
|
1 |
|
return self.negate_bool(self.result) |
94
|
1 |
|
return self.result |
95
|
|
|
|
96
|
1 |
|
def _show_node(self, hide_passing_tests): |
97
|
1 |
|
return not(self.result == 'true' and hide_passing_tests) |
98
|
|
|
|
99
|
1 |
|
def _get_node_style(self): |
100
|
1 |
|
value = self._get_not_negate_result() |
101
|
1 |
|
out_color = None |
102
|
1 |
|
if self.tree.negation and self.is_bool(value): |
103
|
1 |
|
out_color = self.negate_bool(value) |
104
|
|
|
else: |
105
|
1 |
|
out_color = value |
106
|
1 |
|
return dict( |
107
|
|
|
negation_color=out_color, |
108
|
|
|
test_value=value, |
109
|
|
|
) |
110
|
|
|
|
111
|
1 |
|
def get_negation_character(self, value): |
|
|
|
|
112
|
1 |
|
return ('<strong><span class="' + |
113
|
|
|
self.VALUE_TO_BOOTSTRAP_COLOR[value] + |
114
|
|
|
'">NOT</strong></span>') |
115
|
|
|
|
116
|
1 |
|
def _get_label(self): |
117
|
1 |
|
out = dict(negation=None, str="") |
118
|
1 |
|
if self.tree.node_type == 'value': |
119
|
1 |
|
out['negation'] = self._get_negation_for_label_of_value() |
120
|
1 |
|
out['str'] = re.sub( |
121
|
|
|
'(oval:ssg-test_|oval:ssg-)|(:def:1|:tst:1)', '', str(self.tree.node_id)) |
122
|
|
|
else: |
123
|
1 |
|
if str(self.tree.node_id).startswith('xccdf_org'): |
124
|
1 |
|
out['str'] = re.sub( |
125
|
|
|
'(xccdf_org.ssgproject.content_)', '', str( |
126
|
|
|
self.tree.node_id)) |
127
|
|
|
else: |
128
|
1 |
|
out['negation'] = self._get_negation_for_label_of_operator() |
129
|
1 |
|
out['str'] = (self.tree.value).upper() |
130
|
1 |
|
return out |
131
|
|
|
|
132
|
1 |
|
def _get_negation_for_label_of_value(self): |
|
|
|
|
133
|
1 |
|
if self.tree.negation and self.is_bool(self.tree.value): |
134
|
1 |
|
return self.get_negation_character( |
135
|
|
|
self.negate_bool(self.tree.value)) |
136
|
|
|
|
137
|
1 |
|
def _get_negation_for_label_of_operator(self): |
|
|
|
|
138
|
1 |
|
if self.tree.negation and self.is_bool(self.result): |
139
|
1 |
|
return self.get_negation_character(self.result) |
140
|
|
|
|
141
|
1 |
|
def negate_bool(self, value): |
|
|
|
|
142
|
1 |
|
values = { |
143
|
|
|
"true": "false", |
144
|
|
|
"false": "true", |
145
|
|
|
} |
146
|
1 |
|
return values[str(value)] |
147
|
|
|
|
148
|
1 |
|
def is_bool(self, value): |
|
|
|
|
149
|
|
|
return value == "true" or value == "false" |
|
|
|
|
150
|
|
|
|