Revision 4f650d54 ncclient/content.py

b/ncclient/content.py
53 53
    @staticmethod
54 54
    def Element(spec):
55 55
        """DictTree -> Element
56
        
56

  
57 57
        :type spec: :obj:`dict` or :obj:`string` or :class:`~xml.etree.ElementTree.Element`
58

  
58 59
        :rtype: :class:`~xml.etree.ElementTree.Element`
59 60
        """
60 61
        if iselement(spec):
......
64 65
        if not isinstance(spec, dict):
65 66
            raise ContentError("Invalid tree spec")
66 67
        if 'tag' in spec:
67
            ele = ET.Element(spec.get('tag'), spec.get('attributes', {}))
68
            ele = ET.Element(spec.get('tag'), spec.get('attrib', {}))
68 69
            ele.text = spec.get('text', '')
69 70
            ele.tail = spec.get('tail', '')
70 71
            subtree = spec.get('subtree', [])
......
78 79
            return ET.Comment(spec.get('comment'))
79 80
        else:
80 81
            raise ContentError('Invalid tree spec')
81
    
82

  
82 83
    @staticmethod
83 84
    def XML(spec, encoding='UTF-8'):
84 85
        """DictTree -> XML
85
        
86

  
86 87
        :type spec: :obj:`dict` or :obj:`string` or :class:`~xml.etree.ElementTree.Element`
88

  
87 89
        :arg encoding: chraracter encoding
90

  
88 91
        :rtype: string
89 92
        """
90 93
        return Element.XML(DictTree.Element(spec), encoding)
91 94

  
92 95
class Element:
93
    
96

  
94 97
    @staticmethod
95 98
    def DictTree(ele):
96 99
        """DictTree -> Element
97
        
100

  
98 101
        :type spec: :class:`~xml.etree.ElementTree.Element`
99 102
        :rtype: :obj:`dict`
100 103
        """
......
105 108
            'tail': ele.tail,
106 109
            'subtree': [ Element.DictTree(child) for child in root.getchildren() ]
107 110
        }
108
    
111

  
109 112
    @staticmethod
110 113
    def XML(ele, encoding='UTF-8'):
111 114
        """Element -> XML
112
        
115

  
113 116
        :type spec: :class:`~xml.etree.ElementTree.Element`
114 117
        :arg encoding: character encoding
115 118
        :rtype: :obj:`string`
......
121 124
            return '<?xml version="1.0" encoding="%s"?>%s' % (encoding, xml)
122 125

  
123 126
class XML:
124
    
127

  
125 128
    @staticmethod
126 129
    def DictTree(xml):
127 130
        """XML -> DictTree
128
        
131

  
129 132
        :type spec: :obj:`string`
130 133
        :rtype: :obj:`dict`
131 134
        """
132 135
        return Element.DictTree(XML.Element(xml))
133
    
136

  
134 137
    @staticmethod
135 138
    def Element(xml):
136 139
        """XML -> Element
137
        
140

  
138 141
        :type xml: :obj:`string`
139 142
        :rtype: :class:`~xml.etree.ElementTree.Element`
140 143
        """
......
153 156

  
154 157
def find(ele, tag, nslist=[]):
155 158
    """If `nslist` is empty, same as :meth:`xml.etree.ElementTree.Element.find`. If it is not, `tag` is interpreted as an unqualified name and qualified using each item in `nslist`. The first match is returned.
156
    
159

  
157 160
    :arg nslist: optional list of namespaces
158 161
    """
159 162
    if nslist:
......
166 169

  
167 170
def parse_root(raw):
168 171
    """Efficiently parses the root element of an XML document.
169
    
172

  
170 173
    :type raw: string
171 174
    :returns: a tuple of `(tag, attributes)`, where `tag` is the (qualified) name of the element and `attributes` is a dictionary of its attributes.
172 175
    """
......
176 179

  
177 180
def validated_element(rep, tag=None, attrs=None, text=None):
178 181
    """Checks if the root element meets the supplied criteria. Returns a :class:`~xml.etree.ElementTree.Element` instance if so, otherwise raises :exc:`ContentError`.
179
    
182

  
180 183
    :arg tag: tag name or a list of allowable tag names
181 184
    :arg attrs: list of required attribute names, each item may be a list of allowable alternatives
182 185
    :arg text: textual content to match
183 186
    :type rep: :obj:`dict` or :obj:`string` or :class:`~xml.etree.ElementTree.Element`
184
    :see: :ref:`dtree`
185 187
    """
186 188
    ele = dtree2ele(rep)
187 189
    err = False

Also available in: Unified diff