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