Passed
Branch master (2b673d)
by Matěj
03:01
created

ssg.build_cpe   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
eloc 25
dl 0
loc 57
ccs 0
cts 24
cp 0
rs 10
c 0
b 0
f 0

3 Functions

Rating   Name   Duplication   Size   Complexity  
A extract_env_obj() 0 10 3
A extract_subelement() 0 15 4
A extract_referred_nodes() 0 19 5
1
"""
2
Common functions for building CPEs
3
"""
4
5
from __future__ import absolute_import
6
from __future__ import print_function
7
8
9
def extract_subelement(objects, sub_elem_type):
10
    """
11
    From a collection of element objects, return the value of
12
    the first attribute of name sub_elem_type found.
13
14
    This is useful when the object is a single element and
15
    we wish to query some external reference identifier
16
    in the subtree of that element.
17
    """
18
19
    for obj in objects:
20
        for subelement in obj.getiterator():
21
            if subelement.get(sub_elem_type):
22
                sub_element = subelement.get(sub_elem_type)
23
                return sub_element
24
25
26
def extract_env_obj(objects, local_var):
27
    """
28
    From a collection of objects, return the object with id matching
29
    the object_ref of the local variable.
30
    """
31
32
    for obj in objects:
33
        env_id = extract_subelement(local_var, 'object_ref')
34
        if env_id == obj.get('id'):
35
            return obj
36
37
38
def extract_referred_nodes(tree_with_refs, tree_with_ids, attrname):
39
    """
40
    Return the elements in tree_with_ids which are referenced
41
    from tree_with_refs via the element attribute 'attrname'.
42
    """
43
44
    reflist = []
45
    elementlist = []
46
47
    for element in tree_with_refs.getiterator():
48
        value = element.get(attrname)
49
        if value is not None:
50
            reflist.append(value)
51
52
    for element in tree_with_ids.getiterator():
53
        if element.get("id") in reflist:
54
            elementlist.append(element)
55
56
    return elementlist
57