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