PageRenderTime 32ms CodeModel.GetById 11ms app.highlight 15ms RepoModel.GetById 2ms app.codeStats 0ms

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

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