oval_graph.arf_xml_parser._oval_scan_definitions   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Test Coverage

Coverage 98.51%

Importance

Changes 0
Metric Value
eloc 105
dl 0
loc 128
ccs 66
cts 67
cp 0.9851
rs 10
c 0
b 0
f 0
wmc 22

10 Methods

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