comparison env/lib/python3.7/site-packages/boto/s3/lifecycle.py @ 0:26e78fe6e8c4 draft

"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
author shellac
date Sat, 02 May 2020 07:14:21 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:26e78fe6e8c4
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 from boto.compat import six
23
24 class Rule(object):
25 """
26 A Lifecycle rule for an S3 bucket.
27
28 :ivar id: Unique identifier for the rule. The value cannot be longer
29 than 255 characters. This value is optional. The server will
30 generate a unique value for the rule if no value is provided.
31
32 :ivar prefix: Prefix identifying one or more objects to which the
33 rule applies. If prefix is not provided, Boto generates a default
34 prefix which will match all objects.
35
36 :ivar status: If 'Enabled', the rule is currently being applied.
37 If 'Disabled', the rule is not currently being applied.
38
39 :ivar expiration: An instance of `Expiration`. This indicates
40 the lifetime of the objects that are subject to the rule.
41
42 :ivar transition: An instance of `Transition`. This indicates
43 when to transition to a different storage class.
44
45 """
46 def __init__(self, id=None, prefix=None, status=None, expiration=None,
47 transition=None):
48 self.id = id
49 self.prefix = '' if prefix is None else prefix
50 self.status = status
51 if isinstance(expiration, six.integer_types):
52 # retain backwards compatibility???
53 self.expiration = Expiration(days=expiration)
54 else:
55 # None or object
56 self.expiration = expiration
57
58 # retain backwards compatibility
59 if isinstance(transition, Transition):
60 self.transition = Transitions()
61 self.transition.append(transition)
62 elif transition:
63 self.transition = transition
64 else:
65 self.transition = Transitions()
66
67 def __repr__(self):
68 return '<Rule: %s>' % self.id
69
70 def startElement(self, name, attrs, connection):
71 if name == 'Transition':
72 return self.transition
73 elif name == 'Expiration':
74 self.expiration = Expiration()
75 return self.expiration
76 return None
77
78 def endElement(self, name, value, connection):
79 if name == 'ID':
80 self.id = value
81 elif name == 'Prefix':
82 self.prefix = value
83 elif name == 'Status':
84 self.status = value
85 else:
86 setattr(self, name, value)
87
88 def to_xml(self):
89 s = '<Rule>'
90 if self.id is not None:
91 s += '<ID>%s</ID>' % self.id
92 s += '<Prefix>%s</Prefix>' % self.prefix
93 s += '<Status>%s</Status>' % self.status
94 if self.expiration is not None:
95 s += self.expiration.to_xml()
96 if self.transition is not None:
97 s += self.transition.to_xml()
98 s += '</Rule>'
99 return s
100
101 class Expiration(object):
102 """
103 When an object will expire.
104
105 :ivar days: The number of days until the object expires
106
107 :ivar date: The date when the object will expire. Must be
108 in ISO 8601 format.
109 """
110 def __init__(self, days=None, date=None):
111 self.days = days
112 self.date = date
113
114 def startElement(self, name, attrs, connection):
115 return None
116
117 def endElement(self, name, value, connection):
118 if name == 'Days':
119 self.days = int(value)
120 elif name == 'Date':
121 self.date = value
122
123 def __repr__(self):
124 if self.days is None:
125 how_long = "on: %s" % self.date
126 else:
127 how_long = "in: %s days" % self.days
128 return '<Expiration: %s>' % how_long
129
130 def to_xml(self):
131 s = '<Expiration>'
132 if self.days is not None:
133 s += '<Days>%s</Days>' % self.days
134 elif self.date is not None:
135 s += '<Date>%s</Date>' % self.date
136 s += '</Expiration>'
137 return s
138
139 class Transition(object):
140 """
141 A transition to a different storage class.
142
143 :ivar days: The number of days until the object should be moved.
144
145 :ivar date: The date when the object should be moved. Should be
146 in ISO 8601 format.
147
148 :ivar storage_class: The storage class to transition to. Valid
149 values are GLACIER, STANDARD_IA.
150 """
151 def __init__(self, days=None, date=None, storage_class=None):
152 self.days = days
153 self.date = date
154 self.storage_class = storage_class
155
156 def __repr__(self):
157 if self.days is None:
158 how_long = "on: %s" % self.date
159 else:
160 how_long = "in: %s days" % self.days
161 return '<Transition: %s, %s>' % (how_long, self.storage_class)
162
163 def to_xml(self):
164 s = '<Transition>'
165 s += '<StorageClass>%s</StorageClass>' % self.storage_class
166 if self.days is not None:
167 s += '<Days>%s</Days>' % self.days
168 elif self.date is not None:
169 s += '<Date>%s</Date>' % self.date
170 s += '</Transition>'
171 return s
172
173 class Transitions(list):
174 """
175 A container for the transitions associated with a Lifecycle's Rule configuration.
176 """
177 def __init__(self):
178 self.transition_properties = 3
179 self.current_transition_property = 1
180 self.temp_days = None
181 self.temp_date = None
182 self.temp_storage_class = None
183
184 def startElement(self, name, attrs, connection):
185 return None
186
187 def endElement(self, name, value, connection):
188 if name == 'Days':
189 self.temp_days = int(value)
190 elif name == 'Date':
191 self.temp_date = value
192 elif name == 'StorageClass':
193 self.temp_storage_class = value
194
195 # the XML does not contain a <Transitions> tag
196 # but rather N number of <Transition> tags not
197 # structured in any sort of hierarchy.
198 if self.current_transition_property == self.transition_properties:
199 self.append(Transition(self.temp_days, self.temp_date, self.temp_storage_class))
200 self.temp_days = self.temp_date = self.temp_storage_class = None
201 self.current_transition_property = 1
202 else:
203 self.current_transition_property += 1
204
205 def to_xml(self):
206 """
207 Returns a string containing the XML version of the Lifecycle
208 configuration as defined by S3.
209 """
210 s = ''
211 for transition in self:
212 s += transition.to_xml()
213 return s
214
215 def add_transition(self, days=None, date=None, storage_class=None):
216 """
217 Add a transition to this Lifecycle configuration. This only adds
218 the rule to the local copy. To install the new rule(s) on
219 the bucket, you need to pass this Lifecycle config object
220 to the configure_lifecycle method of the Bucket object.
221
222 :ivar days: The number of days until the object should be moved.
223
224 :ivar date: The date when the object should be moved. Should be
225 in ISO 8601 format.
226
227 :ivar storage_class: The storage class to transition to. Valid
228 values are GLACIER, STANDARD_IA.
229 """
230 transition = Transition(days, date, storage_class)
231 self.append(transition)
232
233 def __first_or_default(self, prop):
234 for transition in self:
235 return getattr(transition, prop)
236 return None
237
238 # maintain backwards compatibility so that we can continue utilizing
239 # 'rule.transition.days' syntax
240 @property
241 def days(self):
242 return self.__first_or_default('days')
243
244 @property
245 def date(self):
246 return self.__first_or_default('date')
247
248 @property
249 def storage_class(self):
250 return self.__first_or_default('storage_class')
251
252
253 class Lifecycle(list):
254 """
255 A container for the rules associated with a Lifecycle configuration.
256 """
257
258 def startElement(self, name, attrs, connection):
259 if name == 'Rule':
260 rule = Rule()
261 self.append(rule)
262 return rule
263 return None
264
265 def endElement(self, name, value, connection):
266 setattr(self, name, value)
267
268 def to_xml(self):
269 """
270 Returns a string containing the XML version of the Lifecycle
271 configuration as defined by S3.
272 """
273 s = '<?xml version="1.0" encoding="UTF-8"?>'
274 s += '<LifecycleConfiguration>'
275 for rule in self:
276 s += rule.to_xml()
277 s += '</LifecycleConfiguration>'
278 return s
279
280 def add_rule(self, id=None, prefix='', status='Enabled',
281 expiration=None, transition=None):
282 """
283 Add a rule to this Lifecycle configuration. This only adds
284 the rule to the local copy. To install the new rule(s) on
285 the bucket, you need to pass this Lifecycle config object
286 to the configure_lifecycle method of the Bucket object.
287
288 :type id: str
289 :param id: Unique identifier for the rule. The value cannot be longer
290 than 255 characters. This value is optional. The server will
291 generate a unique value for the rule if no value is provided.
292
293 :type prefix: str
294 :iparam prefix: Prefix identifying one or more objects to which the
295 rule applies.
296
297 :type status: str
298 :param status: If 'Enabled', the rule is currently being applied.
299 If 'Disabled', the rule is not currently being applied.
300
301 :type expiration: int
302 :param expiration: Indicates the lifetime, in days, of the objects
303 that are subject to the rule. The value must be a non-zero
304 positive integer. A Expiration object instance is also perfect.
305
306 :type transition: Transitions
307 :param transition: Indicates when an object transitions to a
308 different storage class.
309 """
310 rule = Rule(id, prefix, status, expiration, transition)
311 self.append(rule)