Project

General

Profile

1 21 aaronmk
# XML DOM tree manipulation
2
3 73 aaronmk
import cgi
4
from HTMLParser import HTMLParser
5 3632 aaronmk
import itertools
6 1809 aaronmk
import re
7 21 aaronmk
from xml.dom import Node
8 299 aaronmk
import xml.dom.minidom as minidom
9 21 aaronmk
10 4331 aaronmk
import lists
11 73 aaronmk
import strings
12 331 aaronmk
import util
13 73 aaronmk
14 840 aaronmk
##### Escaping input
15
16 73 aaronmk
def escape(str_):
17
    return strings.to_unicode(cgi.escape(str_, True)).encode('ascii',
18
        'xmlcharrefreplace')
19
20
def unescape(str_): return HTMLParser().unescape(str_)
21
22 1814 aaronmk
##### Names
23
24
def strip_namespace(name):
25
    namespace, sep, base = name.partition(':')
26
    if sep != '': return base
27
    else: return name
28
29 2431 aaronmk
##### Nodes
30
31
def is_node(value): return isinstance(value, Node)
32
33 2008 aaronmk
##### Replacing a node
34
35
def remove(node): node.parentNode.removeChild(node)
36
37
def replace(old, new):
38
    '''@param new Node|None'''
39 3329 aaronmk
    assert old.parentNode != None # not removed from parent tree
40 4331 aaronmk
    if new == None: new = []
41
    else: new = lists.mk_seq(new)
42 3329 aaronmk
43 4331 aaronmk
    olds_parent = old.parentNode
44
    if not new: olds_parent.removeChild(old)
45
    else: # at least one node
46
        last = new.pop(-1)
47
        olds_parent.replaceChild(last, old) # note arg order reversed
48
        for node in reversed(new):
49
            olds_parent.insertBefore(node, last) # there is no insertAfter()
50 2008 aaronmk
51 4234 aaronmk
def bool2str(val):
52
    '''For use with replace_with_text()'''
53
    if val: return '1'
54
    else: return None # remove node
55
56 2008 aaronmk
def replace_with_text(node, new):
57 3225 aaronmk
    '''
58
    @return The *new* node
59
    '''
60 4235 aaronmk
    if isinstance(new, bool): new = bool2str(new)
61 2008 aaronmk
    if util.is_str(new): new = node.ownerDocument.createTextNode(new)
62
    replace(node, new)
63 3225 aaronmk
    return new
64 2008 aaronmk
65 840 aaronmk
##### Element node contents
66 455 aaronmk
67 840 aaronmk
def is_elem(node): return node.nodeType == Node.ELEMENT_NODE
68
69 453 aaronmk
def is_completely_empty(node): return node.firstChild == None
70 135 aaronmk
71 301 aaronmk
def has_one_child(node):
72
    return node.firstChild != None and node.firstChild.nextSibling == None
73
74 4330 aaronmk
def only_child(node):
75
    if not has_one_child(node):
76
        raise Exception('Must contain only one child:\n'+str(node))
77
    return node.firstChild
78
79 840 aaronmk
def is_simple(node):
80
    '''Whether every child recursively has no more than one child'''
81
    return (not is_elem(node) or is_completely_empty(node)
82
        or (has_one_child(node) and is_simple(node.firstChild)))
83
84 305 aaronmk
class NodeIter:
85
    def __init__(self, node): self.child = node.firstChild
86
87
    def __iter__(self): return self
88
89
    def curr(self):
90
        if self.child != None: return self.child
91
        raise StopIteration
92
93
    def next(self):
94
        child = self.curr()
95
        self.child = self.child.nextSibling
96
        return child
97
98 840 aaronmk
##### Comments
99 455 aaronmk
100 453 aaronmk
def is_comment(node): return node.nodeType == Node.COMMENT_NODE
101
102
def is_empty(node):
103
    for child in NodeIter(node):
104 4038 aaronmk
        if not (is_whitespace(child) or is_comment(child)): return False
105 453 aaronmk
    return True
106
107 1809 aaronmk
def clean_comment(str_):
108
    '''Sanitizes comment node contents. Strips invalid strings.'''
109
    return re.sub(r'-{2,}', r'-', str_) # comments can't contain '--'
