1
|
1 |
|
ns = { |
2
|
|
|
'oval-definitions': 'http://oval.mitre.org/XMLSchema/oval-definitions-5', |
3
|
|
|
} |
4
|
|
|
|
5
|
|
|
|
6
|
1 |
|
class _Comments: |
7
|
1 |
|
def __init__(self, oval_definitions): |
8
|
1 |
|
self.oval_definitions = oval_definitions |
9
|
|
|
|
10
|
1 |
|
def _create_dict_form_criteria(self, criteria, description=None): |
11
|
1 |
|
comments = dict( |
12
|
|
|
operator=self._get_operator(criteria), |
13
|
|
|
comment=self._get_comment(criteria, description), |
14
|
|
|
node=[], |
15
|
|
|
) |
16
|
1 |
|
for criterion in criteria: |
17
|
1 |
|
if criterion.get('operator'): |
18
|
1 |
|
comments['node'].append( |
19
|
|
|
self._create_dict_form_criteria(criterion)) |
20
|
|
|
else: |
21
|
1 |
|
comments['node'].append(self._get_dict_with_comment(criterion)) |
22
|
1 |
|
return comments |
23
|
|
|
|
24
|
1 |
|
def _get_dict_with_comment(self, criterion): |
25
|
1 |
|
out = dict(comment=self._get_comment(criterion)) |
26
|
1 |
|
if criterion.get('definition_ref'): |
27
|
1 |
|
out['extend_definition'] = criterion.get('definition_ref') |
28
|
|
|
else: |
29
|
1 |
|
out['value_id'] = criterion.get('test_ref') |
30
|
1 |
|
return out |
31
|
|
|
|
32
|
1 |
|
@staticmethod |
33
|
|
|
def _get_operator(criterion): |
34
|
1 |
|
operator = criterion.get('operator') |
35
|
1 |
|
return 'AND' if operator is None else operator |
36
|
|
|
|
37
|
1 |
|
@staticmethod |
38
|
1 |
|
def _get_comment(criterion, description=None): |
39
|
1 |
|
comment = criterion.get('comment') |
40
|
1 |
|
return description if comment is None else comment |
41
|
|
|
|
42
|
1 |
|
def _prepare_definition_comments(self): |
43
|
1 |
|
definitions = {} |
44
|
1 |
|
for definition in self.oval_definitions: |
45
|
1 |
|
comment_definition = dict(comment=None, node=[]) |
46
|
1 |
|
title = definition.find( |
47
|
|
|
'.//oval-definitions:metadata/oval-definitions:title', ns) |
48
|
1 |
|
description = definition.find( |
49
|
|
|
'.//oval-definitions:metadata/oval-definitions:description', ns) |
50
|
1 |
|
comment_definition['comment'] = title.text |
51
|
1 |
|
criteria = definition.find('.//oval-definitions:criteria', ns) |
52
|
1 |
|
comment_definition['node'].append( |
53
|
|
|
self._create_dict_form_criteria(criteria, description.text)) |
54
|
1 |
|
definitions[definition.get('id')] = comment_definition |
55
|
1 |
|
return definitions |
56
|
|
|
|
57
|
1 |
|
def _recursive_help_fill_comments(self, tree_of_comments, tree): |
58
|
1 |
|
for node, node_of_comment in zip(tree, tree_of_comments): |
59
|
1 |
|
node['comment'] = node_of_comment['comment'] |
60
|
1 |
|
if 'operator' in node and node_of_comment.get('node'): |
61
|
1 |
|
self._recursive_help_fill_comments( |
62
|
|
|
node_of_comment['node'], node['node']) |
63
|
|
|
|
64
|
1 |
|
def _fill_comment(self, comment_definition, tree_definition): |
65
|
1 |
|
tree_of_comments = comment_definition['node'] |
66
|
1 |
|
tree = [tree_definition['node']] |
67
|
1 |
|
tree_definition['comment'] = comment_definition['comment'] |
68
|
1 |
|
self._recursive_help_fill_comments(tree_of_comments, tree) |
69
|
|
|
|
70
|
1 |
|
def insert_comments(self, dict_of_definitions): |
71
|
1 |
|
comment_definitions = self._prepare_definition_comments() |
72
|
1 |
|
for id_definition, definition in dict_of_definitions.items(): |
73
|
1 |
|
if id_definition in comment_definitions: |
74
|
1 |
|
self._fill_comment( |
75
|
|
|
comment_definitions[id_definition], definition) |
76
|
|
|
|