~vishvananda/nova/network-refactor

« back to all changes in this revision

Viewing changes to vendor/boto/boto/s3/bucketlistresultset.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
def 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 = k.name
 
35
        more_results= rs.is_truncated
 
36
        
 
37
class 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, headers=self.headers)
 
56
 
 
57
def versioned_bucket_lister(bucket, prefix='', delimiter='',
 
58
                            key_marker='', version_id_marker='', headers=None):
 
59
    """
 
60
    A generator function for listing versions in a bucket.
 
61
    """
 
62
    more_results = True
 
63
    k = None
 
64
    while more_results:
 
65
        rs = bucket.get_all_versions(prefix=prefix, key_marker=key_marker,
 
66
                                     version_id_marker=version_id_marker,
 
67
                                     delimiter=delimiter, headers=headers)
 
68
        for k in rs:
 
69
            yield k
 
70
        key_marker = rs.key_marker
 
71
        version_id_marker = rs.version_id_marker
 
72
        more_results= rs.is_truncated
 
73
        
 
74
class VersionedBucketListResultSet:
 
75
    """
 
76
    A resultset for listing versions within a bucket.  Uses the bucket_lister
 
77
    generator function and implements the iterator interface.  This
 
78
    transparently handles the results paging from S3 so even if you have
 
79
    many thousands of keys within the bucket you can iterate over all
 
80
    keys in a reasonably efficient manner.
 
81
    """
 
82
 
 
83
    def __init__(self, bucket=None, prefix='', delimiter='', key_marker='',
 
84
                 version_id_marker='', headers=None):
 
85
        self.bucket = bucket
 
86
        self.prefix = prefix
 
87
        self.delimiter = delimiter
 
88
        self.key_marker = key_marker
 
89
        self.version_id_marker = version_id_marker
 
90
        self.headers = headers
 
91
 
 
92
    def __iter__(self):
 
93
        return versioned_bucket_lister(self.bucket, prefix=self.prefix,
 
94
                                       delimiter=self.delimiter,
 
95
                                       key_marker=self.key_marker,
 
96
                                       version_id_marker=self.version_id_marker,
 
97
                                       headers=self.headers)
 
98
 
 
99