OLD | NEW |
1 # Copyright 2010 Google Inc. | 1 # Copyright 2010 Google Inc. |
2 # | 2 # |
3 # Permission is hereby granted, free of charge, to any person obtaining a | 3 # Permission is hereby granted, free of charge, to any person obtaining a |
4 # copy of this software and associated documentation files (the | 4 # copy of this software and associated documentation files (the |
5 # "Software"), to deal in the Software without restriction, including | 5 # "Software"), to deal in the Software without restriction, including |
6 # without limitation the rights to use, copy, modify, merge, publish, dis- | 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 | 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- | 8 # persons to whom the Software is furnished to do so, subject to the fol- |
9 # lowing conditions: | 9 # lowing conditions: |
10 # | 10 # |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 self.content_type = headers['Content-Type'] | 85 self.content_type = headers['Content-Type'] |
86 | 86 |
87 def open_read(self, headers=NOT_IMPL, query_args=NOT_IMPL, | 87 def open_read(self, headers=NOT_IMPL, query_args=NOT_IMPL, |
88 override_num_retries=NOT_IMPL): | 88 override_num_retries=NOT_IMPL): |
89 pass | 89 pass |
90 | 90 |
91 def set_contents_from_file(self, fp, headers=None, replace=NOT_IMPL, | 91 def set_contents_from_file(self, fp, headers=None, replace=NOT_IMPL, |
92 cb=NOT_IMPL, num_cb=NOT_IMPL, | 92 cb=NOT_IMPL, num_cb=NOT_IMPL, |
93 policy=NOT_IMPL, md5=NOT_IMPL, | 93 policy=NOT_IMPL, md5=NOT_IMPL, |
94 res_upload_handler=NOT_IMPL): | 94 res_upload_handler=NOT_IMPL): |
95 self.data = fp.readlines() | 95 self.data = fp.read() |
96 self.size = len(self.data) | 96 self.size = len(self.data) |
97 self._handle_headers(headers) | 97 self._handle_headers(headers) |
98 | 98 |
99 def set_contents_from_string(self, s, headers=NOT_IMPL, replace=NOT_IMPL, | 99 def set_contents_from_string(self, s, headers=NOT_IMPL, replace=NOT_IMPL, |
100 cb=NOT_IMPL, num_cb=NOT_IMPL, policy=NOT_IMPL, | 100 cb=NOT_IMPL, num_cb=NOT_IMPL, policy=NOT_IMPL, |
101 md5=NOT_IMPL, reduced_redundancy=NOT_IMPL): | 101 md5=NOT_IMPL, reduced_redundancy=NOT_IMPL): |
102 self.data = copy.copy(s) | 102 self.data = copy.copy(s) |
103 self.size = len(s) | 103 self.size = len(s) |
104 self._handle_headers(headers) | 104 self._handle_headers(headers) |
105 | 105 |
| 106 def set_contents_from_filename(self, filename, headers=None, replace=NOT_IMP
L, |
| 107 cb=NOT_IMPL, num_cb=NOT_IMPL, |
| 108 policy=NOT_IMPL, md5=NOT_IMPL, |
| 109 res_upload_handler=NOT_IMPL): |
| 110 fp = open(filename, 'rb') |
| 111 self.set_contents_from_file(fp, headers, replace, cb, num_cb, |
| 112 policy, md5, res_upload_handler) |
| 113 fp.close() |
| 114 |
| 115 def copy(self, dst_bucket_name, dst_key, metadata=NOT_IMPL, |
| 116 reduced_redundancy=NOT_IMPL, preserve_acl=NOT_IMPL): |
| 117 dst_bucket = self.bucket.connection.get_bucket(dst_bucket_name) |
| 118 return dst_bucket.copy_key(dst_key, self.bucket.name, |
| 119 self.name, metadata) |
| 120 |
106 | 121 |
107 class MockBucket(object): | 122 class MockBucket(object): |
108 | 123 |
109 def __init__(self, connection=NOT_IMPL, name=None, key_class=NOT_IMPL): | 124 def __init__(self, connection=None, name=None, key_class=NOT_IMPL): |
110 self.name = name | 125 self.name = name |
111 self.keys = {} | 126 self.keys = {} |
112 self.acls = {name: MockAcl()} | 127 self.acls = {name: MockAcl()} |
| 128 self.subresources = {} |
| 129 self.connection = connection |
| 130 self.logging = False |
113 | 131 |
114 def copy_key(self, new_key_name, src_bucket_name, | 132 def copy_key(self, new_key_name, src_bucket_name, |
115 src_key_name, metadata=NOT_IMPL, src_version_id=NOT_IMPL, | 133 src_key_name, metadata=NOT_IMPL, src_version_id=NOT_IMPL, |
116 storage_class=NOT_IMPL, preserve_acl=NOT_IMPL): | 134 storage_class=NOT_IMPL, preserve_acl=NOT_IMPL): |
117 new_key = self.new_key(key_name=new_key_name) | 135 new_key = self.new_key(key_name=new_key_name) |
118 src_key = mock_connection.get_bucket( | 136 src_key = mock_connection.get_bucket( |
119 src_bucket_name).get_key(src_key_name) | 137 src_bucket_name).get_key(src_key_name) |
120 new_key.data = copy.copy(src_key.data) | 138 new_key.data = copy.copy(src_key.data) |
121 new_key.size = len(new_key.data) | 139 new_key.size = len(new_key.data) |
| 140 return new_key |
| 141 |
| 142 def disable_logging(self): |
| 143 self.logging = False |
| 144 |
| 145 def enable_logging(self, target_bucket_prefix): |
| 146 self.logging = True |
122 | 147 |
123 def get_acl(self, key_name='', headers=NOT_IMPL, version_id=NOT_IMPL): | 148 def get_acl(self, key_name='', headers=NOT_IMPL, version_id=NOT_IMPL): |
124 if key_name: | 149 if key_name: |
125 # Return ACL for the key. | 150 # Return ACL for the key. |
126 return self.acls[key_name] | 151 return self.acls[key_name] |
127 else: | 152 else: |
128 # Return ACL for the bucket. | 153 # Return ACL for the bucket. |
129 return self.acls[self.name] | 154 return self.acls[self.name] |
130 | 155 |
| 156 def get_subresource(self, subresource, key_name=NOT_IMPL, headers=NOT_IMPL, |
| 157 version_id=NOT_IMPL): |
| 158 if subresource in self.subresources: |
| 159 return self.subresources[subresource] |
| 160 else: |
| 161 return '<Subresource/>' |
| 162 |
131 def new_key(self, key_name=None): | 163 def new_key(self, key_name=None): |
132 mock_key = MockKey(self, key_name) | 164 mock_key = MockKey(self, key_name) |
133 self.keys[key_name] = mock_key | 165 self.keys[key_name] = mock_key |
134 self.acls[key_name] = MockAcl() | 166 self.acls[key_name] = MockAcl() |
135 return mock_key | 167 return mock_key |
136 | 168 |
137 def delete_key(self, key_name, headers=NOT_IMPL, | 169 def delete_key(self, key_name, headers=NOT_IMPL, |
138 version_id=NOT_IMPL, mfa_token=NOT_IMPL): | 170 version_id=NOT_IMPL, mfa_token=NOT_IMPL): |
139 if key_name not in self.keys: | 171 if key_name not in self.keys: |
140 raise boto.exception.StorageResponseError(404, 'Not Found') | 172 raise boto.exception.StorageResponseError(404, 'Not Found') |
(...skipping 25 matching lines...) Expand all Loading... |
166 version_id=NOT_IMPL): | 198 version_id=NOT_IMPL): |
167 # We only handle setting ACL XML here; if you pass a canned ACL | 199 # We only handle setting ACL XML here; if you pass a canned ACL |
168 # the get_acl call will just return that string name. | 200 # the get_acl call will just return that string name. |
169 if key_name: | 201 if key_name: |
170 # Set ACL for the key. | 202 # Set ACL for the key. |
171 self.acls[key_name] = acl_or_str | 203 self.acls[key_name] = acl_or_str |
172 else: | 204 else: |
173 # Set ACL for the bucket. | 205 # Set ACL for the bucket. |
174 self.acls[self.name] = acl_or_str | 206 self.acls[self.name] = acl_or_str |
175 | 207 |
| 208 def set_subresource(self, subresource, value, key_name=NOT_IMPL, |
| 209 headers=NOT_IMPL, version_id=NOT_IMPL): |
| 210 self.subresources[subresource] = value |
| 211 |
176 | 212 |
177 class MockConnection(object): | 213 class MockConnection(object): |
178 | 214 |
179 def __init__(self, aws_access_key_id=NOT_IMPL, | 215 def __init__(self, aws_access_key_id=NOT_IMPL, |
180 aws_secret_access_key=NOT_IMPL, is_secure=NOT_IMPL, | 216 aws_secret_access_key=NOT_IMPL, is_secure=NOT_IMPL, |
181 port=NOT_IMPL, proxy=NOT_IMPL, proxy_port=NOT_IMPL, | 217 port=NOT_IMPL, proxy=NOT_IMPL, proxy_port=NOT_IMPL, |
182 proxy_user=NOT_IMPL, proxy_pass=NOT_IMPL, | 218 proxy_user=NOT_IMPL, proxy_pass=NOT_IMPL, |
183 host=NOT_IMPL, debug=NOT_IMPL, | 219 host=NOT_IMPL, debug=NOT_IMPL, |
184 https_connection_factory=NOT_IMPL, | 220 https_connection_factory=NOT_IMPL, |
185 calling_format=NOT_IMPL, | 221 calling_format=NOT_IMPL, |
186 path=NOT_IMPL, provider=NOT_IMPL, | 222 path=NOT_IMPL, provider=NOT_IMPL, |
187 bucket_class=NOT_IMPL): | 223 bucket_class=NOT_IMPL): |
188 self.buckets = {} | 224 self.buckets = {} |
189 | 225 |
190 def create_bucket(self, bucket_name, headers=NOT_IMPL, location=NOT_IMPL, | 226 def create_bucket(self, bucket_name, headers=NOT_IMPL, location=NOT_IMPL, |
191 policy=NOT_IMPL): | 227 policy=NOT_IMPL): |
192 if bucket_name in self.buckets: | 228 if bucket_name in self.buckets: |
193 raise boto.exception.StorageCreateError( | 229 raise boto.exception.StorageCreateError( |
194 409, 'BucketAlreadyOwnedByYou', 'bucket already exists') | 230 409, 'BucketAlreadyOwnedByYou', |
195 mock_bucket = MockBucket(name=bucket_name) | 231 "<Message>Your previous request to create the named bucket " |
| 232 "succeeded and you already own it.</Message>") |
| 233 mock_bucket = MockBucket(name=bucket_name, connection=self) |
196 self.buckets[bucket_name] = mock_bucket | 234 self.buckets[bucket_name] = mock_bucket |
197 return mock_bucket | 235 return mock_bucket |
198 | 236 |
199 def delete_bucket(self, bucket, headers=NOT_IMPL): | 237 def delete_bucket(self, bucket, headers=NOT_IMPL): |
200 if bucket not in self.buckets: | 238 if bucket not in self.buckets: |
201 raise boto.exception.StorageResponseError(404, 'NoSuchBucket', | 239 raise boto.exception.StorageResponseError( |
202 'no such bucket') | 240 404, 'NoSuchBucket', '<Message>no such bucket</Message>') |
203 del self.buckets[bucket] | 241 del self.buckets[bucket] |
204 | 242 |
205 def get_bucket(self, bucket_name, validate=NOT_IMPL, headers=NOT_IMPL): | 243 def get_bucket(self, bucket_name, validate=NOT_IMPL, headers=NOT_IMPL): |
206 if bucket_name not in self.buckets: | 244 if bucket_name not in self.buckets: |
207 raise boto.exception.StorageResponseError(404, 'NoSuchBucket', | 245 raise boto.exception.StorageResponseError(404, 'NoSuchBucket', |
208 'Not Found') | 246 'Not Found') |
209 return self.buckets[bucket_name] | 247 return self.buckets[bucket_name] |
210 | 248 |
211 def get_all_buckets(self, headers=NOT_IMPL): | 249 def get_all_buckets(self, headers=NOT_IMPL): |
212 return self.buckets.itervalues() | 250 return self.buckets.itervalues() |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 policy=NOT_IMPL): | 289 policy=NOT_IMPL): |
252 return self.connect().create_bucket(self.bucket_name) | 290 return self.connect().create_bucket(self.bucket_name) |
253 | 291 |
254 def delete_bucket(self, headers=NOT_IMPL): | 292 def delete_bucket(self, headers=NOT_IMPL): |
255 return self.connect().delete_bucket(self.bucket_name) | 293 return self.connect().delete_bucket(self.bucket_name) |
256 | 294 |
257 def delete_key(self, validate=NOT_IMPL, headers=NOT_IMPL, | 295 def delete_key(self, validate=NOT_IMPL, headers=NOT_IMPL, |
258 version_id=NOT_IMPL, mfa_token=NOT_IMPL): | 296 version_id=NOT_IMPL, mfa_token=NOT_IMPL): |
259 self.get_bucket().delete_key(self.object_name) | 297 self.get_bucket().delete_key(self.object_name) |
260 | 298 |
| 299 def disable_logging(self, validate=NOT_IMPL, headers=NOT_IMPL, |
| 300 version_id=NOT_IMPL): |
| 301 self.get_bucket().disable_logging() |
| 302 |
| 303 def enable_logging(self, target_bucket, target_prefix, canned_acl=NOT_IMPL, |
| 304 validate=NOT_IMPL, headers=NOT_IMPL, |
| 305 version_id=NOT_IMPL): |
| 306 self.get_bucket().enable_logging(target_bucket) |
| 307 |
261 def equals(self, uri): | 308 def equals(self, uri): |
262 return self.uri == uri.uri | 309 return self.uri == uri.uri |
263 | 310 |
264 def get_acl(self, validate=NOT_IMPL, headers=NOT_IMPL, version_id=NOT_IMPL): | 311 def get_acl(self, validate=NOT_IMPL, headers=NOT_IMPL, version_id=NOT_IMPL): |
265 return self.get_bucket().get_acl(self.object_name) | 312 return self.get_bucket().get_acl(self.object_name) |
266 | 313 |
| 314 def get_subresource(self, subresource, validate=NOT_IMPL, headers=NOT_IMPL, |
| 315 version_id=NOT_IMPL): |
| 316 return self.get_bucket().get_subresource(subresource, self.object_name) |
| 317 |
267 def get_all_buckets(self, headers=NOT_IMPL): | 318 def get_all_buckets(self, headers=NOT_IMPL): |
268 return self.connect().get_all_buckets() | 319 return self.connect().get_all_buckets() |
269 | 320 |
270 def get_all_keys(self, validate=NOT_IMPL, headers=NOT_IMPL): | 321 def get_all_keys(self, validate=NOT_IMPL, headers=NOT_IMPL): |
271 return self.get_bucket().get_all_keys(self) | 322 return self.get_bucket().get_all_keys(self) |
272 | 323 |
273 def get_bucket(self, validate=NOT_IMPL, headers=NOT_IMPL): | 324 def get_bucket(self, validate=NOT_IMPL, headers=NOT_IMPL): |
274 return self.connect().get_bucket(self.bucket_name) | 325 return self.connect().get_bucket(self.bucket_name) |
275 | 326 |
276 def get_key(self, validate=NOT_IMPL, headers=NOT_IMPL, | 327 def get_key(self, validate=NOT_IMPL, headers=NOT_IMPL, |
(...skipping 12 matching lines...) Expand all Loading... |
289 def names_singleton(self): | 340 def names_singleton(self): |
290 return self.object_name | 341 return self.object_name |
291 | 342 |
292 def new_key(self, validate=NOT_IMPL, headers=NOT_IMPL): | 343 def new_key(self, validate=NOT_IMPL, headers=NOT_IMPL): |
293 bucket = self.get_bucket() | 344 bucket = self.get_bucket() |
294 return bucket.new_key(self.object_name) | 345 return bucket.new_key(self.object_name) |
295 | 346 |
296 def set_acl(self, acl_or_str, key_name='', validate=NOT_IMPL, | 347 def set_acl(self, acl_or_str, key_name='', validate=NOT_IMPL, |
297 headers=NOT_IMPL, version_id=NOT_IMPL): | 348 headers=NOT_IMPL, version_id=NOT_IMPL): |
298 self.get_bucket().set_acl(acl_or_str, key_name) | 349 self.get_bucket().set_acl(acl_or_str, key_name) |
| 350 |
| 351 def set_subresource(self, subresource, value, validate=NOT_IMPL, |
| 352 headers=NOT_IMPL, version_id=NOT_IMPL): |
| 353 self.get_bucket().set_subresource(subresource, value, self.object_name) |
OLD | NEW |