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