PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/boto-2.5.2/boto/s3/lifecycle.py

#
Python | 128 lines | 59 code | 11 blank | 58 comment | 7 complexity | 5836634d0fe3780853afdc7292f4c1fc MD5 | raw file
  1. # Copyright (c) 2012 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. from boto import handler
  22. import xml.sax
  23. class Rule(object):
  24. """
  25. A Lifcycle rule for an S3 bucket.
  26. :ivar id: Unique identifier for the rule. The value cannot be longer
  27. than 255 characters.
  28. :ivar prefix: Prefix identifying one or more objects to which the
  29. rule applies.
  30. :ivar status: If Enabled, the rule is currently being applied.
  31. If Disabled, the rule is not currently being applied.
  32. :ivar expiration: Indicates the lifetime, in days, of the objects
  33. that are subject to the rule. The value must be a non-zero
  34. positive integer.
  35. """
  36. def __init__(self, id=None, prefix=None, status=None, expiration=None):
  37. self.id = id
  38. self.prefix = prefix
  39. self.status = status
  40. self.expiration = expiration
  41. def __repr__(self):
  42. return '<Rule: %s>' % self.id
  43. def startElement(self, name, attrs, connection):
  44. return None
  45. def endElement(self, name, value, connection):
  46. if name == 'ID':
  47. self.id = value
  48. elif name == 'Prefix':
  49. self.prefix = value
  50. elif name == 'Status':
  51. self.status = value
  52. elif name == 'Days':
  53. self.expiration = int(value)
  54. else:
  55. setattr(self, name, value)
  56. def to_xml(self):
  57. s = '<Rule>'
  58. s += '<ID>%s</ID>' % self.id
  59. s += '<Prefix>%s</Prefix>' % self.prefix
  60. s += '<Status>%s</Status>' % self.status
  61. s += '<Expiration><Days>%d</Days></Expiration>' % self.expiration
  62. s += '</Rule>'
  63. return s
  64. class Lifecycle(list):
  65. """
  66. A container for the rules associated with a Lifecycle configuration.
  67. """
  68. def startElement(self, name, attrs, connection):
  69. if name == 'Rule':
  70. rule = Rule()
  71. self.append(rule)
  72. return rule
  73. return None
  74. def endElement(self, name, value, connection):
  75. setattr(self, name, value)
  76. def to_xml(self):
  77. """
  78. Returns a string containing the XML version of the Lifecycle
  79. configuration as defined by S3.
  80. """
  81. s = '<LifecycleConfiguration>'
  82. for rule in self:
  83. s += rule.to_xml()
  84. s += '</LifecycleConfiguration>'
  85. return s
  86. def add_rule(self, id, prefix, status, expiration):
  87. """
  88. Add a rule to this Lifecycle configuration. This only adds
  89. the rule to the local copy. To install the new rule(s) on
  90. the bucket, you need to pass this Lifecycle config object
  91. to the configure_lifecycle method of the Bucket object.
  92. :type id: str
  93. :param id: Unique identifier for the rule. The value cannot be longer
  94. than 255 characters.
  95. :type prefix: str
  96. :iparam prefix: Prefix identifying one or more objects to which the
  97. rule applies.
  98. :type status: str
  99. :param status: If 'Enabled', the rule is currently being applied.
  100. If 'Disabled', the rule is not currently being applied.
  101. :type expiration: int
  102. :param expiration: Indicates the lifetime, in days, of the objects
  103. that are subject to the rule. The value must be a non-zero
  104. positive integer.
  105. """
  106. rule = Rule(id, prefix, status, expiration)
  107. self.append(rule)