-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patheasyxml.py
More file actions
146 lines (128 loc) · 4.48 KB
/
easyxml.py
File metadata and controls
146 lines (128 loc) · 4.48 KB
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
import xml.dom.minidom
import re
class EasyXML:
'''
EasyXML is an easy and concise way to generate XML output in Python.
It uses a custom attribute getter so element names can be specified
directly in the code:
books = EasyXML('books')
books.book(title='Example A')
books.book.author(name='John Smith', age=57)
books.book.publisher(name='Publisher A')
books.book(title='Example B')
books.book.author(name='Jane Doe', age=30)
books.book.author(name='James Cutter', age=45)
books.book.publisher(name='Publisher B')
print str(books)
The above code produces the following XML:
<books>
<book title="Example A">
<author age="57" name="John Smith"/>
<publisher name="Publisher A"/>
</book>
<book title="Example B">
<author age="30" name="Jane Doe"/>
<author age="45" name="James Cutter"/>
<publisher name="Publisher B"/>
</book>
</books>
You don't actually need to create every parent element, allowing the
following code to work:
root = EasyXML('root')
root.a.b.c()
root.a.b.c()
root.a()
root.a.b.c()
root.a.b.c()
print str(root)
The above code produces the following XML:
<root>
<a>
<b>
<c/>
<c/>
</b>
</a>
<a>
<b>
<c/>
<c/>
</b>
</a>
</root>
Creating an element returns that element, which can then be passed
to helper methods:
def material(primitive, ambient, diffuse):
primitive.ambient(r=ambient[0], g=ambient[1], b=ambient[2])
primitive.diffuse(r=diffuse[0], g=diffuse[1], b=diffuse[2])
root = EasyXML('root')
material(root.primitive(type='sphere'), (64, 0, 0), (192, 0, 0))
material(root.primitive(type='cube'), (0, 64, 0), (0, 192, 0))
print str(root)
The above code produces the following XML:
<root>
<primitive type="sphere">
<ambient b="0" g="0" r="64"/>
<diffuse b="0" g="0" r="192"/>
</primitive>
<primitive type="cube">
<ambient b="0" g="64" r="0"/>
<diffuse b="0" g="192" r="0"/>
</primitive>
</root>
'''
def __init__(self, name):
'''
Construct a new EasyXML node with a certain name. This should
only be used to create the root node, all child nodes should be
created with the attribute syntax (see books example above).
'''
self._parent = None
self._name = name
self._elements = []
self._attributes = {}
self._element_map = {}
def __getattr__(self, name):
'''
If an element with the given name has already been added, just
return that element. Otherwise, return a new element with the
given name and this object as a parent. This does NOT add the
returned element to this object yet, you still need to call the
returned element to add it.
'''
if name.startswith('_'):
return object.__getattr__(self, name)
if name in self._element_map:
return self._element_map[name]
element = EasyXML(name)
element._parent = self
return element
def __call__(self, **kwargs):
'''
Add a new element with our name to our parent element. Any keyword
arguments are set as attributes on the new element. This actually
adds new elements as far up the parent chain as needed, so you don't
need to create every parent explicitly.
'''
e = new_element = EasyXML(self._name)
e._parent = self._parent
e._attributes = kwargs
while e._parent and e not in e._parent._element_map.values():
e._parent._elements.append(e)
e._parent._element_map[e._name] = e
e = e._parent
return new_element
def __str__(self):
'''
Return generated XML representing the stored element tree.
'''
def to_xml(obj):
element = doc.createElement(obj._name)
for k in obj._attributes:
element.setAttribute(k, str(obj._attributes[k]))
for e in obj._elements:
element.appendChild(to_xml(e))
return element
doc = xml.dom.minidom.Document()
doc.appendChild(to_xml(self))
return re.sub(r'<\?xml.*\?>', '', doc.toprettyxml(indent=' ')).strip()