| OLD | NEW |
| (Empty) | |
| 1 # Copyright (c) 2012 Mitch Garnaat http://garnaat.org/ |
| 2 # Copyright (c) 2012 Amazon.com, Inc. or its affiliates. All Rights Reserved |
| 3 # |
| 4 # Permission is hereby granted, free of charge, to any person obtaining a |
| 5 # copy of this software and associated documentation files (the |
| 6 # "Software"), to deal in the Software without restriction, including |
| 7 # without limitation the rights to use, copy, modify, merge, publish, dis- |
| 8 # tribute, sublicense, and/or sell copies of the Software, and to permit |
| 9 # persons to whom the Software is furnished to do so, subject to the fol- |
| 10 # lowing conditions: |
| 11 # |
| 12 # The above copyright notice and this permission notice shall be included |
| 13 # in all copies or substantial portions of the Software. |
| 14 # |
| 15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 16 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- |
| 17 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 18 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 19 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 21 # IN THE SOFTWARE. |
| 22 |
| 23 |
| 24 class Rule(object): |
| 25 """ |
| 26 A Lifcycle rule for an S3 bucket. |
| 27 |
| 28 :ivar id: Unique identifier for the rule. The value cannot be longer |
| 29 than 255 characters. |
| 30 |
| 31 :ivar prefix: Prefix identifying one or more objects to which the |
| 32 rule applies. |
| 33 |
| 34 :ivar status: If Enabled, the rule is currently being applied. |
| 35 If Disabled, the rule is not currently being applied. |
| 36 |
| 37 :ivar expiration: Indicates the lifetime, in days, of the objects |
| 38 that are subject to the rule. The value must be a non-zero |
| 39 positive integer. |
| 40 |
| 41 :ivar transition: An instance of `Transition`. This indicates |
| 42 when to transition to a different storage class. |
| 43 |
| 44 """ |
| 45 def __init__(self, id=None, prefix=None, status=None, expiration=None, |
| 46 transition=None): |
| 47 self.id = id |
| 48 self.prefix = prefix |
| 49 self.status = status |
| 50 self.expiration = expiration |
| 51 self.transition = transition |
| 52 |
| 53 def __repr__(self): |
| 54 return '<Rule: %s>' % self.id |
| 55 |
| 56 def startElement(self, name, attrs, connection): |
| 57 if name == 'Transition': |
| 58 self.transition = Transition() |
| 59 return self.transition |
| 60 return None |
| 61 |
| 62 |
| 63 def endElement(self, name, value, connection): |
| 64 if name == 'ID': |
| 65 self.id = value |
| 66 elif name == 'Prefix': |
| 67 self.prefix = value |
| 68 elif name == 'Status': |
| 69 self.status = value |
| 70 elif name == 'Days': |
| 71 self.expiration = int(value) |
| 72 else: |
| 73 setattr(self, name, value) |
| 74 |
| 75 def to_xml(self): |
| 76 s = '<Rule>' |
| 77 s += '<ID>%s</ID>' % self.id |
| 78 s += '<Prefix>%s</Prefix>' % self.prefix |
| 79 s += '<Status>%s</Status>' % self.status |
| 80 if self.expiration is not None: |
| 81 s += '<Expiration><Days>%d</Days></Expiration>' % self.expiration |
| 82 if self.transition is not None: |
| 83 transition = self.transition |
| 84 s += ('<Transition><StorageClass>%s</StorageClass>' % |
| 85 transition.storage_class) |
| 86 if transition.days is not None: |
| 87 s += '<Days>%s</Days>' % transition.days |
| 88 elif transition.date is not None: |
| 89 s += '<Date>%s</Date>' % transition.date |
| 90 s += '</Transition>' |
| 91 s += '</Rule>' |
| 92 return s |
| 93 |
| 94 |
| 95 class Transition(object): |
| 96 """ |
| 97 A transition to a different storage class. |
| 98 |
| 99 :ivar days: The number of days until the object should be moved. |
| 100 |
| 101 :ivar date: The date when the object should be moved. Should be |
| 102 in ISO 8601 format. |
| 103 |
| 104 :ivar storage_class: The storage class to transition to. Valid |
| 105 values are STANDARD, REDUCED_REDUNDANCY and GLACIER. |
| 106 |
| 107 """ |
| 108 def __init__(self, days=None, date=None, storage_class=None): |
| 109 self.days = days |
| 110 self.date = date |
| 111 self.storage_class = storage_class |
| 112 |
| 113 def startElement(self, name, attrs, connection): |
| 114 return None |
| 115 |
| 116 def endElement(self, name, value, connection): |
| 117 if name == 'Days': |
| 118 self.days = int(value) |
| 119 elif name == 'Date': |
| 120 self.date = value |
| 121 elif name == 'StorageClass': |
| 122 self.storage_class = value |
| 123 |
| 124 def __repr__(self): |
| 125 if self.days is None: |
| 126 how_long = "on: %s" % self.date |
| 127 else: |
| 128 how_long = "in: %s days" % self.days |
| 129 return '<Transition: %s, %s>' % (how_long, self.storage_class) |
| 130 |
| 131 |
| 132 class Lifecycle(list): |
| 133 """ |
| 134 A container for the rules associated with a Lifecycle configuration. |
| 135 """ |
| 136 |
| 137 def startElement(self, name, attrs, connection): |
| 138 if name == 'Rule': |
| 139 rule = Rule() |
| 140 self.append(rule) |
| 141 return rule |
| 142 return None |
| 143 |
| 144 def endElement(self, name, value, connection): |
| 145 setattr(self, name, value) |
| 146 |
| 147 def to_xml(self): |
| 148 """ |
| 149 Returns a string containing the XML version of the Lifecycle |
| 150 configuration as defined by S3. |
| 151 """ |
| 152 s = '<LifecycleConfiguration>' |
| 153 for rule in self: |
| 154 s += rule.to_xml() |
| 155 s += '</LifecycleConfiguration>' |
| 156 return s |
| 157 |
| 158 def add_rule(self, id, prefix, status, expiration): |
| 159 """ |
| 160 Add a rule to this Lifecycle configuration. This only adds |
| 161 the rule to the local copy. To install the new rule(s) on |
| 162 the bucket, you need to pass this Lifecycle config object |
| 163 to the configure_lifecycle method of the Bucket object. |
| 164 |
| 165 :type id: str |
| 166 :param id: Unique identifier for the rule. The value cannot be longer |
| 167 than 255 characters. |
| 168 |
| 169 :type prefix: str |
| 170 :iparam prefix: Prefix identifying one or more objects to which the |
| 171 rule applies. |
| 172 |
| 173 :type status: str |
| 174 :param status: If 'Enabled', the rule is currently being applied. |
| 175 If 'Disabled', the rule is not currently being applied. |
| 176 |
| 177 :type expiration: int |
| 178 :param expiration: Indicates the lifetime, in days, of the objects |
| 179 that are subject to the rule. The value must be a non-zero |
| 180 positive integer. |
| 181 """ |
| 182 rule = Rule(id, prefix, status, expiration) |
| 183 self.append(rule) |
| OLD | NEW |