Test Failed
Pull Request — master (#173)
by Jan
02:52
created

oval_graph._xml_parser_comments   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 20
eloc 62
dl 0
loc 81
ccs 0
cts 49
cp 0
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A _XmlParserComments._get_comment() 0 3 2
A _XmlParserComments.__init__() 0 2 1
A _XmlParserComments._get_operator() 0 3 2
A _XmlParserComments._recursive_help_fill_comments() 0 6 4
A _XmlParserComments._create_dict_form_criteria() 0 14 3
A _XmlParserComments._get_dict_with_comment() 0 9 2
A _XmlParserComments._fill_comment() 0 5 1
A _XmlParserComments.insert_comments() 0 6 3
A _XmlParserComments._prepare_definition_comments() 0 14 2
1
'''
2
    This file contains a class for processing comments from XML
3
'''
4
5
ns = {
6
    'oval-definitions': 'http://oval.mitre.org/XMLSchema/oval-definitions-5',
7
}
8
9
10
class _XmlParserComments:
11
    def __init__(self, oval_definitions):
12
        self.oval_definitions = oval_definitions
13
14
    def _create_dict_form_criteria(self, criteria, description=None):
15
        comments = dict(
16
            operator=self._get_operator(criteria),
17
            comment=self._get_comment(criteria, description),
18
            node=[],
19
        )
20
        for criterion in criteria:
21
            if criterion.get('operator'):
22
                comments['node'].append(
23
                    self._create_dict_form_criteria(criterion))
24
            else:
25
                comments['node'].append(self._get_dict_with_comment(criterion))
26
27
        return comments
28
29
    def _get_dict_with_comment(self, criterion):
30
        out = dict(
31
            comment=self._get_comment(criterion),
32
        )
33
        if criterion.get('definition_ref'):
34
            out['extend_definition'] = criterion.get('definition_ref')
35
        else:
36
            out['value_id'] = criterion.get('test_ref')
37
        return out
38
39
    def _get_operator(self, criterion):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
40
        operator = criterion.get('operator')
41
        return 'AND' if operator is None else operator
42
43
    def _get_comment(self, criterion, description=None):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
44
        comment = criterion.get('comment')
45
        return description if comment is None else comment
46
47
    def _prepare_definition_comments(self):
48
        definitions = {}
49
        for definition in self.oval_definitions:
50
            comment_definition = dict(comment=None, node=[])
51
            title = definition.find(
52
                './/oval-definitions:metadata/oval-definitions:title', ns)
53
            description = definition.find(
54
                './/oval-definitions:metadata/oval-definitions:description', ns)
55
            comment_definition['comment'] = title.text
56
            criteria = definition.find('.//oval-definitions:criteria', ns)
57
            comment_definition['node'].append(
58
                self._create_dict_form_criteria(criteria, description.text))
59
            definitions[definition.get('id')] = comment_definition
60
        return definitions
61
62
    def _recursive_help_fill_comments(self, tree_of_comments, tree):
63
        for node, node_of_comment in zip(tree, tree_of_comments):
64
            node['comment'] = node_of_comment['comment']
65
            if 'operator' in node and node_of_comment.get('node'):
66
                self._recursive_help_fill_comments(
67
                    node_of_comment['node'], node['node'])
68
69
    def _fill_comment(self, comment_definition, tree_definition):
70
        tree_of_comments = comment_definition['node']
71
        tree = [tree_definition['node']]
72
        tree_definition['comment'] = comment_definition['comment']
73
        self._recursive_help_fill_comments(tree_of_comments, tree)
74
75
    def insert_comments(self, dict_of_definitions):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
76
        comment_definitions = self._prepare_definition_comments()
77
        for id_definition, definition in dict_of_definitions.items():
78
            if id_definition in comment_definitions:
79
                self._fill_comment(
80
                    comment_definitions[id_definition], definition)
81