PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

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