110
111
def mk_comment(doc, str_): return doc.createComment(clean_comment(str_))
112
113 840 aaronmk
##### Child nodes that are elements
114 455 aaronmk
115 21 aaronmk
class NodeElemIter:
116
    def __init__(self, node): self.child = node.firstChild
117
118
    def __iter__(self): return self
119
120
    def curr(self):
121
        while self.child != None:
122 298 aaronmk
            if is_elem(self.child): return self.child
123 21 aaronmk
            self.child = self.child.nextSibling
124
        raise StopIteration
125
126
    def next(self):
127
        child = self.curr()
128
        self.child = self.child.nextSibling
129
        return child
130
131
def first_elem(node): return NodeElemIter(node).next()
132
133 450 aaronmk
def has_elems(node):
134
    try: first_elem(node); return True
135
    except StopIteration: return False
136
137 21 aaronmk
class NodeElemReverseIter:
138
    def __init__(self, node): self.child = node.lastChild
139
140
    def __iter__(self): return self
141
142
    def curr(self):
143
        while self.child != None:
144 298 aaronmk
            if is_elem(self.child): return self.child
145 21 aaronmk
            self.child = self.child.previousSibling
146
        raise StopIteration
147
148
    def next(self):
149
        child = self.curr()
150
        self.child = self.child.previousSibling
151
        return child
152
153
def last_elem(node): return NodeElemReverseIter(node).next()
154
155 4238 aaronmk
class NodeEntryIter:
156
    def __init__(self, node): self.iter_ = NodeElemIter(node)
157
158
    def __iter__(self): return self
159
160
    def next(self):
161
        entry = self.iter_.next()
162 4332 aaronmk
        values = list(NodeIter(entry))
163
        if not values: values = None
164
        elif len(values) == 1: values = values[0]
165
        return (entry.tagName, values)
166 4238 aaronmk
167 840 aaronmk
##### Parent nodes
168
169 1017 aaronmk
def parent(node):
170 999 aaronmk
    '''Does not treat the document object as the root node's parent, since it's
171
    not a true element node'''
172 1017 aaronmk
    parent_ = node.parentNode
173
    if parent_ != None and is_elem(parent_): return parent_
174
    else: return None
175
176
class NodeParentIter:
177
    '''See parent() for special treatment of root node.
178
    Note that the first element returned is the current node, not its parent.'''
179 21 aaronmk
    def __init__(self, node): self.node = node
180
181
    def __iter__(self): return self
182
183
    def curr(self):
184 1017 aaronmk
        if self.node != None: return self.node
185
        else: raise StopIteration
186 21 aaronmk
187
    def next(self):
188
        node = self.curr()
189 1017 aaronmk
        self.node = parent(self.node)
190 21 aaronmk
        return node
191
192 840 aaronmk
##### Element nodes containing text
193 661 aaronmk
194 298 aaronmk
def is_text_node(node): return node.nodeType == Node.TEXT_NODE
195
196 301 aaronmk
def is_text(node): return has_one_child(node) and is_text_node(node.firstChild)
197 21 aaronmk
198 2008 aaronmk
def value_node(node):
199 1832 aaronmk
    if is_elem(node):
200
        iter_ = NodeIter(node)
201
        util.skip(iter_, is_comment)
202 2008 aaronmk
        try: return iter_.next()
203 1832 aaronmk
        except StopIteration: return None
204 2008 aaronmk
    else: return node
205 21 aaronmk
206 2008 aaronmk
def value(node):
207 4029 aaronmk
    value_ = util.do_ignore_none(lambda n: n.nodeValue, value_node(node))
208 4034 aaronmk
    if value_ == strings.isspace_none_str: value_ = None # None equiv
209 4029 aaronmk
    return value_
210 2008 aaronmk
211 4031 aaronmk
def is_whitespace(node):
212
    return is_text_node(node) and (node.nodeValue == ''
213
        or node.nodeValue.isspace())
214 1720 aaronmk
215 143 aaronmk
def set_value(node, value):
216 2008 aaronmk
    value_node_ = value_node(node)
217
    if value != None:
218
        if value_node_ != None:
219
            value_node_.nodeValue = value
220
        else:
221
            assert is_elem(node)
222
            node.appendChild(node.ownerDocument.createTextNode(value))
223 2009 aaronmk
    elif value_node_ != None:
