minixml 0.6.3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- minixml-0.6.3/PKG-INFO +466 -0
- minixml-0.6.3/README.md +457 -0
- minixml-0.6.3/pyproject.toml +24 -0
- minixml-0.6.3/pyproject.toml.orig +25 -0
- minixml-0.6.3/src/minixml/__init__.py +8 -0
- minixml-0.6.3/src/minixml/element.py +263 -0
- minixml-0.6.3/src/minixml/io.py +104 -0
- minixml-0.6.3/src/minixml/py.typed +0 -0
minixml-0.6.3/PKG-INFO
ADDED
|
@@ -0,0 +1,466 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: minixml
|
|
3
|
+
Version: 0.6.3
|
|
4
|
+
Summary: Minimalist module for reading, writing, creating and editing an XML element tree.
|
|
5
|
+
Author: Per Kraulis
|
|
6
|
+
Author-email: Per Kraulis <per.kraulis@gmail.com>
|
|
7
|
+
Requires-Python: >=3.14
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
|
|
10
|
+
# minixml
|
|
11
|
+
|
|
12
|
+
Minimalist module for reading, writing, creating and editing an XML element tree.
|
|
13
|
+
|
|
14
|
+
This code has been lovingly hand-crafted. No AI tools were used in its development.
|
|
15
|
+
|
|
16
|
+
# Software
|
|
17
|
+
|
|
18
|
+
There are no dependencies except for the standard Python library.
|
|
19
|
+
|
|
20
|
+
# Usage
|
|
21
|
+
|
|
22
|
+
```python
|
|
23
|
+
import minixml
|
|
24
|
+
|
|
25
|
+
# The root element.
|
|
26
|
+
svg = minixml.Element("svg", width=320, height=130, xmlns="http://www.w3.org/2000/svg")
|
|
27
|
+
|
|
28
|
+
# A child element can be created from the parent.
|
|
29
|
+
# It is automatically appended to it as a child.
|
|
30
|
+
svg.create(
|
|
31
|
+
"rect",
|
|
32
|
+
width=300,
|
|
33
|
+
height=100,
|
|
34
|
+
x=10,
|
|
35
|
+
y=10,
|
|
36
|
+
style="fill:rgb(0,0,255);stroke-width:3;stroke:red",
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
# An element can also be created from scratch and subsequently appended.
|
|
40
|
+
t = minixml.Element("text", x=5, y=15, fill="red")
|
|
41
|
+
t.append("I love SVG!")
|
|
42
|
+
svg.append(t)
|
|
43
|
+
|
|
44
|
+
# Serialize the Element tree into XML code; the XML declaration is prepended.
|
|
45
|
+
xmlcode = minixml.unparse(svg, indent=2)
|
|
46
|
+
|
|
47
|
+
# Ensure that unparse and parse are symmetric functions.
|
|
48
|
+
svg2 = minixml.parse(xmlcode)
|
|
49
|
+
assert svg == svg2
|
|
50
|
+
|
|
51
|
+
# Display the result.
|
|
52
|
+
print(xmlcode)
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Produces:
|
|
56
|
+
|
|
57
|
+
```svg
|
|
58
|
+
<?xml version="1.0" encoding="utf-8"?>
|
|
59
|
+
<svg width="320" height="130" xmlns="http://www.w3.org/2000/svg">
|
|
60
|
+
<rect width="300" height="100" x="10" y="10" style="fill:rgb(0,0,255);stroke-width:3;stroke:red"/>
|
|
61
|
+
<text x="5" y="15" fill="red">I love SVG!</text>
|
|
62
|
+
</svg>
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
# API
|
|
66
|
+
|
|
67
|
+
The `minixml` namespace contains the features most client software will need:
|
|
68
|
+
|
|
69
|
+
* [Element](#element-objects) (class)
|
|
70
|
+
* [parse](#parse) (function)
|
|
71
|
+
* [read](#read) (function)
|
|
72
|
+
* [unparse](#unparse) (function)
|
|
73
|
+
* [write](#write) (function)
|
|
74
|
+
|
|
75
|
+
These have been imported from `minixml.element` and `minixml.io`, which in most
|
|
76
|
+
cases will not need to be used directly by client software.
|
|
77
|
+
|
|
78
|
+
The following was produced using **pydoc-markdown**
|
|
79
|
+
https://github.com/NiklasRosenstein/pydoc-markdown
|
|
80
|
+
|
|
81
|
+
<a id="minixml"></a>
|
|
82
|
+
|
|
83
|
+
# minixml
|
|
84
|
+
|
|
85
|
+
Minimalist package for reading, writing, creating and editing an XML element tree.
|
|
86
|
+
|
|
87
|
+
<a id="minixml.element"></a>
|
|
88
|
+
|
|
89
|
+
# minixml.element
|
|
90
|
+
|
|
91
|
+
Element class.
|
|
92
|
+
|
|
93
|
+
<a id="minixml.element.Element"></a>
|
|
94
|
+
|
|
95
|
+
## Element Objects
|
|
96
|
+
|
|
97
|
+
```python
|
|
98
|
+
class Element()
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
XML element having a tag, attributes, parent (if any) and children (if any).
|
|
102
|
+
|
|
103
|
+
<a id="minixml.element.Element.__init__"></a>
|
|
104
|
+
|
|
105
|
+
#### \_\_init\_\_
|
|
106
|
+
|
|
107
|
+
```python
|
|
108
|
+
def __init__(tag: str, *children, **attributes)
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
The instance is initialized as a root element (i.e. it has no parent).
|
|
112
|
+
|
|
113
|
+
<a id="minixml.element.Element.__str__"></a>
|
|
114
|
+
|
|
115
|
+
#### \_\_str\_\_
|
|
116
|
+
|
|
117
|
+
```python
|
|
118
|
+
def __str__()
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
Return the string representation of the element's starting tag.
|
|
122
|
+
|
|
123
|
+
<a id="minixml.element.Element.__getitem__"></a>
|
|
124
|
+
|
|
125
|
+
#### \_\_getitem\_\_
|
|
126
|
+
|
|
127
|
+
```python
|
|
128
|
+
def __getitem__(key)
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Get the value of the attribute in this element.
|
|
132
|
+
|
|
133
|
+
<a id="minixml.element.Element.__setitem__"></a>
|
|
134
|
+
|
|
135
|
+
#### \_\_setitem\_\_
|
|
136
|
+
|
|
137
|
+
```python
|
|
138
|
+
def __setitem__(key, value)
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
Set the value of the attribute in this element.
|
|
142
|
+
|
|
143
|
+
<a id="minixml.element.Element.__delitem__"></a>
|
|
144
|
+
|
|
145
|
+
#### \_\_delitem\_\_
|
|
146
|
+
|
|
147
|
+
```python
|
|
148
|
+
def __delitem__(key)
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
Delete the attribute in this element.
|
|
152
|
+
|
|
153
|
+
<a id="minixml.element.Element.__contains__"></a>
|
|
154
|
+
|
|
155
|
+
#### \_\_contains\_\_
|
|
156
|
+
|
|
157
|
+
```python
|
|
158
|
+
def __contains__(key)
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
Does this element have the given attribute?
|
|
162
|
+
|
|
163
|
+
<a id="minixml.element.Element.__iter__"></a>
|
|
164
|
+
|
|
165
|
+
#### \_\_iter\_\_
|
|
166
|
+
|
|
167
|
+
```python
|
|
168
|
+
def __iter__()
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
Iterate over the children of this element.
|
|
172
|
+
|
|
173
|
+
<a id="minixml.element.Element.__len__"></a>
|
|
174
|
+
|
|
175
|
+
#### \_\_len\_\_
|
|
176
|
+
|
|
177
|
+
```python
|
|
178
|
+
def __len__()
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
Return the number of children of this element.
|
|
182
|
+
|
|
183
|
+
<a id="minixml.element.Element.__eq__"></a>
|
|
184
|
+
|
|
185
|
+
#### \_\_eq\_\_
|
|
186
|
+
|
|
187
|
+
```python
|
|
188
|
+
def __eq__(other)
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
Are this element and the other equal, recursively? Ignores the parent.
|
|
192
|
+
|
|
193
|
+
<a id="minixml.element.Element.__iadd__"></a>
|
|
194
|
+
|
|
195
|
+
#### \_\_iadd\_\_
|
|
196
|
+
|
|
197
|
+
```python
|
|
198
|
+
def __iadd__(other)
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
Append the other to the end of the list of children of this other.
|
|
202
|
+
|
|
203
|
+
<a id="minixml.element.Element.tag"></a>
|
|
204
|
+
|
|
205
|
+
#### tag
|
|
206
|
+
|
|
207
|
+
```python
|
|
208
|
+
@property
|
|
209
|
+
def tag()
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
Return the tag of this element; read-only.
|
|
213
|
+
|
|
214
|
+
<a id="minixml.element.Element.parent"></a>
|
|
215
|
+
|
|
216
|
+
#### parent
|
|
217
|
+
|
|
218
|
+
```python
|
|
219
|
+
@property
|
|
220
|
+
def parent()
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
Return the parent, if any, of this element; read-only.
|
|
224
|
+
|
|
225
|
+
<a id="minixml.element.Element.attributes"></a>
|
|
226
|
+
|
|
227
|
+
#### attributes
|
|
228
|
+
|
|
229
|
+
```python
|
|
230
|
+
@property
|
|
231
|
+
def attributes()
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
Return the dictionary of the attributes; read only.
|
|
235
|
+
This is a shallow copy of the dictionary in the instance. Changing
|
|
236
|
+
it will therefore not affect the attributes in the instance.
|
|
237
|
+
|
|
238
|
+
<a id="minixml.element.Element.children"></a>
|
|
239
|
+
|
|
240
|
+
#### children
|
|
241
|
+
|
|
242
|
+
```python
|
|
243
|
+
@property
|
|
244
|
+
def children()
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
Return the list of children; read only.
|
|
248
|
+
This is a shallow copy of the list in the instance. Changing it
|
|
249
|
+
will therefore not affect the list of children in the instance.
|
|
250
|
+
|
|
251
|
+
<a id="minixml.element.Element.text"></a>
|
|
252
|
+
|
|
253
|
+
#### text
|
|
254
|
+
|
|
255
|
+
```python
|
|
256
|
+
@property
|
|
257
|
+
def text()
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
Return the text content of this element.
|
|
261
|
+
All children that are non-blank texts are concatenated.
|
|
262
|
+
|
|
263
|
+
<a id="minixml.element.Element.root"></a>
|
|
264
|
+
|
|
265
|
+
#### root
|
|
266
|
+
|
|
267
|
+
```python
|
|
268
|
+
@property
|
|
269
|
+
def root()
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
Return the root element of this element. May be self.
|
|
273
|
+
|
|
274
|
+
<a id="minixml.element.Element.depth"></a>
|
|
275
|
+
|
|
276
|
+
#### depth
|
|
277
|
+
|
|
278
|
+
```python
|
|
279
|
+
@property
|
|
280
|
+
def depth()
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
Depth of hierarchy for this element; number of parent steps to root.
|
|
284
|
+
|
|
285
|
+
<a id="minixml.element.Element.get"></a>
|
|
286
|
+
|
|
287
|
+
#### get
|
|
288
|
+
|
|
289
|
+
```python
|
|
290
|
+
def get(key, default=None)
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
Return the value of the given attribute in this element, or the default.
|
|
294
|
+
|
|
295
|
+
<a id="minixml.element.Element.set"></a>
|
|
296
|
+
|
|
297
|
+
#### set
|
|
298
|
+
|
|
299
|
+
```python
|
|
300
|
+
def set(key: str, value)
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
Set the value of the attribute in this element.
|
|
304
|
+
|
|
305
|
+
<a id="minixml.element.Element.append"></a>
|
|
306
|
+
|
|
307
|
+
#### append
|
|
308
|
+
|
|
309
|
+
```python
|
|
310
|
+
def append(other)
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
Append the element to the end of the list of children of this element.
|
|
314
|
+
|
|
315
|
+
<a id="minixml.element.Element.insert"></a>
|
|
316
|
+
|
|
317
|
+
#### insert
|
|
318
|
+
|
|
319
|
+
```python
|
|
320
|
+
def insert(i: int, other)
|
|
321
|
+
```
|
|
322
|
+
|
|
323
|
+
Insert the element at position i in the list of children of this element.
|
|
324
|
+
|
|
325
|
+
<a id="minixml.element.Element.replace"></a>
|
|
326
|
+
|
|
327
|
+
#### replace
|
|
328
|
+
|
|
329
|
+
```python
|
|
330
|
+
def replace(i: int, other)
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
Replace the element at position i in the list of children of this element.
|
|
334
|
+
The replaced element is returned.
|
|
335
|
+
|
|
336
|
+
<a id="minixml.element.Element.pop"></a>
|
|
337
|
+
|
|
338
|
+
#### pop
|
|
339
|
+
|
|
340
|
+
```python
|
|
341
|
+
def pop(i: [int, None] = None)
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
Remove the element at position i (last if None) and return it.
|
|
345
|
+
|
|
346
|
+
<a id="minixml.element.Element.free"></a>
|
|
347
|
+
|
|
348
|
+
#### free
|
|
349
|
+
|
|
350
|
+
```python
|
|
351
|
+
def free()
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
Remove this element from its parent, if any. Return itself.
|
|
355
|
+
|
|
356
|
+
<a id="minixml.element.Element.create"></a>
|
|
357
|
+
|
|
358
|
+
#### create
|
|
359
|
+
|
|
360
|
+
```python
|
|
361
|
+
def create(tag, **attributes)
|
|
362
|
+
```
|
|
363
|
+
|
|
364
|
+
Create an element with the given tag and attributes,
|
|
365
|
+
and append to the children of this element.
|
|
366
|
+
Return the created element.
|
|
367
|
+
|
|
368
|
+
<a id="minixml.element.Element.copy"></a>
|
|
369
|
+
|
|
370
|
+
#### copy
|
|
371
|
+
|
|
372
|
+
```python
|
|
373
|
+
def copy()
|
|
374
|
+
```
|
|
375
|
+
|
|
376
|
+
Make a copy of this element and its children (i.e. deepcopy).
|
|
377
|
+
The returned copy element has no parent.
|
|
378
|
+
|
|
379
|
+
<a id="minixml.element.Element.walk"></a>
|
|
380
|
+
|
|
381
|
+
#### walk
|
|
382
|
+
|
|
383
|
+
```python
|
|
384
|
+
def walk(test=None)
|
|
385
|
+
```
|
|
386
|
+
|
|
387
|
+
Walk over this element and all its Element children recursively,
|
|
388
|
+
yielding those elements that match the given test function.
|
|
389
|
+
If no test function given, yield all.
|
|
390
|
+
NOTE: The test function must handle both Element instances and strings.
|
|
391
|
+
|
|
392
|
+
<a id="minixml.element.Element.diff"></a>
|
|
393
|
+
|
|
394
|
+
#### diff
|
|
395
|
+
|
|
396
|
+
```python
|
|
397
|
+
def diff(other)
|
|
398
|
+
```
|
|
399
|
+
|
|
400
|
+
Return the first difference between this element and the other, recursively.
|
|
401
|
+
Ignores the parents of this element and the other.
|
|
402
|
+
Return a tuple describing the difference, or None.
|
|
403
|
+
|
|
404
|
+
<a id="minixml.io"></a>
|
|
405
|
+
|
|
406
|
+
# minixml.io
|
|
407
|
+
|
|
408
|
+
Parse XML code into an Element tree, and unparse an Element tree into XML code.
|
|
409
|
+
|
|
410
|
+
<a id="minixml.io.parse"></a>
|
|
411
|
+
|
|
412
|
+
#### parse
|
|
413
|
+
|
|
414
|
+
```python
|
|
415
|
+
def parse(content, element_class=Element)
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
Parse the given XML content (string). Return the root XML element.
|
|
419
|
+
|
|
420
|
+
<a id="minixml.io.read"></a>
|
|
421
|
+
|
|
422
|
+
#### read
|
|
423
|
+
|
|
424
|
+
```python
|
|
425
|
+
def read(filepath_or_stream, element_class=Element)
|
|
426
|
+
```
|
|
427
|
+
|
|
428
|
+
Read and parse the file given by its path, or the open file object.
|
|
429
|
+
Return the root Element.
|
|
430
|
+
|
|
431
|
+
<a id="minixml.io.unparse"></a>
|
|
432
|
+
|
|
433
|
+
#### unparse
|
|
434
|
+
|
|
435
|
+
```python
|
|
436
|
+
def unparse(element, indent=0, decl='<?xml version="1.0" encoding="utf-8"?>')
|
|
437
|
+
```
|
|
438
|
+
|
|
439
|
+
Return the XML code for the element and its children as a string.
|
|
440
|
+
'indent' is the number of blanks to use for each indentation level.
|
|
441
|
+
The 'decl' value is output first if it is a non-empty string.
|
|
442
|
+
|
|
443
|
+
<a id="minixml.io.write"></a>
|
|
444
|
+
|
|
445
|
+
#### write
|
|
446
|
+
|
|
447
|
+
```python
|
|
448
|
+
def write(outfile,
|
|
449
|
+
element,
|
|
450
|
+
indent=0,
|
|
451
|
+
decl='<?xml version="1.0" encoding="utf-8"?>')
|
|
452
|
+
```
|
|
453
|
+
|
|
454
|
+
Write the XML of the element and its children into the open file object.
|
|
455
|
+
The 'decl' value is output first if it is a non-empty string.
|
|
456
|
+
|
|
457
|
+
<a id="minixml.io.ContentHandler"></a>
|
|
458
|
+
|
|
459
|
+
## ContentHandler Objects
|
|
460
|
+
|
|
461
|
+
```python
|
|
462
|
+
class ContentHandler(xml.sax.ContentHandler)
|
|
463
|
+
```
|
|
464
|
+
|
|
465
|
+
Handle XML SAX parse events, turning them into an Element tree.
|
|
466
|
+
|
minixml-0.6.3/README.md
ADDED
|
@@ -0,0 +1,457 @@
|
|
|
1
|
+
# minixml
|
|
2
|
+
|
|
3
|
+
Minimalist module for reading, writing, creating and editing an XML element tree.
|
|
4
|
+
|
|
5
|
+
This code has been lovingly hand-crafted. No AI tools were used in its development.
|
|
6
|
+
|
|
7
|
+
# Software
|
|
8
|
+
|
|
9
|
+
There are no dependencies except for the standard Python library.
|
|
10
|
+
|
|
11
|
+
# Usage
|
|
12
|
+
|
|
13
|
+
```python
|
|
14
|
+
import minixml
|
|
15
|
+
|
|
16
|
+
# The root element.
|
|
17
|
+
svg = minixml.Element("svg", width=320, height=130, xmlns="http://www.w3.org/2000/svg")
|
|
18
|
+
|
|
19
|
+
# A child element can be created from the parent.
|
|
20
|
+
# It is automatically appended to it as a child.
|
|
21
|
+
svg.create(
|
|
22
|
+
"rect",
|
|
23
|
+
width=300,
|
|
24
|
+
height=100,
|
|
25
|
+
x=10,
|
|
26
|
+
y=10,
|
|
27
|
+
style="fill:rgb(0,0,255);stroke-width:3;stroke:red",
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
# An element can also be created from scratch and subsequently appended.
|
|
31
|
+
t = minixml.Element("text", x=5, y=15, fill="red")
|
|
32
|
+
t.append("I love SVG!")
|
|
33
|
+
svg.append(t)
|
|
34
|
+
|
|
35
|
+
# Serialize the Element tree into XML code; the XML declaration is prepended.
|
|
36
|
+
xmlcode = minixml.unparse(svg, indent=2)
|
|
37
|
+
|
|
38
|
+
# Ensure that unparse and parse are symmetric functions.
|
|
39
|
+
svg2 = minixml.parse(xmlcode)
|
|
40
|
+
assert svg == svg2
|
|
41
|
+
|
|
42
|
+
# Display the result.
|
|
43
|
+
print(xmlcode)
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Produces:
|
|
47
|
+
|
|
48
|
+
```svg
|
|
49
|
+
<?xml version="1.0" encoding="utf-8"?>
|
|
50
|
+
<svg width="320" height="130" xmlns="http://www.w3.org/2000/svg">
|
|
51
|
+
<rect width="300" height="100" x="10" y="10" style="fill:rgb(0,0,255);stroke-width:3;stroke:red"/>
|
|
52
|
+
<text x="5" y="15" fill="red">I love SVG!</text>
|
|
53
|
+
</svg>
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
# API
|
|
57
|
+
|
|
58
|
+
The `minixml` namespace contains the features most client software will need:
|
|
59
|
+
|
|
60
|
+
* [Element](#element-objects) (class)
|
|
61
|
+
* [parse](#parse) (function)
|
|
62
|
+
* [read](#read) (function)
|
|
63
|
+
* [unparse](#unparse) (function)
|
|
64
|
+
* [write](#write) (function)
|
|
65
|
+
|
|
66
|
+
These have been imported from `minixml.element` and `minixml.io`, which in most
|
|
67
|
+
cases will not need to be used directly by client software.
|
|
68
|
+
|
|
69
|
+
The following was produced using **pydoc-markdown**
|
|
70
|
+
https://github.com/NiklasRosenstein/pydoc-markdown
|
|
71
|
+
|
|
72
|
+
<a id="minixml"></a>
|
|
73
|
+
|
|
74
|
+
# minixml
|
|
75
|
+
|
|
76
|
+
Minimalist package for reading, writing, creating and editing an XML element tree.
|
|
77
|
+
|
|
78
|
+
<a id="minixml.element"></a>
|
|
79
|
+
|
|
80
|
+
# minixml.element
|
|
81
|
+
|
|
82
|
+
Element class.
|
|
83
|
+
|
|
84
|
+
<a id="minixml.element.Element"></a>
|
|
85
|
+
|
|
86
|
+
## Element Objects
|
|
87
|
+
|
|
88
|
+
```python
|
|
89
|
+
class Element()
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
XML element having a tag, attributes, parent (if any) and children (if any).
|
|
93
|
+
|
|
94
|
+
<a id="minixml.element.Element.__init__"></a>
|
|
95
|
+
|
|
96
|
+
#### \_\_init\_\_
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
def __init__(tag: str, *children, **attributes)
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
The instance is initialized as a root element (i.e. it has no parent).
|
|
103
|
+
|
|
104
|
+
<a id="minixml.element.Element.__str__"></a>
|
|
105
|
+
|
|
106
|
+
#### \_\_str\_\_
|
|
107
|
+
|
|
108
|
+
```python
|
|
109
|
+
def __str__()
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
Return the string representation of the element's starting tag.
|
|
113
|
+
|
|
114
|
+
<a id="minixml.element.Element.__getitem__"></a>
|
|
115
|
+
|
|
116
|
+
#### \_\_getitem\_\_
|
|
117
|
+
|
|
118
|
+
```python
|
|
119
|
+
def __getitem__(key)
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Get the value of the attribute in this element.
|
|
123
|
+
|
|
124
|
+
<a id="minixml.element.Element.__setitem__"></a>
|
|
125
|
+
|
|
126
|
+
#### \_\_setitem\_\_
|
|
127
|
+
|
|
128
|
+
```python
|
|
129
|
+
def __setitem__(key, value)
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
Set the value of the attribute in this element.
|
|
133
|
+
|
|
134
|
+
<a id="minixml.element.Element.__delitem__"></a>
|
|
135
|
+
|
|
136
|
+
#### \_\_delitem\_\_
|
|
137
|
+
|
|
138
|
+
```python
|
|
139
|
+
def __delitem__(key)
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
Delete the attribute in this element.
|
|
143
|
+
|
|
144
|
+
<a id="minixml.element.Element.__contains__"></a>
|
|
145
|
+
|
|
146
|
+
#### \_\_contains\_\_
|
|
147
|
+
|
|
148
|
+
```python
|
|
149
|
+
def __contains__(key)
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
Does this element have the given attribute?
|
|
153
|
+
|
|
154
|
+
<a id="minixml.element.Element.__iter__"></a>
|
|
155
|
+
|
|
156
|
+
#### \_\_iter\_\_
|
|
157
|
+
|
|
158
|
+
```python
|
|
159
|
+
def __iter__()
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
Iterate over the children of this element.
|
|
163
|
+
|
|
164
|
+
<a id="minixml.element.Element.__len__"></a>
|
|
165
|
+
|
|
166
|
+
#### \_\_len\_\_
|
|
167
|
+
|
|
168
|
+
```python
|
|
169
|
+
def __len__()
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
Return the number of children of this element.
|
|
173
|
+
|
|
174
|
+
<a id="minixml.element.Element.__eq__"></a>
|
|
175
|
+
|
|
176
|
+
#### \_\_eq\_\_
|
|
177
|
+
|
|
178
|
+
```python
|
|
179
|
+
def __eq__(other)
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
Are this element and the other equal, recursively? Ignores the parent.
|
|
183
|
+
|
|
184
|
+
<a id="minixml.element.Element.__iadd__"></a>
|
|
185
|
+
|
|
186
|
+
#### \_\_iadd\_\_
|
|
187
|
+
|
|
188
|
+
```python
|
|
189
|
+
def __iadd__(other)
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
Append the other to the end of the list of children of this other.
|
|
193
|
+
|
|
194
|
+
<a id="minixml.element.Element.tag"></a>
|
|
195
|
+
|
|
196
|
+
#### tag
|
|
197
|
+
|
|
198
|
+
```python
|
|
199
|
+
@property
|
|
200
|
+
def tag()
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
Return the tag of this element; read-only.
|
|
204
|
+
|
|
205
|
+
<a id="minixml.element.Element.parent"></a>
|
|
206
|
+
|
|
207
|
+
#### parent
|
|
208
|
+
|
|
209
|
+
```python
|
|
210
|
+
@property
|
|
211
|
+
def parent()
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
Return the parent, if any, of this element; read-only.
|
|
215
|
+
|
|
216
|
+
<a id="minixml.element.Element.attributes"></a>
|
|
217
|
+
|
|
218
|
+
#### attributes
|
|
219
|
+
|
|
220
|
+
```python
|
|
221
|
+
@property
|
|
222
|
+
def attributes()
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
Return the dictionary of the attributes; read only.
|
|
226
|
+
This is a shallow copy of the dictionary in the instance. Changing
|
|
227
|
+
it will therefore not affect the attributes in the instance.
|
|
228
|
+
|
|
229
|
+
<a id="minixml.element.Element.children"></a>
|
|
230
|
+
|
|
231
|
+
#### children
|
|
232
|
+
|
|
233
|
+
```python
|
|
234
|
+
@property
|
|
235
|
+
def children()
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
Return the list of children; read only.
|
|
239
|
+
This is a shallow copy of the list in the instance. Changing it
|
|
240
|
+
will therefore not affect the list of children in the instance.
|
|
241
|
+
|
|
242
|
+
<a id="minixml.element.Element.text"></a>
|
|
243
|
+
|
|
244
|
+
#### text
|
|
245
|
+
|
|
246
|
+
```python
|
|
247
|
+
@property
|
|
248
|
+
def text()
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
Return the text content of this element.
|
|
252
|
+
All children that are non-blank texts are concatenated.
|
|
253
|
+
|
|
254
|
+
<a id="minixml.element.Element.root"></a>
|
|
255
|
+
|
|
256
|
+
#### root
|
|
257
|
+
|
|
258
|
+
```python
|
|
259
|
+
@property
|
|
260
|
+
def root()
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
Return the root element of this element. May be self.
|
|
264
|
+
|
|
265
|
+
<a id="minixml.element.Element.depth"></a>
|
|
266
|
+
|
|
267
|
+
#### depth
|
|
268
|
+
|
|
269
|
+
```python
|
|
270
|
+
@property
|
|
271
|
+
def depth()
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
Depth of hierarchy for this element; number of parent steps to root.
|
|
275
|
+
|
|
276
|
+
<a id="minixml.element.Element.get"></a>
|
|
277
|
+
|
|
278
|
+
#### get
|
|
279
|
+
|
|
280
|
+
```python
|
|
281
|
+
def get(key, default=None)
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
Return the value of the given attribute in this element, or the default.
|
|
285
|
+
|
|
286
|
+
<a id="minixml.element.Element.set"></a>
|
|
287
|
+
|
|
288
|
+
#### set
|
|
289
|
+
|
|
290
|
+
```python
|
|
291
|
+
def set(key: str, value)
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
Set the value of the attribute in this element.
|
|
295
|
+
|
|
296
|
+
<a id="minixml.element.Element.append"></a>
|
|
297
|
+
|
|
298
|
+
#### append
|
|
299
|
+
|
|
300
|
+
```python
|
|
301
|
+
def append(other)
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
Append the element to the end of the list of children of this element.
|
|
305
|
+
|
|
306
|
+
<a id="minixml.element.Element.insert"></a>
|
|
307
|
+
|
|
308
|
+
#### insert
|
|
309
|
+
|
|
310
|
+
```python
|
|
311
|
+
def insert(i: int, other)
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
Insert the element at position i in the list of children of this element.
|
|
315
|
+
|
|
316
|
+
<a id="minixml.element.Element.replace"></a>
|
|
317
|
+
|
|
318
|
+
#### replace
|
|
319
|
+
|
|
320
|
+
```python
|
|
321
|
+
def replace(i: int, other)
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
Replace the element at position i in the list of children of this element.
|
|
325
|
+
The replaced element is returned.
|
|
326
|
+
|
|
327
|
+
<a id="minixml.element.Element.pop"></a>
|
|
328
|
+
|
|
329
|
+
#### pop
|
|
330
|
+
|
|
331
|
+
```python
|
|
332
|
+
def pop(i: [int, None] = None)
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
Remove the element at position i (last if None) and return it.
|
|
336
|
+
|
|
337
|
+
<a id="minixml.element.Element.free"></a>
|
|
338
|
+
|
|
339
|
+
#### free
|
|
340
|
+
|
|
341
|
+
```python
|
|
342
|
+
def free()
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
Remove this element from its parent, if any. Return itself.
|
|
346
|
+
|
|
347
|
+
<a id="minixml.element.Element.create"></a>
|
|
348
|
+
|
|
349
|
+
#### create
|
|
350
|
+
|
|
351
|
+
```python
|
|
352
|
+
def create(tag, **attributes)
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
Create an element with the given tag and attributes,
|
|
356
|
+
and append to the children of this element.
|
|
357
|
+
Return the created element.
|
|
358
|
+
|
|
359
|
+
<a id="minixml.element.Element.copy"></a>
|
|
360
|
+
|
|
361
|
+
#### copy
|
|
362
|
+
|
|
363
|
+
```python
|
|
364
|
+
def copy()
|
|
365
|
+
```
|
|
366
|
+
|
|
367
|
+
Make a copy of this element and its children (i.e. deepcopy).
|
|
368
|
+
The returned copy element has no parent.
|
|
369
|
+
|
|
370
|
+
<a id="minixml.element.Element.walk"></a>
|
|
371
|
+
|
|
372
|
+
#### walk
|
|
373
|
+
|
|
374
|
+
```python
|
|
375
|
+
def walk(test=None)
|
|
376
|
+
```
|
|
377
|
+
|
|
378
|
+
Walk over this element and all its Element children recursively,
|
|
379
|
+
yielding those elements that match the given test function.
|
|
380
|
+
If no test function given, yield all.
|
|
381
|
+
NOTE: The test function must handle both Element instances and strings.
|
|
382
|
+
|
|
383
|
+
<a id="minixml.element.Element.diff"></a>
|
|
384
|
+
|
|
385
|
+
#### diff
|
|
386
|
+
|
|
387
|
+
```python
|
|
388
|
+
def diff(other)
|
|
389
|
+
```
|
|
390
|
+
|
|
391
|
+
Return the first difference between this element and the other, recursively.
|
|
392
|
+
Ignores the parents of this element and the other.
|
|
393
|
+
Return a tuple describing the difference, or None.
|
|
394
|
+
|
|
395
|
+
<a id="minixml.io"></a>
|
|
396
|
+
|
|
397
|
+
# minixml.io
|
|
398
|
+
|
|
399
|
+
Parse XML code into an Element tree, and unparse an Element tree into XML code.
|
|
400
|
+
|
|
401
|
+
<a id="minixml.io.parse"></a>
|
|
402
|
+
|
|
403
|
+
#### parse
|
|
404
|
+
|
|
405
|
+
```python
|
|
406
|
+
def parse(content, element_class=Element)
|
|
407
|
+
```
|
|
408
|
+
|
|
409
|
+
Parse the given XML content (string). Return the root XML element.
|
|
410
|
+
|
|
411
|
+
<a id="minixml.io.read"></a>
|
|
412
|
+
|
|
413
|
+
#### read
|
|
414
|
+
|
|
415
|
+
```python
|
|
416
|
+
def read(filepath_or_stream, element_class=Element)
|
|
417
|
+
```
|
|
418
|
+
|
|
419
|
+
Read and parse the file given by its path, or the open file object.
|
|
420
|
+
Return the root Element.
|
|
421
|
+
|
|
422
|
+
<a id="minixml.io.unparse"></a>
|
|
423
|
+
|
|
424
|
+
#### unparse
|
|
425
|
+
|
|
426
|
+
```python
|
|
427
|
+
def unparse(element, indent=0, decl='<?xml version="1.0" encoding="utf-8"?>')
|
|
428
|
+
```
|
|
429
|
+
|
|
430
|
+
Return the XML code for the element and its children as a string.
|
|
431
|
+
'indent' is the number of blanks to use for each indentation level.
|
|
432
|
+
The 'decl' value is output first if it is a non-empty string.
|
|
433
|
+
|
|
434
|
+
<a id="minixml.io.write"></a>
|
|
435
|
+
|
|
436
|
+
#### write
|
|
437
|
+
|
|
438
|
+
```python
|
|
439
|
+
def write(outfile,
|
|
440
|
+
element,
|
|
441
|
+
indent=0,
|
|
442
|
+
decl='<?xml version="1.0" encoding="utf-8"?>')
|
|
443
|
+
```
|
|
444
|
+
|
|
445
|
+
Write the XML of the element and its children into the open file object.
|
|
446
|
+
The 'decl' value is output first if it is a non-empty string.
|
|
447
|
+
|
|
448
|
+
<a id="minixml.io.ContentHandler"></a>
|
|
449
|
+
|
|
450
|
+
## ContentHandler Objects
|
|
451
|
+
|
|
452
|
+
```python
|
|
453
|
+
class ContentHandler(xml.sax.ContentHandler)
|
|
454
|
+
```
|
|
455
|
+
|
|
456
|
+
Handle XML SAX parse events, turning them into an Element tree.
|
|
457
|
+
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "minixml"
|
|
3
|
+
version = "0.6.3"
|
|
4
|
+
description = "Minimalist module for reading, writing, creating and editing an XML element tree."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.14"
|
|
7
|
+
dependencies = []
|
|
8
|
+
|
|
9
|
+
[[project.authors]]
|
|
10
|
+
name = "Per Kraulis"
|
|
11
|
+
email = "per.kraulis@gmail.com"
|
|
12
|
+
|
|
13
|
+
[build-system]
|
|
14
|
+
requires = ["uv_build>=0.12.21,<0.13.0"]
|
|
15
|
+
build-backend = "uv_build"
|
|
16
|
+
|
|
17
|
+
[[tool.uv.index]]
|
|
18
|
+
name = "testpypi"
|
|
19
|
+
url = "https://test.pypi.org/simple/"
|
|
20
|
+
publish-url = "https://test.pypi.org/legacy/"
|
|
21
|
+
explicit = true
|
|
22
|
+
|
|
23
|
+
[dependency-groups]
|
|
24
|
+
dev = ["pytest>=9.2.0"]
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "minixml"
|
|
3
|
+
version = "0.6.3"
|
|
4
|
+
description = "Minimalist module for reading, writing, creating and editing an XML element tree."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
authors = [
|
|
7
|
+
{ name = "Per Kraulis", email = "per.kraulis@gmail.com" }
|
|
8
|
+
]
|
|
9
|
+
requires-python = ">=3.14"
|
|
10
|
+
dependencies = []
|
|
11
|
+
|
|
12
|
+
[build-system]
|
|
13
|
+
requires = ["uv_build>=0.12.21,<0.13.0"]
|
|
14
|
+
build-backend = "uv_build"
|
|
15
|
+
|
|
16
|
+
[[tool.uv.index]]
|
|
17
|
+
name = "testpypi"
|
|
18
|
+
url = "https://test.pypi.org/simple/"
|
|
19
|
+
publish-url = "https://test.pypi.org/legacy/"
|
|
20
|
+
explicit = true
|
|
21
|
+
|
|
22
|
+
[dependency-groups]
|
|
23
|
+
dev = [
|
|
24
|
+
"pytest>=9.2.0",
|
|
25
|
+
]
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
"Element class."
|
|
2
|
+
|
|
3
|
+
__all__ = ["Element"]
|
|
4
|
+
|
|
5
|
+
import copy
|
|
6
|
+
from xml.sax.saxutils import quoteattr
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class Element:
|
|
10
|
+
"XML element having a tag, attributes, parent (if any) and children (if any)."
|
|
11
|
+
|
|
12
|
+
def __init__(self, tag: str, *children, **attributes):
|
|
13
|
+
"The instance is initialized as a root element (i.e. it has no parent)."
|
|
14
|
+
if not tag:
|
|
15
|
+
raise ValueError("'tag' may not be empty")
|
|
16
|
+
self._tag = tag
|
|
17
|
+
self._parent = None
|
|
18
|
+
self._children = []
|
|
19
|
+
for child in children:
|
|
20
|
+
self.append(child)
|
|
21
|
+
self._attributes = {}
|
|
22
|
+
for name, value in attributes.items():
|
|
23
|
+
self[name] = value
|
|
24
|
+
|
|
25
|
+
def __str__(self):
|
|
26
|
+
"Return the string representation of the element's starting tag."
|
|
27
|
+
result = []
|
|
28
|
+
result.append(f"<{self.tag}")
|
|
29
|
+
for name, value in self._attributes.items():
|
|
30
|
+
result.append(f" {name}={quoteattr(value)}")
|
|
31
|
+
if len(self):
|
|
32
|
+
result.append(">")
|
|
33
|
+
else:
|
|
34
|
+
result.append("/>")
|
|
35
|
+
return " ".join(result)
|
|
36
|
+
|
|
37
|
+
def __getitem__(self, key):
|
|
38
|
+
"Get the value of the attribute in this element."
|
|
39
|
+
try:
|
|
40
|
+
return self._attributes[key]
|
|
41
|
+
except KeyError:
|
|
42
|
+
raise KeyError(f"no such attribute '{key}' in element {self}")
|
|
43
|
+
|
|
44
|
+
def __setitem__(self, key, value):
|
|
45
|
+
"Set the value of the attribute in this element."
|
|
46
|
+
if not isinstance(value, str):
|
|
47
|
+
value = str(value)
|
|
48
|
+
self._attributes[key] = value
|
|
49
|
+
|
|
50
|
+
def __delitem__(self, key):
|
|
51
|
+
"Delete the attribute in this element."
|
|
52
|
+
try:
|
|
53
|
+
del self._attributes[key]
|
|
54
|
+
except KeyError:
|
|
55
|
+
raise KeyError(f"no such attribute '{key}' in element {self}")
|
|
56
|
+
|
|
57
|
+
def __contains__(self, key):
|
|
58
|
+
"Does this element have the given attribute?"
|
|
59
|
+
return key in self._attributes
|
|
60
|
+
|
|
61
|
+
def __iter__(self):
|
|
62
|
+
"Iterate over the children of this element."
|
|
63
|
+
yield from self._children
|
|
64
|
+
|
|
65
|
+
def __len__(self):
|
|
66
|
+
"Return the number of children of this element."
|
|
67
|
+
return len(self._children)
|
|
68
|
+
|
|
69
|
+
def __eq__(self, other):
|
|
70
|
+
"Are this element and the other equal, recursively? Ignores the parent."
|
|
71
|
+
return not self.diff(other)
|
|
72
|
+
|
|
73
|
+
def __iadd__(self, other):
|
|
74
|
+
"Append the other to the end of the list of children of this other."
|
|
75
|
+
self.append(other)
|
|
76
|
+
return self
|
|
77
|
+
|
|
78
|
+
@property
|
|
79
|
+
def tag(self):
|
|
80
|
+
"Return the tag of this element; read-only."
|
|
81
|
+
return self._tag
|
|
82
|
+
|
|
83
|
+
@property
|
|
84
|
+
def parent(self):
|
|
85
|
+
"Return the parent, if any, of this element; read-only."
|
|
86
|
+
return self._parent
|
|
87
|
+
|
|
88
|
+
@property
|
|
89
|
+
def attributes(self):
|
|
90
|
+
"""Return the dictionary of the attributes; read only.
|
|
91
|
+
This is a shallow copy of the dictionary in the instance. Changing
|
|
92
|
+
it will therefore not affect the attributes in the instance.
|
|
93
|
+
"""
|
|
94
|
+
return self._attributes.copy()
|
|
95
|
+
|
|
96
|
+
@property
|
|
97
|
+
def children(self):
|
|
98
|
+
"""Return the list of children; read only.
|
|
99
|
+
This is a shallow copy of the list in the instance. Changing it
|
|
100
|
+
will therefore not affect the list of children in the instance.
|
|
101
|
+
"""
|
|
102
|
+
return list(self._children)
|
|
103
|
+
|
|
104
|
+
@property
|
|
105
|
+
def text(self):
|
|
106
|
+
"""Return the text content of this element.
|
|
107
|
+
All children that are non-blank texts are concatenated.
|
|
108
|
+
"""
|
|
109
|
+
result = []
|
|
110
|
+
for child in self:
|
|
111
|
+
if isinstance(child, str) and child.strip():
|
|
112
|
+
result.append(child)
|
|
113
|
+
return "".join(result)
|
|
114
|
+
|
|
115
|
+
@property
|
|
116
|
+
def root(self):
|
|
117
|
+
"Return the root element of this element. May be self."
|
|
118
|
+
result = self
|
|
119
|
+
while result._parent is not None:
|
|
120
|
+
result = result._parent
|
|
121
|
+
return result
|
|
122
|
+
|
|
123
|
+
@property
|
|
124
|
+
def depth(self):
|
|
125
|
+
"Depth of hierarchy for this element; number of parent steps to root."
|
|
126
|
+
result = 0
|
|
127
|
+
elem = self
|
|
128
|
+
while elem._parent is not None:
|
|
129
|
+
result += 1
|
|
130
|
+
elem = elem._parent
|
|
131
|
+
return result
|
|
132
|
+
|
|
133
|
+
def get(self, key, default=None):
|
|
134
|
+
"Return the value of the given attribute in this element, or the default."
|
|
135
|
+
try:
|
|
136
|
+
return self[key]
|
|
137
|
+
except KeyError:
|
|
138
|
+
return default
|
|
139
|
+
|
|
140
|
+
def set(self, key: str, value):
|
|
141
|
+
"Set the value of the attribute in this element."
|
|
142
|
+
self[key] = value
|
|
143
|
+
|
|
144
|
+
def append(self, other):
|
|
145
|
+
"Append the element to the end of the list of children of this element."
|
|
146
|
+
assert isinstance(other, (Element, str)), other
|
|
147
|
+
assert self is not other
|
|
148
|
+
if isinstance(other, Element):
|
|
149
|
+
if other.parent:
|
|
150
|
+
raise ValueError(f"element '{other}' must have no parent")
|
|
151
|
+
self._children.append(other)
|
|
152
|
+
if isinstance(other, Element):
|
|
153
|
+
other._parent = self
|
|
154
|
+
|
|
155
|
+
def insert(self, i: int, other):
|
|
156
|
+
"Insert the element at position i in the list of children of this element."
|
|
157
|
+
assert i >= 0 and i <= len(self)
|
|
158
|
+
assert isinstance(other, (Element, str)), other
|
|
159
|
+
assert self is not other
|
|
160
|
+
if isinstance(other, Element):
|
|
161
|
+
if other.parent:
|
|
162
|
+
raise ValueError(f"element '{other}' must have no parent")
|
|
163
|
+
self._children.insert(i, other)
|
|
164
|
+
if isinstance(other, Element):
|
|
165
|
+
other._parent = self
|
|
166
|
+
|
|
167
|
+
def replace(self, i: int, other):
|
|
168
|
+
"""Replace the element at position i in the list of children of this element.
|
|
169
|
+
The replaced element is returned.
|
|
170
|
+
"""
|
|
171
|
+
assert i >= 0 and i < len(self)
|
|
172
|
+
assert isinstance(other, (Element, str)), other
|
|
173
|
+
assert self is not other
|
|
174
|
+
if isinstance(other, Element):
|
|
175
|
+
if other.parent:
|
|
176
|
+
raise ValueError(f"element '{other}' must have no parent")
|
|
177
|
+
result = self._children[i]
|
|
178
|
+
if isinstance(result, Element):
|
|
179
|
+
result._parent = None
|
|
180
|
+
self._children[i] = other
|
|
181
|
+
if isinstance(other, Element):
|
|
182
|
+
other._parent = self
|
|
183
|
+
return result
|
|
184
|
+
|
|
185
|
+
def pop(self, i: [int, None]=None):
|
|
186
|
+
"Remove the element at position i (last if None) and return it."
|
|
187
|
+
assert i is None or (i >= 0 and i < len(self))
|
|
188
|
+
if i is None:
|
|
189
|
+
result = self._children.pop()
|
|
190
|
+
else:
|
|
191
|
+
result = self._children.pop(i)
|
|
192
|
+
result._parent = None
|
|
193
|
+
return result
|
|
194
|
+
|
|
195
|
+
def free(self):
|
|
196
|
+
"Remove this element from its parent, if any. Return itself."
|
|
197
|
+
if self._parent is not None:
|
|
198
|
+
self._parent._children.remove(self)
|
|
199
|
+
self._parent = None
|
|
200
|
+
return self
|
|
201
|
+
|
|
202
|
+
def create(self, tag, **attributes):
|
|
203
|
+
"""Create an element with the given tag and attributes,
|
|
204
|
+
and append to the children of this element.
|
|
205
|
+
Return the created element.
|
|
206
|
+
"""
|
|
207
|
+
# Ensure that the same class is used throughout.
|
|
208
|
+
elem = self.__class__(tag, **attributes)
|
|
209
|
+
self.append(elem)
|
|
210
|
+
return elem
|
|
211
|
+
|
|
212
|
+
def copy(self):
|
|
213
|
+
"""Make a copy of this element and its children (i.e. deepcopy).
|
|
214
|
+
The returned copy element has no parent.
|
|
215
|
+
"""
|
|
216
|
+
result = copy.deepcopy(self)
|
|
217
|
+
result._parent = None
|
|
218
|
+
result._set_parent()
|
|
219
|
+
return result
|
|
220
|
+
|
|
221
|
+
def _set_parent(self):
|
|
222
|
+
"Set the correct parent for all children recursively."
|
|
223
|
+
for child in self:
|
|
224
|
+
if isinstance(child, Element):
|
|
225
|
+
child._parent = self
|
|
226
|
+
child._set_parent()
|
|
227
|
+
|
|
228
|
+
def walk(self, test=None):
|
|
229
|
+
"""Walk over this element and all its Element children recursively,
|
|
230
|
+
yielding those elements that match the given test function.
|
|
231
|
+
If no test function given, yield all.
|
|
232
|
+
NOTE: The test function must handle both Element instances and strings.
|
|
233
|
+
"""
|
|
234
|
+
if test is not None and not callable(test):
|
|
235
|
+
raise ValueError("'test' for method 'walk' must be a callable or None")
|
|
236
|
+
if test is None or test(self):
|
|
237
|
+
yield self
|
|
238
|
+
for child in self:
|
|
239
|
+
if isinstance(child, Element):
|
|
240
|
+
yield from child.walk(test=test)
|
|
241
|
+
elif test is None or test(child):
|
|
242
|
+
yield child
|
|
243
|
+
|
|
244
|
+
def diff(self, other):
|
|
245
|
+
"""Return the first difference between this element and the other, recursively.
|
|
246
|
+
Ignores the parents of this element and the other.
|
|
247
|
+
Return a tuple describing the difference, or None.
|
|
248
|
+
"""
|
|
249
|
+
if not isinstance(other, self.__class__):
|
|
250
|
+
return ("instance", self, other)
|
|
251
|
+
if self.tag != other.tag:
|
|
252
|
+
return ("tag", self, other)
|
|
253
|
+
if self._attributes != other.attributes:
|
|
254
|
+
return ("attributes", self, other)
|
|
255
|
+
if len(self) != len(other):
|
|
256
|
+
return ("len", self, other)
|
|
257
|
+
for child1, child2 in zip(self._children, other.children):
|
|
258
|
+
if isinstance(child1, Element):
|
|
259
|
+
if result := child1.diff(child2):
|
|
260
|
+
return result
|
|
261
|
+
elif child1 != child2:
|
|
262
|
+
return ("diff", child1, child2)
|
|
263
|
+
return None
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"Parse XML code into an Element tree, and unparse an Element tree into XML code."
|
|
2
|
+
|
|
3
|
+
__all__ = ["parse", "read", "unparse", "write"]
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
import io
|
|
7
|
+
import xml.sax
|
|
8
|
+
from xml.sax.saxutils import quoteattr, escape, unescape
|
|
9
|
+
|
|
10
|
+
from .element import Element
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def parse(content, element_class=Element):
|
|
14
|
+
"Parse the given XML content (string). Return the root XML element."
|
|
15
|
+
return read(io.StringIO(content), element_class=element_class)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def read(filepath_or_stream, element_class=Element):
|
|
19
|
+
"""Read and parse the file given by its path, or the open file object.
|
|
20
|
+
Return the root Element.
|
|
21
|
+
"""
|
|
22
|
+
content_handler = ContentHandler(element_class=element_class)
|
|
23
|
+
try:
|
|
24
|
+
xml.sax.parse(filepath_or_stream, content_handler)
|
|
25
|
+
except xml.sax.SAXException as error:
|
|
26
|
+
raise ValueError(f"XML parse error: {error}")
|
|
27
|
+
return content_handler.root
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def unparse(element, indent=0, decl='<?xml version="1.0" encoding="utf-8"?>'):
|
|
31
|
+
"""Return the XML code for the element and its children as a string.
|
|
32
|
+
'indent' is the number of blanks to use for each indentation level.
|
|
33
|
+
The 'decl' value is output first if it is a non-empty string.
|
|
34
|
+
"""
|
|
35
|
+
assert isinstance(indent, int) and indent >= 0
|
|
36
|
+
outfile = io.StringIO()
|
|
37
|
+
write(outfile, element, indent=indent, decl=decl)
|
|
38
|
+
return outfile.getvalue()
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def write(outfile, element, indent=0, decl='<?xml version="1.0" encoding="utf-8"?>'):
|
|
42
|
+
"""Write the XML of the element and its children into the open file object.
|
|
43
|
+
The 'decl' value is output first if it is a non-empty string.
|
|
44
|
+
"""
|
|
45
|
+
assert isinstance(indent, int) and indent >= 0
|
|
46
|
+
if decl:
|
|
47
|
+
outfile.write(decl + "\n")
|
|
48
|
+
_write(outfile, element, "", indent)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def _write(outfile, element, padding, indent):
|
|
52
|
+
"Recursively write out the element and its children."
|
|
53
|
+
outfile.write(padding)
|
|
54
|
+
outfile.write(f"<{element.tag}")
|
|
55
|
+
for name, value in element.attributes.items():
|
|
56
|
+
outfile.write(f" {name}={quoteattr(value)}")
|
|
57
|
+
if len(element):
|
|
58
|
+
outfile.write(">")
|
|
59
|
+
newline = False
|
|
60
|
+
for elem in element:
|
|
61
|
+
if isinstance(elem, Element):
|
|
62
|
+
if indent:
|
|
63
|
+
outfile.write("\n")
|
|
64
|
+
newline = True
|
|
65
|
+
_write(outfile, elem, padding + " " * indent, indent)
|
|
66
|
+
elif isinstance(elem, str):
|
|
67
|
+
outfile.write(escape(elem))
|
|
68
|
+
newline = False
|
|
69
|
+
else:
|
|
70
|
+
outfile.write(escape(str(elem)))
|
|
71
|
+
newline = False
|
|
72
|
+
if newline:
|
|
73
|
+
if indent:
|
|
74
|
+
outfile.write("\n")
|
|
75
|
+
outfile.write(padding)
|
|
76
|
+
outfile.write(f"</{element.tag}>")
|
|
77
|
+
else:
|
|
78
|
+
outfile.write("/>")
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
class ContentHandler(xml.sax.ContentHandler):
|
|
82
|
+
"Handle XML SAX parse events, turning them into an Element tree."
|
|
83
|
+
|
|
84
|
+
def __init__(self, element_class=Element):
|
|
85
|
+
assert issubclass(element_class, Element)
|
|
86
|
+
self.element_class = element_class
|
|
87
|
+
self.stack = []
|
|
88
|
+
self.root = None
|
|
89
|
+
|
|
90
|
+
def startElement(self, tag: str, attributes):
|
|
91
|
+
elem = self.element_class(tag, **dict(attributes))
|
|
92
|
+
if self.stack:
|
|
93
|
+
self.stack[-1]._children.append(elem)
|
|
94
|
+
elem._parent = self.stack[-1]
|
|
95
|
+
else:
|
|
96
|
+
self.root = elem
|
|
97
|
+
self.stack.append(elem)
|
|
98
|
+
|
|
99
|
+
def endElement(self, tag: str):
|
|
100
|
+
self.stack.pop()
|
|
101
|
+
|
|
102
|
+
def characters(self, content: str):
|
|
103
|
+
if content := content.strip():
|
|
104
|
+
self.stack[-1]._children.append(unescape(content))
|
|
File without changes
|