comparison env/lib/python3.9/site-packages/boto/jsonresponse.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 # Copyright (c) 2010 Mitch Garnaat http://garnaat.org/
2 # Copyright (c) 2010, Eucalyptus Systems, Inc.
3 #
4 # Permission is hereby granted, free of charge, to any person obtaining a
5 # copy of this software and associated documentation files (the
6 # "Software"), to deal in the Software without restriction, including
7 # without limitation the rights to use, copy, modify, merge, publish, dis-
8 # tribute, sublicense, and/or sell copies of the Software, and to permit
9 # persons to whom the Software is furnished to do so, subject to the fol-
10 # lowing conditions:
11 #
12 # The above copyright notice and this permission notice shall be included
13 # in all copies or substantial portions of the Software.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
17 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
18 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 # IN THE SOFTWARE.
22
23 import xml.sax
24 from boto import utils
25
26
27 class XmlHandler(xml.sax.ContentHandler):
28
29 def __init__(self, root_node, connection):
30 self.connection = connection
31 self.nodes = [('root', root_node)]
32 self.current_text = ''
33
34 def startElement(self, name, attrs):
35 self.current_text = ''
36 t = self.nodes[-1][1].startElement(name, attrs, self.connection)
37 if t is not None:
38 if isinstance(t, tuple):
39 self.nodes.append(t)
40 else:
41 self.nodes.append((name, t))
42
43 def endElement(self, name):
44 self.nodes[-1][1].endElement(name, self.current_text, self.connection)
45 if self.nodes[-1][0] == name:
46 self.nodes.pop()
47 self.current_text = ''
48
49 def characters(self, content):
50 self.current_text += content
51
52 def parse(self, s):
53 if not isinstance(s, bytes):
54 s = s.encode('utf-8')
55 xml.sax.parseString(s, self)
56
57
58 class Element(dict):
59
60 def __init__(self, connection=None, element_name=None,
61 stack=None, parent=None, list_marker=('Set',),
62 item_marker=('member', 'item'),
63 pythonize_name=False):
64 dict.__init__(self)
65 self.connection = connection
66 self.element_name = element_name
67 self.list_marker = utils.mklist(list_marker)
68 self.item_marker = utils.mklist(item_marker)
69 if stack is None:
70 self.stack = []
71 else:
72 self.stack = stack
73 self.pythonize_name = pythonize_name
74 self.parent = parent
75
76 def __getattr__(self, key):
77 if key in self:
78 return self[key]
79 for k in self:
80 e = self[k]
81 if isinstance(e, Element):
82 try:
83 return getattr(e, key)
84 except AttributeError:
85 pass
86 raise AttributeError
87
88 def get_name(self, name):
89 if self.pythonize_name:
90 name = utils.pythonize_name(name)
91 return name
92
93 def startElement(self, name, attrs, connection):
94 self.stack.append(name)
95 for lm in self.list_marker:
96 if name.endswith(lm):
97 l = ListElement(self.connection, name, self.list_marker,
98 self.item_marker, self.pythonize_name)
99 self[self.get_name(name)] = l
100 return l
101 if len(self.stack) > 0:
102 element_name = self.stack[-1]
103 e = Element(self.connection, element_name, self.stack, self,
104 self.list_marker, self.item_marker,
105 self.pythonize_name)
106 self[self.get_name(element_name)] = e
107 return (element_name, e)
108 else:
109 return None
110
111 def endElement(self, name, value, connection):
112 if len(self.stack) > 0:
113 self.stack.pop()
114 value = value.strip()
115 if value:
116 if isinstance(self.parent, Element):
117 self.parent[self.get_name(name)] = value
118 elif isinstance(self.parent, ListElement):
119 self.parent.append(value)
120
121
122 class ListElement(list):
123
124 def __init__(self, connection=None, element_name=None,
125 list_marker=['Set'], item_marker=('member', 'item'),
126 pythonize_name=False):
127 list.__init__(self)
128 self.connection = connection
129 self.element_name = element_name
130 self.list_marker = list_marker
131 self.item_marker = item_marker
132 self.pythonize_name = pythonize_name
133
134 def get_name(self, name):
135 if self.pythonize_name:
136 name = utils.pythonize_name(name)
137 return name
138
139 def startElement(self, name, attrs, connection):
140 for lm in self.list_marker:
141 if name.endswith(lm):
142 l = ListElement(self.connection, name,
143 self.list_marker, self.item_marker,
144 self.pythonize_name)
145 setattr(self, self.get_name(name), l)
146 return l
147 if name in self.item_marker:
148 e = Element(self.connection, name, parent=self,
149 list_marker=self.list_marker,
150 item_marker=self.item_marker,
151 pythonize_name=self.pythonize_name)
152 self.append(e)
153 return e
154 else:
155 return None
156
157 def endElement(self, name, value, connection):
158 if name == self.element_name:
159 if len(self) > 0:
160 empty = []
161 for e in self:
162 if isinstance(e, Element):
163 if len(e) == 0:
164 empty.append(e)
165 for e in empty:
166 self.remove(e)
167 else:
168 setattr(self, self.get_name(name), value)