Passed
Pull Request — master (#173)
by Jan
05:08 queued 01:33
created

oval_graph.oval_tree.builder   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
eloc 67
dl 0
loc 99
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A Builder.dict_to_oval_tree() 0 29 2
A Builder._definition_dict_to_node() 0 27 3
A Builder.dict_of_rule_to_oval_tree() 0 23 1
A Builder._get_id_defintion() 0 5 2
1 1
import uuid
2
3 1
from .oval_node import OvalNode
4
5
6 1
class Builder:
7
    """The Builder object build OVAL tree from dict.
8
    """
9
10 1
    @staticmethod
11
    def _definition_dict_to_node(dict_of_definition):
12 1
        children = []
13 1
        for child in dict_of_definition['node']:
14 1
            if 'operator' in child:
15 1
                children.append(
16
                    Builder._definition_dict_to_node(child))
17
            else:
18 1
                children.append(
19
                    OvalNode(
20
                        node_id=child['value_id'],
21
                        node_type='value',
22
                        value=child['value'],
23
                        negation=child['negate'],
24
                        comment=child['comment'],
25
                        tag=child['tag'],
26
                        test_result_details=child['test_result_details'],
27
                    ))
28
29 1
        return OvalNode(
30
            node_id=Builder._get_id_defintion(dict_of_definition),
31
            node_type='operator',
32
            value=dict_of_definition['operator'],
33
            negation=dict_of_definition['negate'],
34
            comment=dict_of_definition['comment'],
35
            tag=dict_of_definition['tag'],
36
            children=children,
37
        )
38
39 1
    @staticmethod
40
    def _get_id_defintion(dict_of_definition):
41 1
        if 'definition_id' in dict_of_definition:
42 1
            return dict_of_definition['definition_id']
43 1
        return str(uuid.uuid4())
44
45 1
    @staticmethod
46
    def dict_of_rule_to_oval_tree(rule):
47
        """Converts the dict of rule to OVAL tree.
48
49
        Args:
50
            rule (dict): rule dictionary from an ARF file obtained
51
                         parsed by the ARF xml parser
52
53
        Returns:
54
            OvalNode.
55
        """
56 1
        dict_of_definition = rule['definition']
57 1
        dict_of_definition['node']['definition_id'] = rule['definition_id']
58 1
        return OvalNode(
59
            node_id=rule['rule_id'],
60
            node_type='operator',
61
            value='and',
62
            negation=False,
63
            comment=dict_of_definition['comment'],
64
            tag='Rule',
65
            children=[
66
                Builder._definition_dict_to_node(
67
                    dict_of_definition['node'])],
68
        )
69
70 1
    @staticmethod
71
    def dict_to_oval_tree(dict_of_tree):
72
        """Converts the dict of OVAL tree to OvalNode.
73
74
        Args:
75
            dict_of_tree (dict): dict of OVAL tree
76
77
        Returns:
78
            OvalNode.
79
        """
80 1
        if dict_of_tree['child'] is None:
81 1
            return OvalNode(
82
                node_id=dict_of_tree['node_id'],
83
                node_type=dict_of_tree['type'],
84
                value=dict_of_tree['value'],
85
                negation=dict_of_tree['negation'],
86
                comment=dict_of_tree['comment'],
87
                tag=dict_of_tree['tag'],
88
                test_result_details=dict_of_tree['test_result_details']
89
            )
90 1
        return OvalNode(
91
            node_id=dict_of_tree['node_id'],
92
            node_type=dict_of_tree['type'],
93
            value=dict_of_tree['value'],
94
            negation=dict_of_tree['negation'],
95
            comment=dict_of_tree['comment'],
96
            tag=dict_of_tree['tag'],
97
            test_result_details=dict_of_tree['test_result_details'],
98
            children=[Builder.dict_to_oval_tree(i) for i in dict_of_tree['child']]
99
        )
100