Revision 19e7c7f6 ncclient/xml_.py
b/ncclient/xml_.py | ||
---|---|---|
41 | 41 |
from xml.etree import ElementTree |
42 | 42 |
# cElementTree uses ElementTree's _namespace_map, so that's ok |
43 | 43 |
ElementTree._namespace_map[uri] = prefix |
44 |
register_namespace.func_doc = "ElementTree's namespace map determines the prefixes for namespace URI's when serializing to XML. This method allows modifying this map to specify a prefix for a namespace URI." |
|
44 | 45 |
|
45 | 46 |
for (ns, pre) in { |
46 | 47 |
BASE_NS_1_0: 'nc', |
... | ... | |
51 | 52 |
}.items(): register_namespace(pre, ns) |
52 | 53 |
|
53 | 54 |
qualify = lambda tag, ns=BASE_NS_1_0: tag if ns is None else "{%s}%s" % (ns, tag) |
54 |
"""Qualify a tag name with a namespace, in :mod:`~xml.etree.ElementTree` fashion i.e. *{namespace}tagname*. |
|
55 |
|
|
56 |
:arg tag: name of the tag |
|
57 |
:type arg: `string` |
|
58 |
|
|
59 |
:arg ns: namespace to qualify with |
|
60 |
:type ns: `string` |
|
61 |
""" |
|
55 |
"""Qualify a *tag* name with a *namespace*, in :mod:`~xml.etree.ElementTree` fashion i.e. *{namespace}tagname*.""" |
|
62 | 56 |
|
63 | 57 |
def to_xml(ele, encoding="UTF-8"): |
64 |
"""Convert an `~xml.etree.ElementTree.Element` to XML. |
|
65 |
|
|
66 |
:arg ele: the `~xml.etree.ElementTree.Element` |
|
67 |
:arg encoding: character encoding |
|
68 |
:rtype: `string` |
|
69 |
""" |
|
58 |
"Convert and return the XML for an *ele* (:class:`~xml.etree.ElementTree.Element`) with specified *encoding*." |
|
70 | 59 |
xml = ET.tostring(ele, encoding) |
71 | 60 |
return xml if xml.startswith('<?xml') else '<?xml version="1.0" encoding="%s"?>%s' % (encoding, xml) |
72 | 61 |
|
73 | 62 |
def to_ele(x): |
74 |
"""Convert XML to `~xml.etree.ElementTree.Element`. If passed an `~xml.etree.ElementTree.Element` simply returns that. |
|
75 |
|
|
76 |
:arg x: the XML document or element |
|
77 |
:type x: `string` or `~xml.etree.ElementTree.Element` |
|
78 |
:rtype: `~xml.etree.ElementTree.Element` |
|
79 |
""" |
|
63 |
"Convert and return the :class:`~xml.etree.ElementTree.Element` for the XML document *x*. If *x* is already an :class:`~xml.etree.ElementTree.Element` simply returns that." |
|
80 | 64 |
return x if ET.iselement(x) else ET.fromstring(x) |
81 | 65 |
|
82 | 66 |
def parse_root(raw): |
83 |
"""Efficiently parses the root element of an XML document. |
|
84 |
|
|
85 |
:arg raw: XML document |
|
86 |
:returns: a tuple of ``(tag, attrib)``, where *tag* is the (qualified) name of the element and *attrib* is a dictionary of its attributes. |
|
87 |
:rtype: `tuple` |
|
88 |
""" |
|
67 |
"Efficiently parses the root element of a *raw* XML document, returning a tuple of its qualified name and attribute dictionary." |
|
89 | 68 |
fp = StringIO(raw) |
90 | 69 |
for event, element in ET.iterparse(fp, events=('start',)): |
91 | 70 |
return (element.tag, element.attrib) |
... | ... | |
93 | 72 |
def validated_element(x, tags=None, attrs=None): |
94 | 73 |
"""Checks if the root element of an XML document or Element meets the supplied criteria. |
95 | 74 |
|
96 |
:arg tags: allowable tag name or sequence of allowable alternatives |
|
97 |
:type tags: `string` or sequence of strings |
|
98 |
|
|
99 |
:arg attrs: list of required attributes, each of which may be a sequence of several allowable alternatives |
|
100 |
:type attrs: sequence of strings or sequence of sequences of strings |
|
101 |
|
|
102 |
:raises: `XMLError` if the requirements are not met |
|
75 |
*tags* if specified is either a single allowable tag name or sequence of allowable alternatives |
|
76 |
|
|
77 |
*attrs* if specified is a sequence of required attributes, each of which may be a sequence of several allowable alternatives |
|
78 |
|
|
79 |
Raises :exc:`XMLError` if the requirements are not met. |
|
103 | 80 |
""" |
104 | 81 |
ele = to_ele(x) |
105 | 82 |
if tags: |
Also available in: Unified diff