224 4029 aaronmk
        if is_elem(node): remove(value_node_)
225
        else:
226 4033 aaronmk
            if is_text_node(node): value = strings.isspace_none_str # None equiv
227 4029 aaronmk
            node.nodeValue = value
228 22 aaronmk
229 86 aaronmk
class NodeTextEntryIter:
230
    def __init__(self, node): self.iter_ = NodeElemIter(node)
231
232
    def __iter__(self): return self
233
234 1176 aaronmk
    def next(self):
235
        entry = self.iter_.next()
236 3632 aaronmk
        if is_empty(entry): value_ = None
237
        elif is_text(entry): value_ = value(entry)
238 4328 aaronmk
        else: value_ = only_child(entry)
239 839 aaronmk
        return (entry.tagName, value_)
240 86 aaronmk
241 792 aaronmk
def is_text_node_entry(val): return util.is_str(val[1])
242
243 3632 aaronmk
def non_empty(iterable):
244
    return itertools.ifilter(lambda i: i[1] != None, iterable)
245
246 792 aaronmk
class TextEntryOnlyIter(util.CheckedIter):
247
    def __init__(self, iterable):
248 3632 aaronmk
        util.CheckedIter.__init__(self, is_text_node_entry, non_empty(iterable))
249 792 aaronmk
250 1836 aaronmk
##### IDs
251
252
def get_id(node):
253
    '''If the node doesn't have an ID, assumes the node itself is the ID.
254
    @return None if the node doesn't have an ID or a value
255
    '''
256
    id_ = node.getAttribute('id')
257
    if id_ != '': return id_
258
    else: return value(node) # assume the node itself is the ID
259
260
def set_id(node, id_): node.setAttribute('id', id_)
261
262 2008 aaronmk
##### Child nodes
263 455 aaronmk
264 4318 aaronmk
def prune_empty(node):
265
    '''Removes node if it's empty'''
266
    if is_empty(node): remove(node)
267
268
def prune_children(node):
269
    '''Removes empty children'''
270
    for child in NodeElemIter(node): prune_empty(child)
271
272 4321 aaronmk
def prune(node):
273
    '''Removes empty children and then node if it's empty'''
274
    prune_children(node)
275
    prune_empty(node)
276
277 135 aaronmk
def set_child(node, name, value):
278
    '''Note: does not remove any existing child of the same name'''
279
    child = node.ownerDocument.createElement(name)
280 143 aaronmk
    set_value(child, value)
281 135 aaronmk
    node.appendChild(child)
282
283 1814 aaronmk
def by_tag_name(node, name, last_only=False, ignore_namespace=False):
284
    '''last_only optimization returns only the last matching node'''
285
    if ignore_namespace: filter_name = strip_namespace
286
    else: filter_name = lambda name: name
287
    name = filter_name(name)
288
289 22 aaronmk
    children = []
290 888 aaronmk
    if last_only: iter_ = NodeElemReverseIter(node)
291
    else: iter_ = NodeElemIter(node)
292
    for child in iter_:
293 1814 aaronmk
        if filter_name(child.tagName) == name:
294 22 aaronmk
            children.append(child)
295
            if last_only: break
296
    return children
297 28 aaronmk
298 3226 aaronmk
def merge(from_, into):
299 3331 aaronmk
    '''Merges two nodes of the same tag name and their newly-adjacent children.
300
    @post The into node is saved; the from_ node is deleted.
301
    '''
302
    if from_ == None or into == None: return # base case
303
    if from_.tagName != into.tagName: return # not mergeable
304
305
    from_first = from_.firstChild # save before merge
306 3226 aaronmk
    for child in NodeIter(from_): into.appendChild(child)
307
    remove(from_)
308
309 3331 aaronmk
    # Recurse
310
    merge(from_first, from_first.previousSibling) # = into.lastChild
311 3226 aaronmk
312 3331 aaronmk
def merge_by_name(root, name):
313
    '''Merges siblings in root with the given name'''
314
    children = by_tag_name(root, name)
315
    child0 = children.pop(0)
316
    for child in children: merge(child, child0)
317
318 840 aaronmk
##### XML documents
319 455 aaronmk
320 133 aaronmk
def create_doc(root='_'):
321 303 aaronmk
    return minidom.getDOMImplementation().createDocument(None, root, None)
