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

oval_graph.arf_xml_parser._oval_scan_definitions   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Test Coverage

Coverage 98.33%

Importance

Changes 0
Metric Value
eloc 105
dl 0
loc 127
ccs 59
cts 60
cp 0.9833
rs 10
c 0
b 0
f 0
wmc 20

8 Methods

Rating   Name   Duplication   Size   Complexity  
A _OvalScanDefinitions._fill_extend_definition_help() 0 20 4
A _OvalScanDefinitions._find_definition_by_id() 0 9 2
A _OvalScanDefinitions._fill_extend_definition() 0 9 2
A _OvalScanDefinitions.__init__() 0 4 1
A _OvalScanDefinitions._build_node() 0 38 4
A _OvalScanDefinitions._get_negate_status() 0 10 2
A _OvalScanDefinitions.get_scan() 0 9 2
A _OvalScanDefinitions._get_result() 0 15 3
1 1
from ._comments import _Comments
2 1
from ._test_info import _TestInfo
3
4
5 1
class _OvalScanDefinitions:
6 1
    def __init__(self, definitions, oval_definitions, report_data):
7 1
        self.definitions = definitions
8 1
        self.comments_parser = _Comments(oval_definitions)
9 1
        self.test_info_parser = _TestInfo(report_data)
10
11 1
    def get_scan(self):
12 1
        dict_of_definitions = {}
13 1
        for definition in self.definitions:
14 1
            id_definition = definition.get('definition_id')
15 1
            dict_of_definitions[id_definition] = dict(
16
                commnet=None, node=self._build_node(
17
                    definition[0], "Definition", id_definition))
18 1
        self.comments_parser.insert_comments(dict_of_definitions)
19 1
        return self._fill_extend_definition(dict_of_definitions)
20
21 1
    @staticmethod
22
    def _get_negate_status(node):
23 1
        str_to_bool = {
24
            'true': True,
25
            'false': False,
26
        }
27 1
        negate_status = False
28 1
        if node.get('negate') is not None:
29 1
            negate_status = str_to_bool[node.get('negate')]
30 1
        return negate_status
31
32 1
    @staticmethod
33
    def _get_result(negate_status, tree):
34
        """
35
            This  method  removes  the  negation of
36
            the result. Because negation is already
37
            included in the result in ARF file.
38
        """
39 1
        result = tree.get('result')
40 1
        reverse_negate_value = {
41
            'true': 'false',
42
            'false': 'true',
43
        }
44 1
        if negate_status and result in ('true', 'false'):
45 1
            result = reverse_negate_value[result]
46 1
        return result
47
48 1
    def _build_node(self, tree, tag, id_definition=None):
49 1
        negate_status = self._get_negate_status(tree)
50 1
        node = dict(
51
            id=id_definition,
52
            operator=tree.get('operator'),
53
            negate=negate_status,
54
            result=self._get_result(negate_status, tree),
55
            comment=None,
56
            tag=tag,
57
            node=[],
58
        )
59 1
        for child in tree:
60 1
            if child.get('operator') is not None:
61 1
                node['node'].append(self._build_node(child, "Criteria"))
62
            else:
63 1
                negate_status = self._get_negate_status(child)
64 1
                result_of_node = self._get_result(negate_status, child)
65 1
                if child.get('definition_ref') is not None:
66 1
                    node['node'].append(
67
                        dict(
68
                            extend_definition=child.get('definition_ref'),
69
                            result=result_of_node,
70
                            negate=negate_status,
71
                            comment=None,
72
                            tag="Extend definition",
73
                        ))
74
                else:
75 1
                    node['node'].append(
76
                        dict(
77
                            value_id=child.get('test_ref'),
78
                            value=result_of_node,
79
                            negate=negate_status,
80
                            comment=None,
81
                            tag="Test",
82
                            test_result_details=self.test_info_parser.get_info_about_test(
83
                                child.get('test_ref')),
84
                        ))
85 1
        return node
86
87 1
    def _fill_extend_definition(self, dict_of_definitions):
88 1
        out = {}
89 1
        for id_definition, definition in dict_of_definitions.items():
90 1
            out[id_definition] = dict(
91
                comment=definition['comment'],
92
                node=self._fill_extend_definition_help(
93
                    definition['node'],
94
                    dict_of_definitions))
95 1
        return out
96
97 1
    def _fill_extend_definition_help(self, value, dict_of_definitions):
98 1
        out = dict(
99
            operator=value['operator'],
100
            negate=value['negate'],
101
            result=value['result'],
102
            comment=value['comment'],
103
            tag=value['tag'],
104
            node=[],
105
        )
106 1
        for child in value['node']:
107 1
            if 'operator' in child:
108 1
                out['node'].append(
109
                    self._fill_extend_definition_help(
110
                        child, dict_of_definitions))
111 1
            elif 'extend_definition' in child:
112 1
                out['node'].append(
113
                    self._find_definition_by_id(dict_of_definitions, child))
114
            else:
115 1
                out['node'].append(child)
116 1
        return out
117
118 1
    def _find_definition_by_id(self, dict_of_definitions, child):
119 1
        id_ = child['extend_definition']
120 1
        if id_ in dict_of_definitions:
121 1
            dict_of_definitions[id_]['node']['negate'] = child['negate']
122 1
            dict_of_definitions[id_]['node']['comment'] = child['comment']
123 1
            dict_of_definitions[id_]['node']['tag'] = child['tag']
124 1
            return self._fill_extend_definition_help(
125
                dict_of_definitions[id_]['node'], dict_of_definitions)
126
        return None
127