Passed
Pull Request — master (#202)
by Jan
03:42
created

oval_graph.arf_xml_parser._comments   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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