Passed
Pull Request — master (#187)
by Jan
05:04
created

oval_graph.arf_xml_parser._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 62
dl 0
loc 77
ccs 49
cts 49
cp 1
rs 10
c 0
b 0
f 0
wmc 20

9 Methods

Rating   Name   Duplication   Size   Complexity  
A _XmlParserComments._create_dict_form_criteria() 0 14 3
A _XmlParserComments.__init__() 0 2 1
A _XmlParserComments._recursive_help_fill_comments() 0 6 4
A _XmlParserComments._get_comment() 0 3 2
A _XmlParserComments._fill_comment() 0 5 1
A _XmlParserComments.insert_comments() 0 6 3
A _XmlParserComments._get_dict_with_comment() 0 9 2
A _XmlParserComments._prepare_definition_comments() 0 14 2
A _XmlParserComments._get_operator() 0 3 2
1 1
ns = {
2
    'oval-definitions': 'http://oval.mitre.org/XMLSchema/oval-definitions-5',
3
}
4
5
6 1
class _XmlParserComments:
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
23 1
        return comments
24
25 1
    def _get_dict_with_comment(self, criterion):
26 1
        out = dict(
27
            comment=self._get_comment(criterion),
28
        )
29 1
        if criterion.get('definition_ref'):
30 1
            out['extend_definition'] = criterion.get('definition_ref')
31
        else:
32 1
            out['value_id'] = criterion.get('test_ref')
33 1
        return out
34
35 1
    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...
36 1
        operator = criterion.get('operator')
37 1
        return 'AND' if operator is None else operator
38
39 1
    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...
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', ns)
49 1
            description = definition.find(
50
                './/oval-definitions:metadata/oval-definitions:description', ns)
51 1
            comment_definition['comment'] = title.text
52 1
            criteria = definition.find('.//oval-definitions:criteria', ns)
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