comparison planemo/lib/python3.7/site-packages/boto/ecs/item.py @ 0:d30785e31577 draft

"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
author guerler
date Fri, 31 Jul 2020 00:18:57 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:d30785e31577
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
22
23 import xml.sax
24 import cgi
25 from boto.compat import six, StringIO
26
27 class ResponseGroup(xml.sax.ContentHandler):
28 """A Generic "Response Group", which can
29 be anything from the entire list of Items to
30 specific response elements within an item"""
31
32 def __init__(self, connection=None, nodename=None):
33 """Initialize this Item"""
34 self._connection = connection
35 self._nodename = nodename
36 self._nodepath = []
37 self._curobj = None
38 self._xml = StringIO()
39
40 def __repr__(self):
41 return '<%s: %s>' % (self.__class__.__name__, self.__dict__)
42
43 #
44 # Attribute Functions
45 #
46 def get(self, name):
47 return self.__dict__.get(name)
48
49 def set(self, name, value):
50 self.__dict__[name] = value
51
52 def to_xml(self):
53 return "<%s>%s</%s>" % (self._nodename, self._xml.getvalue(), self._nodename)
54
55 #
56 # XML Parser functions
57 #
58 def startElement(self, name, attrs, connection):
59 self._xml.write("<%s>" % name)
60 self._nodepath.append(name)
61 if len(self._nodepath) == 1:
62 obj = ResponseGroup(self._connection)
63 self.set(name, obj)
64 self._curobj = obj
65 elif self._curobj:
66 self._curobj.startElement(name, attrs, connection)
67 return None
68
69 def endElement(self, name, value, connection):
70 self._xml.write("%s</%s>" % (cgi.escape(value).replace("&amp;amp;", "&amp;"), name))
71 if len(self._nodepath) == 0:
72 return
73 obj = None
74 curval = self.get(name)
75 if len(self._nodepath) == 1:
76 if value or not curval:
77 self.set(name, value)
78 if self._curobj:
79 self._curobj = None
80 #elif len(self._nodepath) == 2:
81 #self._curobj = None
82 elif self._curobj:
83 self._curobj.endElement(name, value, connection)
84 self._nodepath.pop()
85 return None
86
87
88 class Item(ResponseGroup):
89 """A single Item"""
90
91 def __init__(self, connection=None):
92 """Initialize this Item"""
93 ResponseGroup.__init__(self, connection, "Item")
94
95 class ItemSet(ResponseGroup):
96 """A special ResponseGroup that has built-in paging, and
97 only creates new Items on the "Item" tag"""
98
99 def __init__(self, connection, action, params, page=0):
100 ResponseGroup.__init__(self, connection, "Items")
101 self.objs = []
102 self.iter = None
103 self.page = page
104 self.action = action
105 self.params = params
106 self.curItem = None
107 self.total_results = 0
108 self.total_pages = 0
109 self.is_valid = False
110 self.errors = []
111
112 def startElement(self, name, attrs, connection):
113 if name == "Item":
114 self.curItem = Item(self._connection)
115 elif self.curItem is not None:
116 self.curItem.startElement(name, attrs, connection)
117 return None
118
119 def endElement(self, name, value, connection):
120 if name == 'TotalResults':
121 self.total_results = value
122 elif name == 'TotalPages':
123 self.total_pages = value
124 elif name == 'IsValid':
125 if value == 'True':
126 self.is_valid = True
127 elif name == 'Code':
128 self.errors.append({'Code': value, 'Message': None})
129 elif name == 'Message':
130 self.errors[-1]['Message'] = value
131 elif name == 'Item':
132 self.objs.append(self.curItem)
133 self._xml.write(self.curItem.to_xml())
134 self.curItem = None
135 elif self.curItem is not None:
136 self.curItem.endElement(name, value, connection)
137 return None
138
139 def __next__(self):
140 """Special paging functionality"""
141 if self.iter is None:
142 self.iter = iter(self.objs)
143 try:
144 return next(self.iter)
145 except StopIteration:
146 self.iter = None
147 self.objs = []
148 if int(self.page) < int(self.total_pages):
149 self.page += 1
150 self._connection.get_response(self.action, self.params, self.page, self)
151 return next(self)
152 else:
153 raise
154
155 next = __next__
156
157 def __iter__(self):
158 return self
159
160 def to_xml(self):
161 """Override to first fetch everything"""
162 for item in self:
163 pass
164 return ResponseGroup.to_xml(self)