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