PageRenderTime 19ms CodeModel.GetById 12ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/boto-2.5.2/boto/cloudfront/invalidation.py

#
Python | 97 lines | 71 code | 3 blank | 23 comment | 0 complexity | b6afe3d3da95a37e53200f66124e095b MD5 | raw file
 1# Copyright (c) 2006-2010 Chris Moyer http://coredumped.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
22import uuid
23import urllib
24
25class InvalidationBatch(object):
26    """A simple invalidation request.
27        :see: http://docs.amazonwebservices.com/AmazonCloudFront/2010-08-01/APIReference/index.html?InvalidationBatchDatatype.html
28    """
29
30    def __init__(self, paths=None, connection=None, distribution=None, caller_reference=''):
31        """Create a new invalidation request:
32            :paths: An array of paths to invalidate
33        """
34        self.paths = paths or []
35        self.distribution = distribution
36        self.caller_reference = caller_reference
37        if not self.caller_reference:
38            self.caller_reference = str(uuid.uuid4())
39
40        # If we passed in a distribution,
41        # then we use that as the connection object
42        if distribution:
43            self.connection = distribution
44        else:
45            self.connection = connection
46
47    def add(self, path):
48        """Add another path to this invalidation request"""
49        return self.paths.append(path)
50
51    def remove(self, path):
52        """Remove a path from this invalidation request"""
53        return self.paths.remove(path)
54
55    def __iter__(self):
56        return iter(self.paths)
57
58    def __getitem__(self, i):
59        return self.paths[i]
60
61    def __setitem__(self, k, v):
62        self.paths[k] = v
63
64    def escape(self, p):
65        """Escape a path, make sure it begins with a slash and contains no invalid characters"""
66        if not p[0] == "/":
67            p = "/%s" % p
68        return urllib.quote(p)
69
70    def to_xml(self):
71        """Get this batch as XML"""
72        assert self.connection != None
73        s = '<?xml version="1.0" encoding="UTF-8"?>\n'
74        s += '<InvalidationBatch xmlns="http://cloudfront.amazonaws.com/doc/%s/">\n' % self.connection.Version
75        for p in self.paths:
76            s += '    <Path>%s</Path>\n' % self.escape(p)
77        s += '    <CallerReference>%s</CallerReference>\n' % self.caller_reference
78        s += '</InvalidationBatch>\n'
79        return s
80
81    def startElement(self, name, attrs, connection):
82        if name == "InvalidationBatch":
83            self.paths = []
84        return None
85
86    def endElement(self, name, value, connection):
87        if name == 'Path':
88            self.paths.append(value)
89        elif name == "Status":
90            self.status = value
91        elif name == "Id":
92            self.id = value
93        elif name == "CreateTime":
94            self.create_time = value
95        elif name == "CallerReference":
96            self.caller_reference = value
97        return None