322 133 aaronmk
323 840 aaronmk
##### Printing XML
324 455 aaronmk
325 304 aaronmk
prettyxml_config = dict(addindent='    ', newl='\n')
326 331 aaronmk
toprettyxml_config = prettyxml_config.copy()
327
util.rename_key(toprettyxml_config, 'addindent', 'indent')
328 304 aaronmk
329 840 aaronmk
##### minidom modifications
330 455 aaronmk
331 1720 aaronmk
#### Module
332
333 299 aaronmk
minidom._write_data = lambda writer, data: writer.write(escape(data))
334 73 aaronmk
335 305 aaronmk
minidom.Node.__iter__ = lambda self: NodeIter(self)
336
337 857 aaronmk
def __Node_str(self):
338 860 aaronmk
    return strings.remove_extra_newl(self.toprettyxml(**toprettyxml_config))
339 796 aaronmk
minidom.Node.__str__ = __Node_str
340
minidom.Node.__repr__ = __Node_str
341
minidom.Element.__repr__ = __Node_str
342 301 aaronmk
343 1720 aaronmk
#### Node
344 888 aaronmk
345 315 aaronmk
minidom.Node.pop = lambda self: self.removeChild(self.lastChild)
346
347
def __Node_clear(self):
348
    while not is_empty(self): self.pop()
349
minidom.Node.clear = __Node_clear
350
351 1720 aaronmk
#### Text
352
353
__Text_writexml_orig = minidom.Text.writexml
354
def __Text_writexml(self, *args, **kw_args):
355
    if is_whitespace(self): pass # we add our own whitespace
356
    else: __Text_writexml_orig(self, *args, **kw_args)
357
minidom.Text.writexml = __Text_writexml
358
359
#### Attr
360
361
def __Attr_str(self): return escape(self.name)+'="'+escape(self.value)+'"'
362
minidom.Attr.__str__ = __Attr_str
363
minidom.Attr.__repr__ = __Attr_str
364
365
#### Element
366
367 301 aaronmk
def __Element_write_opening(self, writer, indent='', addindent='', newl=''):
368 1563 aaronmk
    writer.write(indent+'<'+escape(self.tagName))
369 298 aaronmk
    for attr_idx in xrange(self.attributes.length):
370 891 aaronmk
        writer.write(' '+str(self.attributes.item(attr_idx)))
371 298 aaronmk
    writer.write('>'+newl)
372 301 aaronmk
minidom.Element.write_opening = __Element_write_opening
373 73 aaronmk
374 301 aaronmk
def __Element_write_closing(self, writer, indent='', addindent='', newl=''):
375 1563 aaronmk
    writer.write('</'+escape(self.tagName)+'>'+newl)
376 301 aaronmk
minidom.Element.write_closing = __Element_write_closing
377 298 aaronmk
378 1720 aaronmk
__Element_writexml_orig = minidom.Element.writexml
379 301 aaronmk
def __Element_writexml(self, writer, indent='', addindent='', newl=''):
380 306 aaronmk
    if isinstance(indent, int): indent = addindent*indent
381 774 aaronmk
    if is_simple(self):
382
        writer.write(indent)
383 1720 aaronmk
        __Element_writexml_orig(self, writer)
384 774 aaronmk
        writer.write(newl)
385 1720 aaronmk
    else: __Element_writexml_orig(self, writer, indent, addindent, newl)
386 301 aaronmk
minidom.Element.writexml = __Element_writexml
387 28 aaronmk
388 1720 aaronmk
#### Document
389
390 301 aaronmk
def __Document_write_opening(self, writer, indent='', addindent='', newl='',
391
    encoding=None):
392
    xmlDecl = '<?xml version="1.0" '
393
    if encoding != None: xmlDecl += 'encoding="'+escape(encoding)+'"'
394
    xmlDecl += '?>'+newl
395
    writer.write(xmlDecl)
396
    assert has_one_child(self)
397
    assert is_elem(self.firstChild)
398
    self.firstChild.write_opening(writer, indent, addindent, newl)
399
minidom.Document.write_opening = __Document_write_opening
400
401
def __Document_write_closing(self, writer, indent='', addindent='', newl=''):
402
    self.firstChild.write_closing(writer, indent, addindent, newl)
403
minidom.Document.write_closing = __Document_write_closing