-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtag.py
186 lines (160 loc) · 6.8 KB
/
tag.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
#!/usr/bin/env python
from xml.dom import minidom
import copy
import datetime
TIMEFMT = '%Y-%m-%d %H:%M:%S'
class Tag(object):
def __init__(self,name,attributes={},data=None,root=None,schema=None):
if not isinstance(attributes,dict):
raise Exception,'Attributes for Tag %s are of type %s, not dict!' % (name,type(attributes))
self.data = data
self.attributes = attributes
self.name = name
self.schema = schema
self.children = []
def addAttribute(self,key,value):
self.attributes[key] = value
def loadFromFile(self,xmlfile):
xmlstr = open(xmlfile,'rt').read()
return self.loadFromString(xmlstr)
def loadFromString(self,xmlstr):
dom = minidom.parseString(xmlstr)
#try to detect which child node of the dom is an actual XML element
for root in dom.childNodes:
if not isinstance(root,minidom.DocumentType):
break
self.name = root.nodeName
atts = root.attributes
hasData = root.firstChild.nodeType == root.TEXT_NODE and len(root.firstChild.nodeValue.strip())
if len(atts) and hasData:
pass
#raise Exception,'You can have child elements or tag data, but not both!'
if len(atts):
for item in atts.items():
key = item[0]
value = item[1]
self.attributes[key] = value
if hasData:
self.data = root.firstChild.nodeValue.strip()
for child in root.childNodes:
if child.nodeType == child.ELEMENT_NODE:
self.children.append(copy.deepcopy(self.convertNode(child)))
def convertNode(self,child):
name = child.nodeName
atts = child.attributes
c1 = child.firstChild is not None
hasData = False
if c1:
c2 = child.firstChild.nodeType == child.TEXT_NODE
c3 = len(child.firstChild.nodeValue.strip())
hasData = c1 and c2 and c3
if len(atts) and hasData:
pass
#raise Exception,'You can have child elements or tag data, but not both!'
attributes = {}
if len(atts):
for item in atts.items():
key = item[0]
value = item[1]
try:
value = float(value)
except ValueError,msg:
try:
value = int(value)
except ValueError,msg:
pass
#this may be a time field - let's assume it is and try to parse it
if (isinstance(value,str) or isinstance(value,unicode)) and (key.lower().count('time') or key.lower().count('date')):
try:
value = datetime.datetime.strptime(value,TIMEFMT)
except ValueError:
pass #oh, well, I guess it isn't
attributes[key] = value
data = None
if hasData:
data = child.firstChild.nodeValue.strip()
children = []
for child2 in child.childNodes:
if child2.nodeType == child2.ELEMENT_NODE:
children.append(copy.deepcopy(self.convertNode(child2)))
t = Tag(name,attributes,data)
for child in children:
t.addChild(child)
return t
def __repr__(self):
fmt = '[%s: Attributes: "%s" Data: %s... %i children]'
attstr = ' '.join(self.attributes.keys())
repstr = fmt % (self.name,attstr,self.data,len(self.children))
return repstr
def getChildren(self,name):
children = []
for child in self.children:
if child.name == name:
children.append(child)
return children
def addChild(self,tag):
if not isinstance(tag,Tag):
raise Exception,'addChild only takes Tag objects as arguments'
if self.data is not None:
raise Exception,'You can have child elements or tag data, but not both!'
self.children.append(tag)
def deleteChildren(self,tagname):
if not isinstance(tagname,str):
raise Exception,'deleteChildren only takes string objects as arguments'
numchildren = 0
goodchildren = []
for child in self.children:
if child.name != tagname:
goodchildren.append(copy.deepcopy(child))
else:
numchildren += 1
self.children = goodchildren
return numchildren
def renderTag(self,ntabs):
hasAttributes = bool(len(self.attributes))
hasData = self.data is not None
hasChildren = bool(len(self.children))
linestr = '\t'*ntabs+'<%s ' % self.name
if hasAttributes:
attstr = ''
for key,value in self.attributes.iteritems():
if isinstance(value,datetime.datetime):
value = value.strftime(TIMEFMT)
#strip out " (double quotes) from attributes - they'll mess up XML parsing later
if isinstance(value,str) or isinstance(value,unicode):
value = value.replace('"',"'")
attstr = attstr + '%s="%s" ' % (key,str(value))
linestr = linestr + attstr.strip()
if hasData or hasChildren:
if hasData:
linestr = linestr.rstrip() + '>\n'
linestr = linestr + '\t'*(ntabs+1) + self.data + '\n' + '\t'*ntabs + '</%s>\n' % self.name
if hasChildren:
linestr = linestr.rstrip() + '>\n'
for child in self.children:
linestr = linestr + child.renderTag(ntabs+1)
linestr = linestr + '\n' + '\t'*ntabs + '</%s>\n' % self.name
#linestr = linestr + '\t'*ntabs + '</%s>\n' % self.name
else:
linestr = linestr + '/>\n'
return linestr
def renderToXML(self,filename=None,ntabs=0):
xmlstr = '<?xml version="1.0" encoding="US-ASCII" standalone="yes"?>\n'
xmlstr = xmlstr + self.renderTag(ntabs)
xmlstr2 = ''
for line in xmlstr.split('\n'):
if len(line.strip()):
xmlstr2 = xmlstr2 + line + '\n'
if filename is not None:
f = open(filename,'wt')
f.write(xmlstr2)
f.close()
return xmlstr
if __name__ == '__main__':
root = Tag('parent',attributes={'name':'fred','age':34})
child1 = Tag('child1',attributes={'name':'pebbles','age':10})
c2data = '''I am a child that likes to say "Bam!" a lot. My parents think that I am difficult, but you should see what Pebbles likes to do!'''
child2 = Tag('child2',attributes={'name':'bam-bam','age':11},data=c2data)
root.addChild(child1)
root.addChild(child2)
print root.renderToXML()