-
Notifications
You must be signed in to change notification settings - Fork 1
/
xmltramp3.py
executable file
·443 lines (355 loc) · 14.9 KB
/
xmltramp3.py
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#!/usr/bin/python3
"""xmltramp: Make XML documents easily accessible."""
from collections import defaultdict
from xml.sax import make_parser
from xml.sax.handler import (
EntityResolver, DTDHandler, ContentHandler, ErrorHandler, feature_namespaces
)
__version__ = "2.18"
__author__ = "Aaron Swartz"
__credits__ = "Many thanks to pjz, bitsko, and DanC."
__copyright__ = "(C) 2003-2006 Aaron Swartz. GNU GPL 2."
__all__ = ['Element', 'Namespace', 'seed', 'load', 'parse', 'indent']
def isstr(f):
return isinstance(f, str)
def islst(f):
return isinstance(f, (tuple, list))
def iselement(x, n):
return isinstance(x, Element) and x._name == n
empty = {
'http://www.w3.org/1999/xhtml': (
'img', 'br', 'hr', 'meta', 'link', 'base', 'param', 'input', 'col', 'area'
),
'http://www.w3.org/2005/Atom': ('link', 'category'),
}
indent = '\t'
def quote(x, elt=True):
if elt and '<' in x and len(x) > 24 and x.find(']]>') == -1:
return f'<![CDATA[{x}]]>'
x = x.replace('&', '&').replace('<', '<').replace(']]>', ']]>')
if elt:
return x
return x.replace('"', '"')
def transpose(d):
return dict(zip(d.values(), d.keys()))
def delns(name):
return name[1] if islst(name) and name[0] is None else name
class Element:
def __init__(self, name, attrs=None, children=None, prefixes=None, value=None):
self._name = delns(name)
self._attrs = dict((delns(k), v) for k, v in attrs.items()) if attrs else {}
self._dir = children or []
prefixes = prefixes or {}
self._prefixes = transpose(prefixes)
self._dNS = prefixes.get(None, None) if prefixes else None
if value is not None:
if islst(value):
self(*value)
elif isinstance(value, dict):
self(**value)
elif value:
self._dir.append(value)
def __repr__(self, recursive=0, multiline=0, inner=0, inprefixes=None):
def qname(name, inprefixes):
if islst(name):
if inprefixes[name[0]] is not None:
return inprefixes[name[0]] + ':' + name[1]
else:
return name[1]
else:
return name
def arep(a, inprefixes, addns=1):
out = ''
for p in self._prefixes:
if p not in list(inprefixes):
if addns:
out += ' xmlns'
if self._prefixes[p]:
out += ':' + self._prefixes[p]
out += '="' + quote(p, False) + '"'
inprefixes[p] = self._prefixes[p]
for k, v in a.items():
out += ' ' + qname(k, inprefixes) + '="' + quote(v, False) + '"'
return out
inprefixes = inprefixes or {'http://www.w3.org/XML/1998/namespace': 'xml'}
# need to call first to set inprefixes:
if not inner:
attributes = arep(self._attrs, inprefixes, recursive)
out = '<' + qname(self._name, inprefixes) + attributes
else:
out = ''
if not self._dir and islst(self._name) and self._name[1] in empty.get(self._name[0], []):
if not inner:
out += ' />'
return out
if not inner:
out += '>'
if recursive:
indent_content = multiline and any(isinstance(x, Element) for x in self._dir)
for x in self._dir:
if indent_content:
out += '\n' + indent * recursive
if isstr(x):
out += quote(x)
elif isinstance(x, Element):
out += x.__repr__(recursive + 1, multiline, 0, inprefixes.copy())
else:
raise TypeError(f"I wasn't expecting '{x!r}'")
if indent_content:
out += '\n' + indent * (recursive - 1)
else:
if self._dir:
out += '...'
if not inner:
out += '</' + qname(self._name, inprefixes) + '>'
return out
def __str__(self):
return ''.join(str(x) for x in self._dir)
def _mkns(self, n):
return (self._dNS, n) if self._dNS and not islst(n) else n
def _first(self, n, default=None):
n = self._mkns(n)
for x in self._dir:
if iselement(x, n):
return x
return default
def __getattr__(self, n):
if n[0] == '_':
raise AttributeError(f"Use foo['{n}'] to access the child element.")
if child := self._first(n):
return child
raise AttributeError(f"No child element named {n!r}")
def __hasattr__(self, n):
return bool(self._first(n))
def __setattr__(self, n, v):
if n[0] == '_':
self.__dict__[n] = v
else:
self[n] = v
def _getchild(self, n, default=None):
return self._first(n, default)
def __getitem__(self, n):
if isinstance(n, int): # d[1] == d._dir[1]
return self._dir[n]
elif isinstance(n, slice):
# numerical slices
if n.start is None or isinstance(n.start, int):
return self._dir[n]
# d['foo':] == all <foo>s
n = self._mkns(n.start)
return [x for x in self._dir if iselement(x, n)]
else: # d['foo'] == first <foo>
if child := self._getchild(n):
return child
raise KeyError(n)
def __setitem__(self, n, v):
if isinstance(n, int): # d[1]
self._dir[n] = v
elif n is None: # d[None] appends v
self._dir.append(v)
elif isinstance(n, slice): # d['foo':] adds a new foo
self._dir.append(Element(self._mkns(n.start),
prefixes=transpose(self._prefixes), value=v))
else: # d["foo"] replaces first <foo> and deletes rest
n = self._mkns(n)
nv = Element(n, prefixes=transpose(self._prefixes), value=v)
idx = [i for (i, x) in enumerate(self._dir) if iselement(x, n)]
if idx:
self._dir[idx[0]] = nv
for x in idx[1:]:
del self._dir[x]
else:
self._dir.append(nv)
def __delitem__(self, n):
if isinstance(n, int):
del self._dir[n]
elif isinstance(n, slice): # delete all <foo>s
n = self._mkns(n.start)
for i in range(len(self)):
if self[i]._name == n:
del self[i]
elif isinstance(n, Element): # delete exactly element n
try:
self._dir.remove(n)
except ValueError:
pass
else: # delete first foo
n = self._mkns(n)
for i in range(len(self)):
if self[i]._name == n:
del self[i]
break
def __call__(self, *_pos, **_set):
if _set: # e(a=1, b=2) sets attrs
for k, v in _set.items():
self._attrs[k] = v
if not _pos: # without positional args allows chaining
return self
if len(_pos) > 1: # e('a', 1, 'b', 2) sets attrs pairwise
for i in range(0, len(_pos), 2):
self._attrs[_pos[i]] = _pos[i + 1]
return self
if len(_pos) == 1: # e('attr') returns the attr value
return self._attrs[_pos[0]]
if len(_pos) == 0: # e() returns the complete attr dict
return self._attrs
def __len__(self):
return len(self._dir)
def __bool__(self):
return True # prevents Elements without children from being falsy
def __contains__(self, n):
# true if self has a child element n
return self._getchild(n) is not None
def _new(self, n, v=None):
# appends element n with content v and returns it
self[n:] = v
return self._dir[-1]
class Namespace:
def __init__(self, uri):
self.__uri = uri
def __getattr__(self, n):
return (self.__uri, n)
def __getitem__(self, n):
return (self.__uri, n)
def _prefix(self, p):
return {p: self.__uri}
class Seeder(EntityResolver, DTDHandler, ContentHandler, ErrorHandler):
def __init__(self):
self.stack = []
self.ch = ''
self.prefixes = defaultdict(list)
ContentHandler.__init__(self)
def startPrefixMapping(self, prefix, uri):
self.prefixes[prefix].append(uri)
def endPrefixMapping(self, prefix):
self.prefixes[prefix].pop()
def startElementNS(self, name, qname, attrs):
ch = self.ch
self.ch = ''
if ch and not ch.isspace():
self.stack[-1]._dir.append(ch)
attrs = dict(attrs)
newprefixes = dict((k, v[-1]) for k, v in self.prefixes.items())
self.stack.append(Element(name, attrs, prefixes=newprefixes))
def characters(self, ch):
self.ch += ch
def endElementNS(self, name, qname):
ch = self.ch
self.ch = ''
if ch and not ch.isspace():
self.stack[-1]._dir.append(ch)
element = self.stack.pop()
if self.stack:
self.stack[-1]._dir.append(element)
else:
self.result = element
def seed(fileobj):
seeder = Seeder()
parser = make_parser()
parser.setFeature(feature_namespaces, 1)
parser.setContentHandler(seeder)
parser.parse(fileobj)
return seeder.result
def parse(text):
from io import StringIO
return seed(StringIO(text))
def load(url):
import urllib.request
return seed(urllib.request.urlopen(url))
def unittest():
parse('<doc>a<baz>f<b>o</b>ob<b>a</b>r</baz>a</doc>').__repr__(1, 1) == \
'<doc>\n\ta<baz>\n\t\tf<b>o</b>ob<b>a</b>r\n\t</baz>a\n</doc>'
assert str(parse("<doc />")) == ""
assert str(parse("<doc>I <b>love</b> you.</doc>")) == "I love you."
assert parse("<doc>\nmom\nwow\n</doc>")[0].strip() == "mom\nwow"
assert str(parse('<bing> <bang> <bong>center</bong> </bang> </bing>')) == "center"
assert str(parse('<doc>\xcf\x80</doc>')) == '\xcf\x80'
assert str(parse('<doc>äöü</doc>')) == 'äöü'
d = Element('foo', attrs={'foo': 'bar'}, children=['hit with a', Element('bar'), Element('bar')])
try:
d._doesnotexist
raise RuntimeError("Expected error, but found success. Damn.")
except AttributeError:
pass
assert d.bar._name == 'bar'
assert hasattr(d, 'bar')
assert 'bar' in d
assert 'foo' not in d
assert d._getchild('baz') is None
assert d._getchild('baz', 1) == 1
assert d('foo') == 'bar'
d(silly='yes')
assert d('silly') == 'yes'
assert d() == d._attrs
assert d[0] == 'hit with a'
d[0] = 'ice cream'
assert d[0] == 'ice cream'
del d[0]
assert d[0]._name == "bar"
assert len(d[:]) == len(d._dir)
assert len(d[1:]) == len(d._dir) - 1
assert len(d['bar':]) == 2
d['bar':] = 'baz'
assert len(d['bar':]) == 3
assert d['bar']._name == 'bar'
d[-1](sillier='no')
assert d[-1].__repr__(1) == '<bar sillier="no">baz</bar>'
del d[d[-1]]
assert len(d['bar':]) == 2
assert d[-1].__repr__(1) == '<bar></bar>'
doc = Namespace("http://example.org/bar")
bbc = Namespace("http://example.org/bbc")
dc = Namespace("http://purl.org/dc/elements/1.1/")
d = parse("""<doc version="2.7182818284590451"
xmlns="http://example.org/bar"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:bbc="http://example.org/bbc">
<author>John Polk and John Palfrey</author>
<dc:creator>John Polk</dc:creator>
<dc:creator>John Palfrey</dc:creator>
<bbc:show bbc:station="4">Buffy</bbc:show>
</doc>""")
global indent
indent = ' '
assert repr(d) == '<doc version="2.7182818284590451">...</doc>'
assert d.__repr__(1) == '<doc xmlns="http://example.org/bar" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:bbc="http://example.org/bbc" version="2.7182818284590451"><author>John Polk and John Palfrey</author><dc:creator>John Polk</dc:creator><dc:creator>John Palfrey</dc:creator><bbc:show bbc:station="4">Buffy</bbc:show></doc>'
assert d.__repr__(1, inner=1) == '<author xmlns="http://example.org/bar" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:bbc="http://example.org/bbc">John Polk and John Palfrey</author><dc:creator xmlns="http://example.org/bar" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:bbc="http://example.org/bbc">John Polk</dc:creator><dc:creator xmlns="http://example.org/bar" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:bbc="http://example.org/bbc">John Palfrey</dc:creator><bbc:show xmlns="http://example.org/bar" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:bbc="http://example.org/bbc" bbc:station="4">Buffy</bbc:show>'
assert d.__repr__(1, 1) == '''<doc xmlns="http://example.org/bar" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:bbc="http://example.org/bbc" version="2.7182818284590451">
<author>John Polk and John Palfrey</author>
<dc:creator>John Polk</dc:creator>
<dc:creator>John Palfrey</dc:creator>
<bbc:show bbc:station="4">Buffy</bbc:show>
</doc>'''
assert repr(parse("<doc xml:lang='en' />")) == '<doc xml:lang="en"></doc>'
assert str(d.author) == str(d['author']) == "John Polk and John Palfrey"
assert d.author._name == doc.author
assert str(d[dc.creator]) == "John Polk"
assert d[dc.creator]._name == dc.creator
assert str(d[dc.creator:][1]) == "John Palfrey"
d[dc.creator] = "Me!!!"
assert str(d[dc.creator]) == "Me!!!"
assert len(d[dc.creator:]) == 1
d[dc.creator:] = "You!!!"
assert len(d[dc.creator:]) == 2
assert d[bbc.show](bbc.station) == "4"
d[bbc.show](bbc.station, "5")
assert d[bbc.show](bbc.station) == "5"
e = Element('e')
e.c = '<img src="foo">'
assert e.__repr__(1) == '<e><c><img src="foo"></c></e>'
e.c = '2 > 4'
assert e.__repr__(1) == '<e><c>2 > 4</c></e>'
assert e.__repr__(1, inner=1) == '<c>2 > 4</c>'
e.c = 'CDATA sections are <em>closed</em> with ]]>.'
assert e.__repr__(1) == '<e><c>CDATA sections are <em>closed</em> with ]]>.</c></e>'
e.c = parse('<div xmlns="http://www.w3.org/1999/xhtml">i<br /><span></span>love<br />you</div>')
assert e.__repr__(1) == '<e><c><div xmlns="http://www.w3.org/1999/xhtml">i<br /><span></span>love<br />you</div></c></e>'
e = Element('e')
e('c', 'that "sucks"')
assert e.__repr__(1) == '<e c="that "sucks""></e>'
assert quote("]]>") == "]]>"
assert quote('< dkdkdsd dkd sksdksdfsd fsdfdsf]]> kfdfkg >') == '< dkdkdsd dkd sksdksdfsd fsdfdsf]]> kfdfkg >'
assert parse('<x a="<"></x>').__repr__(1) == '<x a="<"></x>'
assert parse('<a xmlns="http://a"><b xmlns="http://b"/></a>').__repr__(1) == '<a xmlns="http://a"><b xmlns="http://b"></b></a>'
if __name__ == '__main__':
unittest()