Passed
Pull Request — master (#97)
by Jan
01:40
created

oval_graph._xml_parser_comments   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 17
eloc 59
dl 0
loc 73
ccs 42
cts 42
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A _XmlParserComments.__init__() 0 2 1
A _XmlParserComments._recursive_help_fill_comments() 0 7 3
B _XmlParserComments._create_dict_form_criteria() 0 24 6
A _XmlParserComments._fill_comment() 0 5 1
A _XmlParserComments.insert_comments() 0 6 4
A _XmlParserComments._prepare_definition_comments() 0 15 2
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