Test Failed
Pull Request — master (#203)
by Jan
03:38
created

oval_graph.arf_xml_parser._comments   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 63
dl 0
loc 76
ccs 50
cts 50
cp 1
rs 10
c 0
b 0
f 0
wmc 20

9 Methods

Rating   Name   Duplication   Size   Complexity  
A _Comments._get_operator() 0 4 2
A _Comments._prepare_definition_comments() 0 14 2
A _Comments._get_comment() 0 4 2
A _Comments._fill_comment() 0 5 1
A _Comments._recursive_help_fill_comments() 0 6 4
A _Comments._get_dict_with_comment() 0 7 2
A _Comments.insert_comments() 0 6 3
A _Comments._create_dict_form_criteria() 0 13 3
A _Comments.__init__() 0 2 1
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