Test Failed
Push — master ( aa538d...df2cf1 )
by Jan
02:59 queued 10s
created

ssg.xml.add_xhtml_namespace()   A

Complexity

Conditions 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.2963

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 1
dl 0
loc 8
ccs 1
cts 3
cp 0.3333
crap 1.2963
rs 10
c 0
b 0
f 0
1 2
from __future__ import absolute_import
2 2
from __future__ import print_function
3
4 2
import platform
5 2
import re
6
7 2
from .constants import xml_version, oval_header, timestamp, PREFIX_TO_NS
8
9
10 2
try:
11 2
    from xml.etree import cElementTree as ElementTree
12
except ImportError:
13
    from xml.etree import ElementTree as ElementTree
14
15
16 2
def oval_generated_header(product_name, schema_version, ssg_version):
17 2
    return xml_version + oval_header + \
18
        """
19
    <generator>
20
        <oval:product_name>%s from SCAP Security Guide</oval:product_name>
21
        <oval:product_version>ssg: %s, python: %s</oval:product_version>
22
        <oval:schema_version>%s</oval:schema_version>
23
        <oval:timestamp>%s</oval:timestamp>
24
    </generator>""" % (product_name, ssg_version, platform.python_version(),
25
                       schema_version, timestamp)
26
27
28 2
def register_namespaces():
29
    """
30
    Register all possible namespaces
31
    """
32
    try:
33
        for prefix, uri in PREFIX_TO_NS.items():
34
            ElementTree.register_namespace(prefix, uri)
35
    except Exception:
36
        # Probably an old version of Python
37
        # Doesn't matter, as this is non-essential.
38
        pass
39
40
41 2
def open_xml(filename):
42
    """
43
    Given a filename, register all possible namespaces, and return the XML tree.
44
    """
45
    register_namespaces()
46
    return ElementTree.parse(filename)
47
48
49 2
def parse_file(filename):
50
    """
51
    Given a filename, return the root of the ElementTree
52
    """
53
    tree = open_xml(filename)
54
    return tree.getroot()
55
56
57 2
def map_elements_to_their_ids(tree, xpath_expr):
58
    """
59
    Given an ElementTree and an XPath expression,
60
    iterate through matching elements and create 1:1 id->element mapping.
61
62
    Raises AssertionError if a matching element doesn't have the ``id``
63
    attribute.
64
65
    Returns mapping as a dictionary
66
    """
67
    aggregated = {}
68
    for element in tree.findall(xpath_expr):
69
        element_id = element.get("id")
70
        assert element_id is not None
71
        aggregated[element_id] = element
72
    return aggregated
73
74
75 2
SSG_XHTML_TAGS = [
76
    'table', 'tr', 'th', 'td', 'ul', 'li', 'ol',
77
    'p', 'code', 'strong', 'b', 'em', 'i', 'pre', 'br', 'hr', 'small',
78
]
79
80
81 2
def add_xhtml_namespace(data):
82
    """
83
    Given a xml blob, adds the xhtml namespace to all relevant tags.
84
    """
85
    # Transform <tt> in <code>
86
    data = re.sub(r'<(\/)?tt(\/)?>', r'<\1code\2>', data)
87
    # Adds xhtml prefix to elements: <tag>, </tag>, <tag/>
88
    return re.sub(r'<(\/)?((?:%s).*?)(\/)?>' % "|".join(SSG_XHTML_TAGS), r'<\1xhtml:\2\3>', data)
89