summaryrefslogtreecommitdiffstats
path: root/src/lib/Bcfg2/Client/XML.py
blob: 4ba06abae7c3ca29b1ef2528f9b21c863c72ef73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'''XML lib compatibility layer for the Bcfg2 client'''

# library will use lxml, then builtin xml.etree, then ElementTree

# pylint: disable=E0611,W0611,W0613,C0103

try:
    from lxml.etree import Element, SubElement, tostring, XMLParser
    from lxml.etree import XMLSyntaxError as ParseError
    from lxml.etree import XML as _XML
    from Bcfg2.Compat import wraps
    driver = 'lxml'

    # libxml2 2.9.0+ doesn't parse 10M+ documents by default:
    # https://mail.gnome.org/archives/commits-list/2012-August/msg00645.html
    try:
        _parser = XMLParser(huge_tree=True)
    except TypeError:
        _parser = XMLParser()

    @wraps(_XML)
    def XML(val, **kwargs):
        """ unicode strings w/encoding declaration are not supported in
        recent lxml.etree, so we try to read XML, and if it fails we try
        encoding the string. """
        kwargs.setdefault('parser', _parser)
        try:
            return _XML(val, **kwargs)
        except ValueError:
            return _XML(val.encode(), **kwargs)
except ImportError:
    # lxml not available
    from xml.parsers.expat import ExpatError as ParseError
    try:
        import xml.etree.ElementTree
        Element = xml.etree.ElementTree.Element
        SubElement = xml.etree.ElementTree.SubElement
        XML = xml.etree.ElementTree.XML

        def tostring(el, encoding=None, xml_declaration=None):
            """ tostring implementation compatible with lxml """
            return xml.etree.ElementTree.tostring(el, encoding=encoding)

        driver = 'etree-py'
    except ImportError:
        try:
            from elementtree.ElementTree import Element, SubElement, XML, \
                tostring
            driver = 'etree'
            import elementtree.ElementTree
            Element = elementtree.ElementTree.Element
            SubElement = elementtree.ElementTree.SubElement
            XML = elementtree.ElementTree.XML

            def tostring(el, encoding=None, xml_declaration=None):
                """ tostring implementation compatible with lxml """
                return elementtree.ElementTree.tostring(el)

        except ImportError:
            print("Failed to load lxml, xml.etree or elementtree.ElementTree")
            print("Cannot continue")
            raise SystemExit(1)