Test Failed
Pull Request — master (#40)
by Jan
03:16
created

openscap_report.scap_results_parser.description_parser   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 21.43%

Importance

Changes 0
Metric Value
wmc 18
eloc 44
dl 0
loc 50
ccs 9
cts 42
cp 0.2143
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B DescriptionParser.get_html_tag_as_string() 0 15 7
A DescriptionParser.__init__() 0 2 1
B DescriptionParser.get_full_description() 0 14 7
A DescriptionParser.get_html_attributes_as_string() 0 6 2
A DescriptionParser.replace_sub_tag() 0 2 1
1 1
from lxml import etree
2
3 1
from .namespaces import NAMESPACES
4
5
6 1
class DescriptionParser():
7 1
    def __init__(self, ref_values):
8
        self.ref_values = ref_values
9
10 1
    def replace_sub_tag(self, tag):
11
        return self.ref_values.get(tag.get("idref"))
12
13 1
    @staticmethod
14 1
    def get_html_attributes_as_string(attributes):
15
        out = ""
16
        for key, value in attributes.items():
17
            out += f" {key}=\"{value}\""
18
        return out
19
20 1
    def get_html_tag_as_string(self, tag):
21
        tag_name = etree.QName(tag).localname
22
        tag_text = tag.text
23
        tag_attributes = self.get_html_attributes_as_string(tag.attrib)
24
        for child in tag:
25
            if tag_text is None:
26
                tag_text = ""
27
            if child.prefix == "html":
28
                tag_text += self.get_html_tag_as_string(child)
29
            if etree.QName(child).localname == "sub":
30
                tag_text += self.replace_sub_tag(child)
31
            tag_text += child.tail if child.tail is not None else ""
32
        if tag_text is not None:
33
            return f"<{tag_name}{tag_attributes}>{tag_text}</{tag_name}>"
34
        return f"<{tag_name}{tag_attributes}>"
35
36 1
    def get_full_description(self, rule):
37
        description = rule.find(".//xccdf:description", NAMESPACES)
38
        if description is None:
39
            return None
40
        str_description = description.text
41
        for child in description:
42
            if str_description is None:
43
                str_description = ""
44
            if child.prefix == "html":
45
                str_description += self.get_html_tag_as_string(child)
46
            if etree.QName(child).localname == "sub":
47
                str_description += self.replace_sub_tag(child)
48
            str_description += child.tail if child.tail is not None else ""
49
        return str_description
50