comparison env/lib/python3.9/site-packages/boto/resultset.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 # Copyright (c) 2006,2007 Mitch Garnaat http://garnaat.org/
2 #
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-
9 # lowing conditions:
10 #
11 # The above copyright notice and this permission notice shall be included
12 # in all copies or substantial portions of the Software.
13 #
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
20 # IN THE SOFTWARE.
21
22 from boto.s3.user import User
23
24
25 class ResultSet(list):
26 """
27 The ResultSet is used to pass results back from the Amazon services
28 to the client. It is light wrapper around Python's :py:class:`list` class,
29 with some additional methods for parsing XML results from AWS.
30 Because I don't really want any dependencies on external libraries,
31 I'm using the standard SAX parser that comes with Python. The good news is
32 that it's quite fast and efficient but it makes some things rather
33 difficult.
34
35 You can pass in, as the marker_elem parameter, a list of tuples.
36 Each tuple contains a string as the first element which represents
37 the XML element that the resultset needs to be on the lookout for
38 and a Python class as the second element of the tuple. Each time the
39 specified element is found in the XML, a new instance of the class
40 will be created and popped onto the stack.
41
42 :ivar str next_token: A hash used to assist in paging through very long
43 result sets. In most cases, passing this value to certain methods
44 will give you another 'page' of results.
45 """
46 def __init__(self, marker_elem=None):
47 list.__init__(self)
48 if isinstance(marker_elem, list):
49 self.markers = marker_elem
50 else:
51 self.markers = []
52 self.marker = None
53 self.key_marker = None
54 self.next_marker = None # avail when delimiter used
55 self.next_key_marker = None
56 self.next_upload_id_marker = None
57 self.next_version_id_marker = None
58 self.next_generation_marker = None
59 self.version_id_marker = None
60 self.is_truncated = False
61 self.next_token = None
62 self.status = True
63
64 def startElement(self, name, attrs, connection):
65 for t in self.markers:
66 if name == t[0]:
67 obj = t[1](connection)
68 self.append(obj)
69 return obj
70 if name == 'Owner':
71 # Makes owner available for get_service and
72 # perhaps other lists where not handled by
73 # another element.
74 self.owner = User()
75 return self.owner
76 return None
77
78 def to_boolean(self, value, true_value='true'):
79 if value == true_value:
80 return True
81 else:
82 return False
83
84 def endElement(self, name, value, connection):
85 if name == 'IsTruncated':
86 self.is_truncated = self.to_boolean(value)
87 elif name == 'Marker':
88 self.marker = value
89 elif name == 'KeyMarker':
90 self.key_marker = value
91 elif name == 'NextMarker':
92 self.next_marker = value
93 elif name == 'NextKeyMarker':
94 self.next_key_marker = value
95 elif name == 'VersionIdMarker':
96 self.version_id_marker = value
97 elif name == 'NextVersionIdMarker':
98 self.next_version_id_marker = value
99 elif name == 'NextGenerationMarker':
100 self.next_generation_marker = value
101 elif name == 'UploadIdMarker':
102 self.upload_id_marker = value
103 elif name == 'NextUploadIdMarker':
104 self.next_upload_id_marker = value
105 elif name == 'Bucket':
106 self.bucket = value
107 elif name == 'MaxUploads':
108 self.max_uploads = int(value)
109 elif name == 'MaxItems':
110 self.max_items = int(value)
111 elif name == 'Prefix':
112 self.prefix = value
113 elif name == 'return':
114 self.status = self.to_boolean(value)
115 elif name == 'StatusCode':
116 self.status = self.to_boolean(value, 'Success')
117 elif name == 'ItemName':
118 self.append(value)
119 elif name == 'NextToken':
120 self.next_token = value
121 elif name == 'nextToken':
122 self.next_token = value
123 # Code exists which expects nextToken to be available, so we
124 # set it here to remain backwards-compatibile.
125 self.nextToken = value
126 elif name == 'BoxUsage':
127 try:
128 connection.box_usage += float(value)
129 except:
130 pass
131 elif name == 'IsValid':
132 self.status = self.to_boolean(value, 'True')
133 else:
134 setattr(self, name, value)
135
136
137 class BooleanResult(object):
138
139 def __init__(self, marker_elem=None):
140 self.status = True
141 self.request_id = None
142 self.box_usage = None
143
144 def __repr__(self):
145 if self.status:
146 return 'True'
147 else:
148 return 'False'
149
150 def __nonzero__(self):
151 return self.status
152
153 def startElement(self, name, attrs, connection):
154 return None
155
156 def to_boolean(self, value, true_value='true'):
157 if value == true_value:
158 return True
159 else:
160 return False
161
162 def endElement(self, name, value, connection):
163 if name == 'return':
164 self.status = self.to_boolean(value)
165 elif name == 'StatusCode':
166 self.status = self.to_boolean(value, 'Success')
167 elif name == 'IsValid':
168 self.status = self.to_boolean(value, 'True')
169 elif name == 'RequestId':
170 self.request_id = value
171 elif name == 'requestId':
172 self.request_id = value
173 elif name == 'BoxUsage':
174 self.request_id = value
175 else:
176 setattr(self, name, value)