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: An instance of `Expiration`. This indicates |
| 38 the lifetime of the objects that are subject to the rule. |
| 39 |
| 40 :ivar transition: An instance of `Transition`. This indicates |
| 41 when to transition to a different storage class. |
| 42 |
| 43 """ |
| 44 def __init__(self, id=None, prefix=None, status=None, expiration=None, |
| 45 transition=None): |
| 46 self.id = id |
| 47 self.prefix = prefix |
| 48 self.status = status |
| 49 if isinstance(expiration, (int, long)): |
| 50 # retain backwards compatibility??? |
| 51 self.expiration = Expiration(days=expiration) |
| 52 else: |
| 53 # None or object |
| 54 self.expiration = expiration |
| 55 self.transition = transition |
| 56 |
| 57 def __repr__(self): |
| 58 return '<Rule: %s>' % self.id |
| 59 |
| 60 def startElement(self, name, attrs, connection): |
| 61 if name == 'Transition': |
| 62 self.transition = Transition() |
| 63 return self.transition |
| 64 elif name == 'Expiration': |
| 65 self.expiration = Expiration() |
| 66 return self.expiration |
| 67 return None |
| 68 |
| 69 def endElement(self, name, value, connection): |
| 70 if name == 'ID': |
| 71 self.id = value |
| 72 elif name == 'Prefix': |
| 73 self.prefix = value |
| 74 elif name == 'Status': |
| 75 self.status = value |
| 76 else: |
| 77 setattr(self, name, value) |
| 78 |
| 79 def to_xml(self): |
| 80 s = '<Rule>' |
| 81 s += '<ID>%s</ID>' % self.id |
| 82 s += '<Prefix>%s</Prefix>' % self.prefix |
| 83 s += '<Status>%s</Status>' % self.status |
| 84 if self.expiration is not None: |
| 85 s += self.expiration.to_xml() |
| 86 if self.transition is not None: |
| 87 s += self.transition.to_xml() |
| 88 s += '</Rule>' |
| 89 return s |
| 90 |
| 91 class Expiration(object): |
| 92 """ |
| 93 When an object will expire. |
| 94 |
| 95 :ivar days: The number of days until the object expires |
| 96 |
| 97 :ivar date: The date when the object will expire. Must be |
| 98 in ISO 8601 format. |
| 99 """ |
| 100 def __init__(self, days=None, date=None): |
| 101 self.days = days |
| 102 self.date = date |
| 103 |
| 104 def startElement(self, name, attrs, connection): |
| 105 return None |
| 106 |
| 107 def endElement(self, name, value, connection): |
| 108 if name == 'Days': |
| 109 self.days = int(value) |
| 110 elif name == 'Date': |
| 111 self.date = value |
| 112 |
| 113 def __repr__(self): |
| 114 if self.days is None: |
| 115 how_long = "on: %s" % self.date |
| 116 else: |
| 117 how_long = "in: %s days" % self.days |
| 118 return '<Expiration: %s>' % how_long |
| 119 |
| 120 def to_xml(self): |
| 121 s = '<Expiration>' |
| 122 if self.days is not None: |
| 123 s += '<Days>%s</Days>' % self.days |
| 124 elif self.date is not None: |
| 125 s += '<Date>%s</Date>' % self.date |
| 126 s += '</Expiration>' |
| 127 return s |
| 128 |
| 129 class Transition(object): |
| 130 """ |
| 131 A transition to a different storage class. |
| 132 |
| 133 :ivar days: The number of days until the object should be moved. |
| 134 |
| 135 :ivar date: The date when the object should be moved. Should be |
| 136 in ISO 8601 format. |
| 137 |
| 138 :ivar storage_class: The storage class to transition to. Valid |
| 139 values are GLACIER. |
| 140 |
| 141 """ |
| 142 def __init__(self, days=None, date=None, storage_class=None): |
| 143 self.days = days |
| 144 self.date = date |
| 145 self.storage_class = storage_class |
| 146 |
| 147 def startElement(self, name, attrs, connection): |
| 148 return None |
| 149 |
| 150 def endElement(self, name, value, connection): |
| 151 if name == 'Days': |
| 152 self.days = int(value) |
| 153 elif name == 'Date': |
| 154 self.date = value |
| 155 elif name == 'StorageClass': |
| 156 self.storage_class = value |
| 157 |
| 158 def __repr__(self): |
| 159 if self.days is None: |
| 160 how_long = "on: %s" % self.date |
| 161 else: |
| 162 how_long = "in: %s days" % self.days |
| 163 return '<Transition: %s, %s>' % (how_long, self.storage_class) |
| 164 |
| 165 def to_xml(self): |
| 166 s = '<Transition>' |
| 167 s += '<StorageClass>%s</StorageClass>' % self.storage_class |
| 168 if self.days is not None: |
| 169 s += '<Days>%s</Days>' % self.days |
| 170 elif self.date is not None: |
| 171 s += '<Date>%s</Date>' % self.date |
| 172 s += '</Transition>' |
| 173 return s |
| 174 |
| 175 class Lifecycle(list): |
| 176 """ |
| 177 A container for the rules associated with a Lifecycle configuration. |
| 178 """ |
| 179 |
| 180 def startElement(self, name, attrs, connection): |
| 181 if name == 'Rule': |
| 182 rule = Rule() |
| 183 self.append(rule) |
| 184 return rule |
| 185 return None |
| 186 |
| 187 def endElement(self, name, value, connection): |
| 188 setattr(self, name, value) |
| 189 |
| 190 def to_xml(self): |
| 191 """ |
| 192 Returns a string containing the XML version of the Lifecycle |
| 193 configuration as defined by S3. |
| 194 """ |
| 195 s = '<?xml version="1.0" encoding="UTF-8"?>' |
| 196 s += '<LifecycleConfiguration>' |
| 197 for rule in self: |
| 198 s += rule.to_xml() |
| 199 s += '</LifecycleConfiguration>' |
| 200 return s |
| 201 |
| 202 def add_rule(self, id, prefix, status, expiration, transition=None): |
| 203 """ |
| 204 Add a rule to this Lifecycle configuration. This only adds |
| 205 the rule to the local copy. To install the new rule(s) on |
| 206 the bucket, you need to pass this Lifecycle config object |
| 207 to the configure_lifecycle method of the Bucket object. |
| 208 |
| 209 :type id: str |
| 210 :param id: Unique identifier for the rule. The value cannot be longer |
| 211 than 255 characters. |
| 212 |
| 213 :type prefix: str |
| 214 :iparam prefix: Prefix identifying one or more objects to which the |
| 215 rule applies. |
| 216 |
| 217 :type status: str |
| 218 :param status: If 'Enabled', the rule is currently being applied. |
| 219 If 'Disabled', the rule is not currently being applied. |
| 220 |
| 221 :type expiration: int |
| 222 :param expiration: Indicates the lifetime, in days, of the objects |
| 223 that are subject to the rule. The value must be a non-zero |
| 224 positive integer. A Expiration object instance is also perfect. |
| 225 |
| 226 :type transition: Transition |
| 227 :param transition: Indicates when an object transitions to a |
| 228 different storage class. |
| 229 """ |
| 230 rule = Rule(id, prefix, status, expiration, transition) |
| 231 self.append(rule) |
OLD | NEW |