1
# Copyright (c) 2006,2007 Mitch Garnaat http://garnaat.org/
3
# Permission is hereby granted, free of charge, to any person obtaining a
4
# copy of this software and associated documentation files (the
5
# "Software"), to deal in the Software without restriction, including
6
# without limitation the rights to use, copy, modify, merge, publish, dis-
7
# tribute, sublicense, and/or sell copies of the Software, and to permit
8
# persons to whom the Software is furnished to do so, subject to the fol-
11
# The above copyright notice and this permission notice shall be included
12
# in all copies or substantial portions of the Software.
14
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
16
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
17
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22
class ResultSet(list):
24
The ResultSet is used to pass results back from the Amazon services
25
to the client. It has an ugly but workable mechanism for parsing
26
the XML results from AWS. Because I don't really want any dependencies
27
on external libraries, I'm using the standard SAX parser that comes
28
with Python. The good news is that it's quite fast and efficient but
29
it makes some things rather difficult.
31
You can pass in, as the marker_elem parameter, a list of tuples.
32
Each tuple contains a string as the first element which represents
33
the XML element that the resultset needs to be on the lookout for
34
and a Python class as the second element of the tuple. Each time the
35
specified element is found in the XML, a new instance of the class
36
will be created and popped onto the stack.
40
def __init__(self, marker_elem=None):
42
if isinstance(marker_elem, list):
43
self.markers = marker_elem
47
self.key_marker = None
48
self.version_id_marker = None
49
self.is_truncated = False
50
self.next_token = None
53
def startElement(self, name, attrs, connection):
54
for t in self.markers:
56
obj = t[1](connection)
61
def to_boolean(self, value, true_value='true'):
62
if value == true_value:
67
def endElement(self, name, value, connection):
68
if name == 'IsTruncated':
69
self.is_truncated = self.to_boolean(value)
70
elif name == 'Marker':
72
elif name == 'KeyMarker':
73
self.key_marker = value
74
elif name == 'VersionIdMarker':
75
self.version_id_marker = value
76
elif name == 'Prefix':
78
elif name == 'return':
79
self.status = self.to_boolean(value)
80
elif name == 'StatusCode':
81
self.status = self.to_boolean(value, 'Success')
82
elif name == 'ItemName':
84
elif name == 'NextToken':
85
self.next_token = value
86
elif name == 'BoxUsage':
88
connection.box_usage += float(value)
91
elif name == 'IsValid':
92
self.status = self.to_boolean(value, 'True')
94
setattr(self, name, value)
96
class BooleanResult(object):
98
def __init__(self, marker_elem=None):
100
self.request_id = None
101
self.box_usage = None
109
def __nonzero__(self):
112
def startElement(self, name, attrs, connection):
115
def to_boolean(self, value, true_value='true'):
116
if value == true_value:
121
def endElement(self, name, value, connection):
123
self.status = self.to_boolean(value)
124
elif name == 'StatusCode':
125
self.status = self.to_boolean(value, 'Success')
126
elif name == 'IsValid':
127
self.status = self.to_boolean(value, 'True')
128
elif name == 'RequestId':
129
self.request_id = value
130
elif name == 'requestId':
131
self.request_id = value
132
elif name == 'BoxUsage':
133
self.request_id = value
135
setattr(self, name, value)