PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/boto-2.5.2/boto/ecs/item.py

#
Python | 153 lines | 130 code | 3 blank | 20 comment | 0 complexity | e825992e527903ed6882146ba070a84f MD5 | raw file
  1. # Copyright (c) 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 xml.sax
  22. import cgi
  23. from StringIO import StringIO
  24. class ResponseGroup(xml.sax.ContentHandler):
  25. """A Generic "Response Group", which can
  26. be anything from the entire list of Items to
  27. specific response elements within an item"""
  28. def __init__(self, connection=None, nodename=None):
  29. """Initialize this Item"""
  30. self._connection = connection
  31. self._nodename = nodename
  32. self._nodepath = []
  33. self._curobj = None
  34. self._xml = StringIO()
  35. def __repr__(self):
  36. return '<%s: %s>' % (self.__class__.__name__, self.__dict__)
  37. #
  38. # Attribute Functions
  39. #
  40. def get(self, name):
  41. return self.__dict__.get(name)
  42. def set(self, name, value):
  43. self.__dict__[name] = value
  44. def to_xml(self):
  45. return "<%s>%s</%s>" % (self._nodename, self._xml.getvalue(), self._nodename)
  46. #
  47. # XML Parser functions
  48. #
  49. def startElement(self, name, attrs, connection):
  50. self._xml.write("<%s>" % name)
  51. self._nodepath.append(name)
  52. if len(self._nodepath) == 1:
  53. obj = ResponseGroup(self._connection)
  54. self.set(name, obj)
  55. self._curobj = obj
  56. elif self._curobj:
  57. self._curobj.startElement(name, attrs, connection)
  58. return None
  59. def endElement(self, name, value, connection):
  60. self._xml.write("%s</%s>" % (cgi.escape(value).replace("&amp;amp;", "&amp;"), name))
  61. if len(self._nodepath) == 0:
  62. return
  63. obj = None
  64. curval = self.get(name)
  65. if len(self._nodepath) == 1:
  66. if value or not curval:
  67. self.set(name, value)
  68. if self._curobj:
  69. self._curobj = None
  70. #elif len(self._nodepath) == 2:
  71. #self._curobj = None
  72. elif self._curobj:
  73. self._curobj.endElement(name, value, connection)
  74. self._nodepath.pop()
  75. return None
  76. class Item(ResponseGroup):
  77. """A single Item"""
  78. def __init__(self, connection=None):
  79. """Initialize this Item"""
  80. ResponseGroup.__init__(self, connection, "Item")
  81. class ItemSet(ResponseGroup):
  82. """A special ResponseGroup that has built-in paging, and
  83. only creates new Items on the "Item" tag"""
  84. def __init__(self, connection, action, params, page=0):
  85. ResponseGroup.__init__(self, connection, "Items")
  86. self.objs = []
  87. self.iter = None
  88. self.page = page
  89. self.action = action
  90. self.params = params
  91. self.curItem = None
  92. self.total_results = 0
  93. self.total_pages = 0
  94. def startElement(self, name, attrs, connection):
  95. if name == "Item":
  96. self.curItem = Item(self._connection)
  97. elif self.curItem != None:
  98. self.curItem.startElement(name, attrs, connection)
  99. return None
  100. def endElement(self, name, value, connection):
  101. if name == 'TotalResults':
  102. self.total_results = value
  103. elif name == 'TotalPages':
  104. self.total_pages = value
  105. elif name == "Item":
  106. self.objs.append(self.curItem)
  107. self._xml.write(self.curItem.to_xml())
  108. self.curItem = None
  109. elif self.curItem != None:
  110. self.curItem.endElement(name, value, connection)
  111. return None
  112. def next(self):
  113. """Special paging functionality"""
  114. if self.iter == None:
  115. self.iter = iter(self.objs)
  116. try:
  117. return self.iter.next()
  118. except StopIteration:
  119. self.iter = None
  120. self.objs = []
  121. if int(self.page) < int(self.total_pages):
  122. self.page += 1
  123. self._connection.get_response(self.action, self.params, self.page, self)
  124. return self.next()
  125. else:
  126. raise
  127. def __iter__(self):
  128. return self
  129. def to_xml(self):
  130. """Override to first fetch everything"""
  131. for item in self:
  132. pass
  133. return ResponseGroup.to_xml(self)