This repository has been archived by the owner on Dec 17, 2020. It is now read-only.
/
DOMLight.py
188 lines (169 loc) · 7.07 KB
/
DOMLight.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
"""
DOMLight is a little library for making DOM's more accessable in python.
Copyright (C) 2011 Brian Muller <bamuller@gmail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""
## DOMLight is a little library for making DOM's more accessable in python.
##
## For instance, say you've got the following XML:
## <cheese with="spam">
## <block type="jack">
## A huge bunch of jack.
## </block>
## <block type="cheddar">
## I like cheddar.
## <color>yellow</color>
## It is tastey.
## </block>
## <additional stuff="eggs" />
## </cheese>
##
## Python code to access the objects:
##
## import DOMLight
## root = DOMLight.createModel(xml_string)
## print root["with"] # this accesses the attribute "with" in the root node
## print root["doesnt_exist"] # this attempts to access a non-existant attribute, returns ""
## block = root.block[0] # block now holds root's first child named block
## print block["type"] # -> "jack"
## print block.text[0] # Text children are named "text" - this prints the first text child
## block = root.block[1] # block now holds root's second child named block
## print block.color[0].text[0] # -> "yellow"
## print block.text[0] # -> "I like cheddar."
## print block.text[1] # -> "It is tastey."
## a = root.additional # "a" is now an list with one element
## print a[0]["stuff"] # -> "eggs"
##
## node.children is a hash. The keys are children names, and the values
## are lists of children with that name.
##
## for nodename, nodelist in root.children.items():
## print "There are %d children named %s" % (len(nodelist), nodename)
## for node in nodelist:
## # if it's a text node, just print the text
## if type(node).__name__ == 'unicode':
## print "text value is: \"%s\"" % str(node)
## else:
## # node.attrs is a hash. The keys are attribute names,
## # and the values are the attribute values.
## for attr_name,attr_value in node.attrs.items():
## print "%s = %s" % (attr_name, attr_value)
from xml.dom.minidom import *
import cgi
import re
def createModel(xml_string):
def makeObject(elem):
doml = DOMLightObject(elem.nodeName)
if elem.attributes:
for k,v in elem.attributes.items():
doml[k] = v
for child in elem.childNodes:
if child.nodeType == 3 and child.nodeValue.rstrip() != "":
doml.setAttr('text', child.nodeValue)
elif child.nodeType == 8:
doml.setAttr('comment', child.nodeValue)
elif child.nodeType != 3:
doml.setAttr(child.nodeName, makeObject(child))
return doml
doc = parseString(xml_string)
for kid in doc.childNodes:
if kid.nodeType == 1:
return makeObject(kid)
return None
class DOMLightObject:
def __init__(self, name):
self.children = {}
self.attrs = {}
self._name = name
def __getitem__(self, name):
if self.attrs.has_key(name):
return self.attrs[name]
return ""
def __setitem__(self, name, value):
self.attrs[name] = value
def setAttr(self, name, kid):
if not self.children.has_key(name):
self.children[name] = []
self.children[name].append(kid)
def __getattr__(self, name):
if self.children.has_key(name):
return self.children[name]
return []
def __str__(self):
return "<DOMLightObject %s>" % self._name
## This class is used to generate xml. It is used like:
# xml = XMLMaker()
# print xml.a({'href': 'http://google.com'}, xml.img({'src': 'http://www.google.com/intl/en/images/logo.gif'}), "Click the image")
# <a href="http://google.com"><img src="http://www.google.com/intl/en/images/logo.gif"/>Click the image</a>
class XMLMaker:
## Inner class representing an XML element.
class Elem:
def __init__(self,name):
self.name = name
def set(self,attr={},*args):
self.attr = attr
self.kids = args
return self
def cleanAttrs(self):
if len(self.attr)==0: return ""
return ' '+" ".join(["%s=\"%s\"" % (k,v) for (k,v) in self.attr.items()])
def __str__(self):
if len(self.kids)==0: return "<%s%s/>" % (self.name,self.cleanAttrs())
result = ""
kids = ""
try:
kids = "".join([str(kid) for kid in self.kids])
strmap = lambda *a: map(lambda x: str(x), a)
(self.name,cleanedAttrs,kids,self.name) = strmap(self.name,self.cleanAttrs(),kids,self.name)
result = "<%s%s>%s</%s>" % (self.name,cleanedAttrs,kids,self.name)
except Exception, e:
print "Error: %s\nkids: %s" % (str(e),kids)
return result
## Constructor.
# @param namespace The xmlns to use
def __init__(self, namespace=None):
self.namespace = namespace
## Manually create a function
def makeFunc(self,name):
return self.__getattr__(name)
## Make a new elemented
# @param name Name of the elem to make
def __getattr__(self,name):
if self.namespace!=None: elem = XMLMaker.Elem(self.namespace+':'+name)
else: elem = XMLMaker.Elem(name)
return elem.set
## This function first html escapes a string, and then converts
# entities to html entities to xml entities as much as possible.
# \return An XML entity escaped string
def XMLEscape(string):
s = cgi.escape(string, True)
return htmlToXmlEntities(s)
## XML only allows 5 entities (< > " & ') and
# html tidy does not do any conversion when creating xml from html,
# so this must be done manually. This function will convert Unicode
# entities to html entities to xml entities as much as possible.
# (for example, "©" becomes "&copy;"
# @param s The string containing HTML entities
# \return A string only containing XML entities
def htmlToXmlEntities(s):
def convertentity(m):
if m.group(1)=='#':
try:
return "&%s;" % htmlentitydefs.codepoint2name[int(m.group(2))]
except KeyError:
return "" # Must keep runing - even if we loose a uni char
elif m.group(2) in ['lt', 'gt', 'quot', 'amp', 'apos']:
return '&%s;' % m.group(2)
else:
return '&%s;' % m.group(2)
return re.sub(r'&(#?)(.+?);', convertentity, s)