Passed
Pull Request — master (#97)
by Jan
01:40
created

oval_graph._xml_parser_oval_scan_definitions   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Test Coverage

Coverage 97.1%

Importance

Changes 0
Metric Value
eloc 109
dl 0
loc 137
ccs 67
cts 69
cp 0.971
rs 10
c 0
b 0
f 0
wmc 26

8 Methods

Rating   Name   Duplication   Size   Complexity  
A _XmlParserScanDefinitions.get_scan() 0 6 2
A _XmlParserScanDefinitions._str_to_bool() 0 8 3
A _XmlParserScanDefinitions._fill_extend_definition() 0 13 3
B _XmlParserScanDefinitions._operator_as_child() 0 27 5
A _XmlParserScanDefinitions._build_graph() 0 14 3
A _XmlParserScanDefinitions.__init__() 0 4 1
B _XmlParserScanDefinitions._build_node() 0 44 6
A _XmlParserScanDefinitions._find_definition_by_id() 0 7 3
1 1
from ._xml_parser_comments import _XmlParserComments
2 1
from ._xml_parser_test_info import _XmlParserTestInfo
3
4
5 1
class _XmlParserScanDefinitions:
6 1
    def __init__(self, definitions, oval_definitions, report_data):
7 1
        self.definitions = definitions
8 1
        self.comments_parser = _XmlParserComments(oval_definitions)
9 1
        self.test_info_parser = _XmlParserTestInfo(report_data)
10
11 1
    def get_scan(self):
12 1
        scan = dict(definitions=[])
13 1
        for definition in self.definitions:
14 1
            scan['definitions'].append(self._build_graph(definition))
15 1
        self.comments_parser.insert_comments(scan)
16 1
        return self._fill_extend_definition(scan)
17
18 1
    def _build_graph(self, tree_data):
19 1
        graph = dict(
20
            id=tree_data.get('definition_id'),
21
            node=[],
22
        )
23 1
        for tree in tree_data:
24
            # rework this
25 1
            negate_status = False
26 1
            if 'negate' in tree:
27
                negate_status = self._str_to_bool(tree.get('negate'))
28
29 1
            graph['negate'] = negate_status
30 1
            graph['node'].append(self._build_node(tree, "Definition"))
31 1
        return graph
32
33
    # rework this
34 1
    @staticmethod
35
    def _str_to_bool(s):
36 1
        if s == 'true':
37 1
            return True
38 1
        elif s == 'false':
39 1
            return False
40
        else:
41 1
            raise ValueError('err- negation is not bool')
42
43 1
    def _build_node(self, tree, tag):
44
        # rework this
45 1
        negate_status = False
46 1
        if tree.get('negate') is not None:
47 1
            negate_status = self._str_to_bool(tree.get('negate'))
48
49 1
        node = dict(
50
            operator=tree.get('operator'),
51
            negate=negate_status,
52
            result=tree.get('result'),
53
            comment=None,
54
            tag=tag,
55
            node=[],
56
        )
57 1
        for child in tree:
58 1
            if child.get('operator') is not None:
59 1
                node['node'].append(self._build_node(child, "Criteria"))
60
            else:
61
                # rework this
62 1
                negate_status = False
63 1
                if child.get('negate') is not None:
64 1
                    negate_status = self._str_to_bool(child.get('negate'))
65
66 1
                if child.get('definition_ref') is not None:
67 1
                    node['node'].append(
68
                        dict(
69
                            extend_definition=child.get('definition_ref'),
70
                            result=child.get('result'),
71
                            negate=negate_status,
72
                            comment=None,
73
                            tag="Extend definition",
74
                        ))
75
                else:
76 1
                    node['node'].append(
77
                        dict(
78
                            value_id=child.get('test_ref'),
79
                            value=child.get('result'),
80
                            negate=negate_status,
81
                            comment=None,
82
                            tag="Test",
83
                            test_result_details=self.test_info_parser.get_info_about_test(
84
                                child.get('test_ref')),
85
                        ))
86 1
        return node
87
88 1
    def _fill_extend_definition(self, scan):
89 1
        out = dict(definitions=[])
90 1
        for definition in scan['definitions']:
91 1
            nodes = []
92 1
            for value in definition['node']:
93 1
                nodes.append(self._operator_as_child(value, scan))
94 1
            out['definitions'].append(
95
                dict(
96
                    id=definition['id'],
97
                    comment=definition['comment'],
98
                    node=nodes,
99
                ))
100 1
        return out
101
102 1
    def _operator_as_child(self, value, scan):
103 1
        out = dict(
104
            operator=value['operator'],
105
            negate=value['negate'],
106
            result=value['result'],
107
            comment=value['comment'],
108
            tag=value['tag'],
109
            node=[],
110
        )
111 1
        for child in value['node']:
112 1
            if 'operator' in child:
113 1
                out['node'].append(self._operator_as_child(child, scan))
114 1
            elif 'extend_definition' in child:
115 1
                out['node'].append(
116
                    self._find_definition_by_id(
117
                        scan,
118
                        child['extend_definition'],
119
                        child['negate'],
120
                        child['comment'],
121
                        child['tag'],
122
                    ))
123
            # look at this
124 1
            elif 'value_id' in child:
125 1
                out['node'].append(child)
126
            else:
127
                raise ValueError('error - unknown child')
128 1
        return out
129
130 1
    def _find_definition_by_id(self, scan, id, negate_status, comment, tag):
131 1
        for definition in scan['definitions']:
132 1
            if definition['id'] == id:
133 1
                definition['node'][0]['negate'] = negate_status
134 1
                definition['node'][0]['comment'] = comment
135 1
                definition['node'][0]['tag'] = tag
136
                return self._operator_as_child(definition['node'][0], scan)
137