forked from galvez/xmlwitch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmlwitch.py
161 lines (128 loc) · 5.55 KB
/
xmlwitch.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
from __future__ import with_statement
from StringIO import StringIO
from xml.sax import saxutils
from keyword import kwlist as PYTHON_KWORD_LIST
__all__ = ['Builder', 'Element']
__license__ = 'BSD'
__version__ = '0.2.1'
__author__ = "Jonas Galvez <http://jonasgalvez.com.br/>"
__contributors__ = ["bbolli <http://github.com/bbolli/>",
"masklinn <http://github.com/masklinn/>"]
class Builder:
def __init__(self, encoding='utf-8', indent=' '*2, version=None):
self._document = StringIO()
self._encoding = encoding
self._indent = indent
self._indentation = 0
if version is not None:
self.write('<?xml version="%s" encoding="%s"?>\n' % (
version, encoding
))
self._treebuilder = XmlTreeBuilder()
def __getattr__(self, name):
return Element(name, self)
def __getitem__(self, name):
return Element(name, self)
def __str__(self):
self._treebuilder.reset()
self._treebuilder.render(self)
return self._document.getvalue().encode(self._encoding).strip()
def __unicode__(self):
return self._document.getvalue().decode(self._encoding).strip()
def write(self, content):
"""Write raw content to the document"""
if type(content) is not unicode:
content = content.decode(self._encoding)
self._document.write('%s' % content)
def write_escaped(self, content):
"""Write escaped content to the document"""
self.write(saxutils.escape(content))
def write_indented(self, content):
"""Write indented content to the document"""
self.write('%s%s\n' % (self._indent * self._indentation, content))
builder = Builder # 0.1 backward compatibility
class XmlTreeBuilder:
def __init__(self):
self.root = []
self.position = self.root
self.stack = []
def open_tag(self, tag, attributes, text, with_block=False):
if len(self.stack) > 0 and not self.stack[-1][1]:
self.position = self.stack.pop()[0] # pop unclosed
self.stack.append([ self.position, with_block ])
self.position.append((tag, attributes, text, []))
self.position = self.position[-1][3]
def reopen_tag_with_block(self):
self.stack[-1][1] = True
def close_tag(self):
# close last open block
while len(self.stack) > 1 and not self.stack[-1][1]:
self.position = self.stack.pop()[0]
self.position = self.stack.pop()[0]
def reset(self):
self.stack = [] # reset stack, effectively closing any remaining open tags
self.position = self.root
def render(self, builder):
self.render_subtree(self.root, builder)
def render_subtree(self, trees, builder):
for tree in trees:
if tree[2] == None and len(tree[3]) == 0:
builder.write_indented( "<%s%s />" % (tree[0], tree[1]) )
else:
if tree[2] != None:
if len(tree[3]) == 0:
text = "<%s%s>%s</%s>" % (tree[0], tree[1], tree[2], tree[0])
builder.write_indented( text )
else:
builder.write_indented( "<%s%s>%s" % (tree[0], tree[1], tree[2]) )
builder._indentation += 1
self.render_subtree(tree[3], builder)
builder._indentation += -1
builder.write_indented( "</%s>" % tree[0] )
else:
builder.write_indented( "<%s%s>" % (tree[0], tree[1]) )
builder._indentation += 1
self.render_subtree(tree[3], builder)
builder._indentation += -1
builder.write_indented( "</%s>" % tree[0] )
class Element:
PYTHON_KWORD_MAP = dict([(k + '_', k) for k in PYTHON_KWORD_LIST])
def __init__(self, name, builder):
self.name = self._nameprep(name)
self.builder = builder
self.attributes = {}
self.opened = False
self.tree = builder._treebuilder
def __enter__(self):
"""Add a parent element to the document"""
if(self.opened):
self.tree.reopen_tag_with_block()
else:
self.tree.open_tag(self.name, self._serialized_attrs(), None, True)
self.opened = True
return self
def __exit__(self, type, value, tb):
"""Add close tag to current parent element"""
self.tree.close_tag()
def __call__(*args, **kargs):
"""Add a child element to the document"""
self = args[0]
self.attributes.update(kargs)
value = args[1] if len(args) > 1 else None
if value:
value = saxutils.escape(value)
self.opened = True
self.tree.open_tag(self.name, self._serialized_attrs(), value, False)
return self
def _serialized_attrs(self):
"""Serialize attributes for element insertion"""
serialized = []
for attr, value in self.attributes.items():
serialized.append(' %s=%s' % (
self._nameprep(attr), saxutils.quoteattr(value)
))
return ''.join(serialized)
def _nameprep(self, name):
"""Undo keyword and colon mangling"""
name = Element.PYTHON_KWORD_MAP.get(name, name)
return name.replace('__', ':')