1
|
|
|
""" |
2
|
|
|
This file contains a class for converting OVAL tree to according JSON used in HTML |
3
|
|
|
""" |
4
|
|
|
|
5
|
1 |
|
import re |
6
|
|
|
|
7
|
1 |
|
from .oval_node import OvalNode |
8
|
|
|
|
9
|
1 |
|
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 |
|
BOOTSTRAP_COLOR_TO_LABEL_COLOR = { |
19
|
|
|
"text-success": "label-success", |
20
|
|
|
"text-danger": "label-danger", |
21
|
|
|
"text-dark": "label-default" |
22
|
|
|
} |
23
|
|
|
|
24
|
1 |
|
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
|
|
|
|
34
|
1 |
|
class Converter(): |
35
|
|
|
"""The Converter object converts OVAL tree to dict according JSON |
36
|
|
|
for graphic representation with JsTree. |
37
|
|
|
|
38
|
|
|
Attributes: |
39
|
|
|
tree (OvalNode): OVAL tree |
40
|
|
|
result (str): result of node |
41
|
|
|
""" |
42
|
|
|
|
43
|
1 |
|
def __init__(self, tree): |
44
|
|
|
"""This metode construct Converter. |
45
|
|
|
|
46
|
|
|
Args: |
47
|
|
|
tree (OvalNode): OVAL tree |
48
|
|
|
|
49
|
|
|
Raises: |
50
|
|
|
ValueError |
51
|
|
|
""" |
52
|
1 |
|
if isinstance(tree, OvalNode): |
53
|
1 |
|
self.tree = tree |
54
|
1 |
|
self.result = self.tree.evaluate_tree() |
55
|
1 |
|
if self.tree.node_type == 'value': |
56
|
1 |
|
self.result = self.tree.value |
57
|
|
|
else: |
58
|
|
|
raise ValueError( |
59
|
|
|
'This converter can process only trees created from OvalNodes.') |
60
|
|
|
|
61
|
1 |
|
def _get_node_icon(self): |
62
|
1 |
|
values = self._get_node_style() |
63
|
1 |
|
return dict( |
64
|
|
|
color=VALUE_TO_BOOTSTRAP_COLOR[values['test_value']], |
65
|
|
|
icon=VALUE_TO_ICON[values['negation_color']], |
66
|
|
|
) |
67
|
|
|
|
68
|
1 |
|
def _get_comment(self): |
69
|
1 |
|
if self.tree.comment is not None: |
70
|
1 |
|
return str(self.tree.comment) |
71
|
1 |
|
return "" |
72
|
|
|
|
73
|
1 |
|
def _get_tag(self): |
74
|
1 |
|
if self.tree.tag is not None: |
75
|
1 |
|
return str(self.tree.tag) |
76
|
1 |
|
return "" |
77
|
|
|
|
78
|
1 |
|
def _get_not_negate_result(self): |
79
|
1 |
|
if self.tree.negation and self.tree.node_type == 'operator' and self._is_bool( |
80
|
|
|
self.result): |
81
|
1 |
|
return self._negate_bool(self.result) |
82
|
1 |
|
return self.result |
83
|
|
|
|
84
|
1 |
|
def _show_node(self, hide_passing_tests): |
85
|
1 |
|
return not(self.result == 'true' and hide_passing_tests) |
86
|
|
|
|
87
|
1 |
|
def _get_node_style(self): |
88
|
1 |
|
value = self._get_not_negate_result() |
89
|
1 |
|
out_color = None |
90
|
1 |
|
if self.tree.negation and self._is_bool(value): |
91
|
1 |
|
out_color = self._negate_bool(value) |
92
|
|
|
else: |
93
|
1 |
|
out_color = value |
94
|
1 |
|
return dict( |
95
|
|
|
negation_color=out_color, |
96
|
|
|
test_value=value, |
97
|
|
|
) |
98
|
|
|
|
99
|
1 |
|
@staticmethod |
100
|
|
|
def _get_negation_character(value): |
101
|
1 |
|
return ( |
102
|
|
|
'<strong>' |
103
|
|
|
'<span class="' + VALUE_TO_BOOTSTRAP_COLOR[value] + '">NOT</strong>' |
104
|
|
|
'</span>' |
105
|
|
|
) |
106
|
|
|
|
107
|
1 |
|
def _get_label(self): |
108
|
1 |
|
out = dict(negation=None, str="") |
109
|
1 |
|
if self.tree.node_type == 'value': |
110
|
1 |
|
out['negation'] = self._get_negation_label() |
111
|
1 |
|
out['str'] = re.sub( |
112
|
|
|
'(oval:ssg-test_|oval:ssg-)|(:def:1|:tst:1)', '', str(self.tree.node_id)) |
113
|
|
|
else: |
114
|
1 |
|
if str(self.tree.node_id).startswith('xccdf_org'): |
115
|
1 |
|
out['str'] = re.sub( |
116
|
|
|
'(xccdf_org.ssgproject.content_)', '', str( |
117
|
|
|
self.tree.node_id)) |
118
|
|
|
else: |
119
|
1 |
|
out['negation'] = self._get_negation_label() |
120
|
1 |
|
out['str'] = (self.tree.value).upper() |
121
|
1 |
|
return out |
122
|
|
|
|
123
|
1 |
|
def _get_negation_label(self): |
124
|
1 |
|
if self.tree.negation and self._is_bool(self.tree.value): |
125
|
1 |
|
return self._get_negation_character(self._negate_bool(self.tree.value)) |
126
|
1 |
|
if self.tree.negation and self._is_bool(self.result): |
127
|
1 |
|
return self._get_negation_character(self.result) |
128
|
1 |
|
return None |
129
|
|
|
|
130
|
1 |
|
@staticmethod |
131
|
|
|
def _negate_bool(value): |
132
|
1 |
|
values = { |
133
|
|
|
"true": "false", |
134
|
|
|
"false": "true", |
135
|
|
|
} |
136
|
1 |
|
return values[str(value)] |
137
|
|
|
|
138
|
1 |
|
@staticmethod |
139
|
|
|
def _is_bool(value): |
140
|
1 |
|
return value in ("true", "false") |
141
|
|
|
|
142
|
1 |
|
def as_js_tree_dict(self, hide_passing_tests=False): |
143
|
|
|
"""Converts the entire OVAL tree to dict according JSON |
144
|
|
|
for graphic representation with JsTree. |
145
|
|
|
|
146
|
|
|
Args: |
147
|
|
|
hide_passing_tests (bool): bool switch witch enable hiding passing tests |
148
|
|
|
|
149
|
|
|
Returns: |
150
|
|
|
dict. Dictionary representing OVAL tree |
151
|
|
|
""" |
152
|
1 |
|
icons = self._get_node_icon() |
153
|
1 |
|
label = self._get_label() |
154
|
1 |
|
if self.tree.test_result_details: |
155
|
1 |
|
self.tree.test_result_details['result'] = ( |
156
|
|
|
' <span class="label {color_tag}">{result}</span>' |
157
|
|
|
.format( |
158
|
|
|
color_tag=BOOTSTRAP_COLOR_TO_LABEL_COLOR[icons['color']], |
159
|
|
|
result=self.result, |
160
|
|
|
)) |
161
|
1 |
|
out = {'text': |
162
|
|
|
'{negation} <strong><span class="{icon}">{label}</span></strong>' |
163
|
|
|
' <span class="label {color_tag}">{tag}</span>' |
164
|
|
|
' <span class="label {color_tag}">{result}</span>' |
165
|
|
|
' <i>{comment}</i>' |
166
|
|
|
.format( |
167
|
|
|
negation=str(label['negation'] if label['negation'] else ""), |
168
|
|
|
icon=icons['color'], |
169
|
|
|
label=label['str'], |
170
|
|
|
color_tag=BOOTSTRAP_COLOR_TO_LABEL_COLOR[icons['color']], |
171
|
|
|
tag=self._get_tag(), |
172
|
|
|
result=self._get_not_negate_result(), |
173
|
|
|
comment=self._get_comment()), |
174
|
|
|
"icon": icons['icon'], |
175
|
|
|
"state": {"opened": self._show_node(hide_passing_tests)}, |
176
|
|
|
"info": self.tree.test_result_details, |
177
|
|
|
} |
178
|
1 |
|
if self.tree.children: |
179
|
1 |
|
out['children'] = [Converter(child).as_js_tree_dict( |
180
|
|
|
hide_passing_tests) for child in self.tree.children] |
181
|
1 |
|
return out |
182
|
|
|
|
183
|
1 |
|
def as_dict(self): |
184
|
|
|
"""Converts the entire OVAL tree to dict. |
185
|
|
|
|
186
|
|
|
Returns: |
187
|
|
|
dict. Dictionary representing OVAL tree |
188
|
|
|
""" |
189
|
1 |
|
node = self.tree |
190
|
1 |
|
if not node.children: |
191
|
1 |
|
return { |
192
|
|
|
'node_id': node.node_id, |
193
|
|
|
'type': node.node_type, |
194
|
|
|
'value': node.value, |
195
|
|
|
'negation': node.negation, |
196
|
|
|
'comment': node.comment, |
197
|
|
|
'tag': node.tag, |
198
|
|
|
'test_result_details': node.test_result_details, |
199
|
|
|
'child': None |
200
|
|
|
} |
201
|
1 |
|
return { |
202
|
|
|
'node_id': node.node_id, |
203
|
|
|
'type': node.node_type, |
204
|
|
|
'value': node.value, |
205
|
|
|
'negation': node.negation, |
206
|
|
|
'comment': node.comment, |
207
|
|
|
'tag': node.tag, |
208
|
|
|
'test_result_details': node.test_result_details, |
209
|
|
|
'child': [Converter(child).as_dict() for child in node.children] |
210
|
|
|
} |
211
|
|
|
|