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

/boto-2.5.2/boto/s3/bucketlogging.py

#
Python | 83 lines | 54 code | 8 blank | 21 comment | 10 complexity | 6cebeffa6cad65a890c98a00c59a6cd5 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
22import xml.sax.saxutils
23from acl import Grant
24
25class BucketLogging:
26
27    def __init__(self, target=None, prefix=None, grants=None):
28        self.target = target
29        self.prefix = prefix
30        if grants is None:
31            self.grants = []
32        else:
33            self.grants = grants
34
35    def __repr__(self):
36        if self.target is None:
37            return "<BucketLoggingStatus: Disabled>"
38        grants = []
39        for g in self.grants:
40            if g.type == 'CanonicalUser':
41                u = g.display_name
42            elif g.type == 'Group':
43                u = g.uri
44            else:
45                u = g.email_address
46            grants.append("%s = %s" % (u, g.permission))
47        return "<BucketLoggingStatus: %s/%s (%s)>" % (self.target, self.prefix, ", ".join(grants))
48
49    def add_grant(self, grant):
50        self.grants.append(grant)
51
52    def startElement(self, name, attrs, connection):
53        if name == 'Grant':
54            self.grants.append(Grant())
55            return self.grants[-1]
56        else:
57            return None
58
59    def endElement(self, name, value, connection):
60        if name == 'TargetBucket':
61            self.target = value
62        elif name == 'TargetPrefix':
63            self.prefix = value
64        else:
65            setattr(self, name, value)
66
67    def to_xml(self):
68        # caller is responsible to encode to utf-8
69        s = u'<?xml version="1.0" encoding="UTF-8"?>'
70        s += u'<BucketLoggingStatus xmlns="http://doc.s3.amazonaws.com/2006-03-01">'
71        if self.target is not None: 
72            s += u'<LoggingEnabled>'
73            s += u'<TargetBucket>%s</TargetBucket>' % self.target
74            prefix = self.prefix or ''
75            s += u'<TargetPrefix>%s</TargetPrefix>' % xml.sax.saxutils.escape(prefix)
76            if self.grants:
77                s += '<TargetGrants>'
78                for grant in self.grants:
79                    s += grant.to_xml()
80                s += '</TargetGrants>'
81            s += u'</LoggingEnabled>'
82        s += u'</BucketLoggingStatus>'
83        return s