File size: 2,797 Bytes
d439dc1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# -*- coding: utf-8 -*-
# MinIO Python Library for Amazon S3 Compatible Cloud Storage, (C)
# 2020 MinIO, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""XML utility module."""

from __future__ import absolute_import

import io
from xml.etree import ElementTree as ET

_S3_NAMESPACE = "http://s3.amazonaws.com/doc/2006-03-01/"


def Element(tag, namespace=_S3_NAMESPACE):  # pylint: disable=invalid-name
    """Create ElementTree.Element with tag and namespace."""
    return ET.Element(tag, {'xmlns': namespace} if namespace else {})


def SubElement(parent, tag, text=None):  # pylint: disable=invalid-name
    """Create ElementTree.SubElement on parent with tag and text."""
    element = ET.SubElement(parent, tag)
    if text is not None:
        element.text = text
    return element


def _get_namespace(element):
    """Exact namespace if found."""
    start = element.tag.find("{")
    if start < 0:
        return ""
    start += 1
    end = element.tag.find("}")
    if end < 0:
        return ""
    return element.tag[start:end]


def findall(element, name):
    """Namespace aware ElementTree.Element.findall()."""
    namespace = _get_namespace(element)
    return element.findall(
        "ns:" + name if namespace else name,
        {"ns": namespace} if namespace else {},
    )


def find(element, name):
    """Namespace aware ElementTree.Element.find()."""
    namespace = _get_namespace(element)
    return element.find(
        "ns:" + name if namespace else name,
        {"ns": namespace} if namespace else {},
    )


def findtext(element, name, strict=False):
    """
    Namespace aware ElementTree.Element.findtext() with strict flag
    raises ValueError if element name not exist.
    """
    element = find(element, name)
    if element is None:
        if strict:
            raise ValueError("XML element <{0}> not found".format(name))
        return None
    return element.text


def unmarshal(cls, xmlstring):
    """Unmarshal given XML string to an object of passed class."""
    return cls.fromxml(ET.fromstring(xmlstring))


def marshal(obj):
    """Get XML data as bytes of ElementTree.Element."""
    data = io.BytesIO()
    ET.ElementTree(obj.toxml(None)).write(
        data, encoding=None, xml_declaration=False,
    )
    return data.getvalue()