Revision bfcf8aea ncclient/xml_.py

b/ncclient/xml_.py
13 13
# limitations under the License.
14 14

  
15 15

  
16
"""The :mod:`xml_` module provides methods for creating, parsing, and dealing
17
with XML. It uses :mod:`~xml.etree.ElementTree`."""
16
"Methods for creating, parsing, and dealing with XML and ElementTree objects."
18 17

  
19 18
from cStringIO import StringIO
20 19
from xml.etree import cElementTree as ET
21 20

  
22 21
from ncclient import NCClientError
23 22

  
24
class XMLError(NCClientError):
25
    pass
23
class XMLError(NCClientError): pass
26 24

  
27 25
### Namespace-related
28 26

  
......
93 91
        return (element.tag, element.attrib)
94 92

  
95 93
def validated_element(x, tags=None, attrs=None):
96
    """Checks if the root element of an XML document or
97
    :class:`~xml.etree.ElementTree.Element` meets the supplied criteria. Raises
98
    :exc:`XMLError` if it is not met.
99
    
100
    :arg x: the XML document or :class:`~xml.etree.ElementTree.Element` to validate
101
    :arg tags: tag name or a sequence of allowable tag names
102
    :arg attrs: sequence of required attribute names, each item may be a list of allowable alternatives
103
    :returns: validated element
104
    :rtype: :class:`~xml.etree.ElementTree.Element`
105
    """
94
    "Checks if the root element of an XML document or Element meets the supplied criteria."
106 95
    ele = to_ele(x)
107 96
    if tags:
108 97
        if isinstance(tags, basestring):
......
119 108
                raise XMLError("Element [%s] does not have required attributes" % ele.tag)
120 109
    return ele
121 110

  
122
def new_ele(tag, attrs={}, **extra):
123
    return ET.Element(tag, attrs, **extra)
124 111

  
125
def sub_ele(parent, tag, attrs={}, **extra):
126
    return ET.SubElement(parent, tag, attrs, **extra)
112
new_ele = lambda tag, attrs={}, **extra: ET.Element(tag, attrs, **extra)
113

  
114
sub_ele = lambda parent, tag, attrs={}, **extra: ET.SubElement(parent, tag, attrs, **extra)

Also available in: Unified diff