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