Passed
Pull Request — master (#173)
by Jan
05:28
created

oval_graph.oval_tree.builder   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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