1# Copyright (c) 2011 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
22from boto import handler
23import xml.sax
24
25class Deleted(object):
26    """
27    A successfully deleted object in a multi-object delete request.
28
29    :ivar key: Key name of the object that was deleted.
30
31    :ivar version_id: Version id of the object that was deleted.
32
33    :ivar delete_marker: If True, indicates the object deleted
34        was a DeleteMarker.
35
36    :ivar delete_marker_version_id: Version ID of the delete marker
37        deleted.
38    """
39    def __init__(self, key=None, version_id=None,
40                 delete_marker=False, delete_marker_version_id=None):
41        self.key = key
42        self.version_id = version_id
43        self.delete_marker = delete_marker
44        self.delete_marker_version_id = delete_marker_version_id
45
46    def __repr__(self):
47        if self.version_id:
48            return '<Deleted: %s.%s>' % (self.key, self.version_id)
49        else:
50            return '<Deleted: %s>' % self.key
51
52    def startElement(self, name, attrs, connection):
53        return None
54
55    def endElement(self, name, value, connection):
56        if name == 'Key':
57            self.key = value
58        elif name == 'VersionId':
59            self.version_id = value
60        elif name == 'DeleteMarker':
61            if value.lower() == 'true':
62                self.delete_marker = True
63        elif name == 'DeleteMarkerVersionId':
64            self.delete_marker_version_id = value
65        else:
66            setattr(self, name, value)
67
68class Error(object):
69    """
70    An unsuccessful deleted object in a multi-object delete request.
71
72    :ivar key: Key name of the object that was not deleted.
73
74    :ivar version_id: Version id of the object that was not deleted.
75
76    :ivar code: Status code of the failed delete operation.
77
78    :ivar message: Status message of the failed delete operation.
79    """
80    def __init__(self, key=None, version_id=None,
81                 code=None, message=None):
82        self.key = key
83        self.version_id = version_id
84        self.code = code
85        self.message = message
86
87    def __repr__(self):
88        if self.version_id:
89            return '<Error: %s.%s(%s)>' % (self.key, self.version_id,
90                                           self.code)
91        else:
92            return '<Error: %s(%s)>' % (self.key, self.code)
93
94    def startElement(self, name, attrs, connection):
95        return None
96
97    def endElement(self, name, value, connection):
98        if name == 'Key':
99            self.key = value
100        elif name == 'VersionId':
101            self.version_id = value
102        elif name == 'Code':
103            self.code = value
104        elif name == 'Message':
105            self.message = value
106        else:
107            setattr(self, name, value)
108
109class MultiDeleteResult(object):
110    """
111    The status returned from a MultiObject Delete request.
112
113    :ivar deleted: A list of successfully deleted objects.  Note that if
114        the quiet flag was specified in the request, this list will
115        be empty because only error responses would be returned.
116
117    :ivar errors: A list of unsuccessfully deleted objects.
118    """
119
120    def __init__(self, bucket=None):
121        self.bucket = None
122        self.deleted = []
123        self.errors = []
124
125    def startElement(self, name, attrs, connection):
126        if name == 'Deleted':
127            d = Deleted()
128            self.deleted.append(d)
129            return d
130        elif name == 'Error':
131            e = Error()
132            self.errors.append(e)
133            return e
134        return None
135
136    def endElement(self, name, value, connection):
137        setattr(self, name, value)
138
139