PageRenderTime 32ms CodeModel.GetById 14ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/boto-2.5.2/boto/s3/bucketlistresultset.py

#
Python | 141 lines | 120 code | 1 blank | 20 comment | 0 complexity | de512761ae287c43955f5c6c2b47851d MD5 | raw file
  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
 22def bucket_lister(bucket, prefix='', delimiter='', marker='', headers=None):
 23    """
 24    A generator function for listing keys in a bucket.
 25    """
 26    more_results = True
 27    k = None
 28    while more_results:
 29        rs = bucket.get_all_keys(prefix=prefix, marker=marker,
 30                                 delimiter=delimiter, headers=headers)
 31        for k in rs:
 32            yield k
 33        if k:
 34            marker = rs.next_marker or k.name
 35        more_results= rs.is_truncated
 36        
 37class BucketListResultSet:
 38    """
 39    A resultset for listing keys within a bucket.  Uses the bucket_lister
 40    generator function and implements the iterator interface.  This
 41    transparently handles the results paging from S3 so even if you have
 42    many thousands of keys within the bucket you can iterate over all
 43    keys in a reasonably efficient manner.
 44    """
 45
 46    def __init__(self, bucket=None, prefix='', delimiter='', marker='', headers=None):
 47        self.bucket = bucket
 48        self.prefix = prefix
 49        self.delimiter = delimiter
 50        self.marker = marker
 51        self.headers = headers
 52
 53    def __iter__(self):
 54        return bucket_lister(self.bucket, prefix=self.prefix,
 55                             delimiter=self.delimiter, marker=self.marker,
 56                             headers=self.headers)
 57
 58def versioned_bucket_lister(bucket, prefix='', delimiter='',
 59                            key_marker='', version_id_marker='', headers=None):
 60    """
 61    A generator function for listing versions in a bucket.
 62    """
 63    more_results = True
 64    k = None
 65    while more_results:
 66        rs = bucket.get_all_versions(prefix=prefix, key_marker=key_marker,
 67                                     version_id_marker=version_id_marker,
 68                                     delimiter=delimiter, headers=headers,
 69                                     max_keys=999)
 70        for k in rs:
 71            yield k
 72        key_marker = rs.next_key_marker
 73        version_id_marker = rs.next_version_id_marker
 74        more_results= rs.is_truncated
 75        
 76class VersionedBucketListResultSet:
 77    """
 78    A resultset for listing versions within a bucket.  Uses the bucket_lister
 79    generator function and implements the iterator interface.  This
 80    transparently handles the results paging from S3 so even if you have
 81    many thousands of keys within the bucket you can iterate over all
 82    keys in a reasonably efficient manner.
 83    """
 84
 85    def __init__(self, bucket=None, prefix='', delimiter='', key_marker='',
 86                 version_id_marker='', headers=None):
 87        self.bucket = bucket
 88        self.prefix = prefix
 89        self.delimiter = delimiter
 90        self.key_marker = key_marker
 91        self.version_id_marker = version_id_marker
 92        self.headers = headers
 93
 94    def __iter__(self):
 95        return versioned_bucket_lister(self.bucket, prefix=self.prefix,
 96                                       delimiter=self.delimiter,
 97                                       key_marker=self.key_marker,
 98                                       version_id_marker=self.version_id_marker,
 99                                       headers=self.headers)
100
101def multipart_upload_lister(bucket, key_marker='',
102                            upload_id_marker='',
103                            headers=None):
104    """
105    A generator function for listing multipart uploads in a bucket.
106    """
107    more_results = True
108    k = None
109    while more_results:
110        rs = bucket.get_all_multipart_uploads(key_marker=key_marker,
111                                              upload_id_marker=upload_id_marker,
112                                              headers=headers)
113        for k in rs:
114            yield k
115        key_marker = rs.next_key_marker
116        upload_id_marker = rs.next_upload_id_marker
117        more_results= rs.is_truncated
118        
119class MultiPartUploadListResultSet:
120    """
121    A resultset for listing multipart uploads within a bucket.
122    Uses the multipart_upload_lister generator function and
123    implements the iterator interface.  This
124    transparently handles the results paging from S3 so even if you have
125    many thousands of uploads within the bucket you can iterate over all
126    keys in a reasonably efficient manner.
127    """
128    def __init__(self, bucket=None, key_marker='',
129                 upload_id_marker='', headers=None):
130        self.bucket = bucket
131        self.key_marker = key_marker
132        self.upload_id_marker = upload_id_marker
133        self.headers = headers
134
135    def __iter__(self):
136        return multipart_upload_lister(self.bucket,
137                                       key_marker=self.key_marker,
138                                       upload_id_marker=self.upload_id_marker,
139                                       headers=self.headers)
140
141