-
Notifications
You must be signed in to change notification settings - Fork 1
/
xmlutils.py
186 lines (161 loc) · 5.4 KB
/
xmlutils.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
import xml.etree.ElementTree as ET
from resource import Resource
from resource import urlToPath
import datetime as DT
from urllib import quote, unquote
# parseResponse :: String -> (False, Resource) | (True, [Resource])
def parseResponse(xmlstring):
e = ET.fromstring(xmlstring)
if e.text == 'ResourceList':
return (True, _parseResourceList(e))
else:
return (False, _parseResourceDownload(e))
# parseResourceList :: ET.Element -> [Resource]
def _parseResourceList(e):
output = []
for child in list(e):
output.append(parseResource(child))
return output
# parseResourceList :: ET.Element -> [Resource]
def parseResourceList(xmlstring):
e = ET.fromstring(xmlstring)
output = []
for child in list(e):
output.append(parseResource(child))
return output
# buildResourceList :: [Resource] -> String
def buildResourceList(resources):
e = ET.Element('ResourceList')
for r in resources:
e.append(buildResource(r))
return ET.tostring(e)
# parseResourceDownload :: ET.Element -> Resource
def _parseResourceDownload(e):
return parseResource(e.find('Resource'))
# parseResourceDownload :: ET.Element -> Resource
def parseResourceDownload(xmlstring):
e = ET.fromstring(xmlstring)
return parseResource(e.find('Resource'))
# buildResourceDownload :: Resource -> String
def buildResourceDownload(resource):
e = ET.Element('ResourceDownload')
e.insert(0,buildResource(resource))
return ET.tostring(e)
# parseResourceUpload :: String -> Resource
def parseResourceUpload(xmlstring):
e = ET.fromstring(xmlstring)
return parseResource(e.find('Resource'))
# buildResourceUpload :: Resource -> String
def buildResourceUpload(resource):
e = ET.Element('ResourceUpload')
e.insert(0,buildResource(resource))
return ET.tostring(e)
#HELPERS:
propertiesToTags = {
'name':'ResourceName',
'url':'ResourceURL',
'encoding':'ResourceEncoding',
'content':'ResourceContent',
'resourceType':'ResourceType'
}
# parseResource :: ET.Element -> Resource
def parseResource(e):
r = Resource()
ETtoObject(e,r,propertiesToTags)
r.name = unquote(r.name)
r.category = e.get('category')
if r.category == 'directory':
if e.findtext('ResourceNumItems') is not None:
r.numItems = int(e.findtext('ResourceNumItems'))
elif e.findtext('ResourceSize') is not None:
r.size = int(e.findtext('ResourceSize'))
if hasattr(r,'url'):
r.path = urlToPath(r.url)
r.resourceDate = parseDate(e.find('ResourceDate'))
return r
def ETtoObject(et, obj, ptot):
for (prop, tag) in ptot.iteritems():
val = et.findtext(tag)
if val is not None:
setattr(obj, prop, val)
# buildResource :: Resource -> ET.Element
def buildResource(resource):
e = ET.Element('Resource',{'category' : resource.category})
e.extend(objectToET(resource,propertiesToTags))
ec = None
if resource.category == 'directory':
if hasattr(resource,'numItems'):
ec = ET.Element('ResourceNumItems')
ec.text = str(resource.numItems)
elif hasattr(resource, 'size'):
ec = ET.Element('ResourceSize')
ec.text = str(resource.size)
if ec is not None:
e.append(ec)
if hasattr(resource, 'resourceDate') and resource.resourceDate is not None:
e.append(buildDate(resource.resourceDate))
return e
def objectToET(obj, propertiesToTags):
ets = []
for (prop, tag) in propertiesToTags.iteritems():
if hasattr(obj, prop):
ec = ET.Element(tag)
ec.text = getattr(obj, prop)
ets.append(ec)
return ets
# parseDate :: ET.Element -> DT.DateTime
def parseDate(e):
if e is None:
return None
year = int(e.findtext('year'))
month = int(e.findtext('month'))
day = int(e.findtext('day'))
hour = int(e.findtext('hour'))
minu = int(e.findtext('min'))
sec = int(e.findtext('sec'))
return DT.datetime(year, month, day, hour, minu, sec)
# buildDate :: DT.DateTime -> ET.Element
def buildDate(dt):
e = ET.Element('ResourceDate')
y = ET.Element('year')
y.text = str(dt.year)
e.append(y)
m = ET.Element('month')
m.text = str(dt.month)
e.append(m)
d = ET.Element('day')
d.text = str(dt.day)
e.append(d)
h = ET.Element('hour')
h.text = str(dt.hour)
e.append(h)
mi = ET.Element('min')
mi.text = str(dt.minute)
e.append(mi)
s = ET.Element('sec')
s.text = str(dt.second)
e.append(s)
return e
if __name__ == "__main__":
rl_xmlstr = "<ResourceList>\
<Resource category=\"file\">\
<ResourceName> test.pdf</ResourceName>\
<ResourceSize> 128 </ResourceSize>\
<ResourceURL> http://Dbox-sample.com/sampleuser/test.pdf </ResourceURL>\
<ResourceDate>\
<year>2010</year>\
<month>10</month>\
<day>13</day>\
<hour>06</hour>\
<min>24</min>\
<sec>10</sec>\
</ResourceDate>\
<ResourceType> application/pdf </ResourceType>\
</Resource>\
<Resource category=\"directory\">\
<ResourceName>mymusic </ResourceName>\
</Resource>\
</ResourceList>"
rs = parseResourceList(rl_xmlstr)
rl_xml = buildResourceList(rs)
rs2 = parseResourceList(rl_xml)