Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(341)

Side by Side Diff: third_party/boto/core/dictresponse.py

Issue 12633019: Added boto/ to depot_tools/third_party (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/tools/depot_tools
Patch Set: Moved boto down by one Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « third_party/boto/core/credentials.py ('k') | third_party/boto/core/service.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 # Copyright (c) 2012 Mitch Garnaat http://garnaat.org/
2 # Copyright (c) 2012 Amazon.com, Inc. or its affiliates.
3 # All Rights Reserved
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining a
6 # copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish, dis-
9 # tribute, sublicense, and/or sell copies of the Software, and to permit
10 # persons to whom the Software is furnished to do so, subject to the fol-
11 # lowing conditions:
12 #
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
18 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
19 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 # IN THE SOFTWARE.
23 #
24
25 import xml.sax
26
27
28 def pythonize_name(name, sep='_'):
29 s = ''
30 if name[0].isupper:
31 s = name[0].lower()
32 for c in name[1:]:
33 if c.isupper():
34 s += sep + c.lower()
35 else:
36 s += c
37 return s
38
39
40 class XmlHandler(xml.sax.ContentHandler):
41
42 def __init__(self, root_node, connection):
43 self.connection = connection
44 self.nodes = [('root', root_node)]
45 self.current_text = ''
46
47 def startElement(self, name, attrs):
48 self.current_text = ''
49 t = self.nodes[-1][1].startElement(name, attrs, self.connection)
50 if t != None:
51 if isinstance(t, tuple):
52 self.nodes.append(t)
53 else:
54 self.nodes.append((name, t))
55
56 def endElement(self, name):
57 self.nodes[-1][1].endElement(name, self.current_text, self.connection)
58 if self.nodes[-1][0] == name:
59 self.nodes.pop()
60 self.current_text = ''
61
62 def characters(self, content):
63 self.current_text += content
64
65 def parse(self, s):
66 xml.sax.parseString(s, self)
67
68
69 class Element(dict):
70
71 def __init__(self, connection=None, element_name=None,
72 stack=None, parent=None, list_marker=None,
73 item_marker=None, pythonize_name=False):
74 dict.__init__(self)
75 self.connection = connection
76 self.element_name = element_name
77 self.list_marker = list_marker or ['Set']
78 self.item_marker = item_marker or ['member', 'item']
79 if stack is None:
80 self.stack = []
81 else:
82 self.stack = stack
83 self.pythonize_name = pythonize_name
84 self.parent = parent
85
86 def __getattr__(self, key):
87 if key in self:
88 return self[key]
89 for k in self:
90 e = self[k]
91 if isinstance(e, Element):
92 try:
93 return getattr(e, key)
94 except AttributeError:
95 pass
96 raise AttributeError
97
98 def get_name(self, name):
99 if self.pythonize_name:
100 name = pythonize_name(name)
101 return name
102
103 def startElement(self, name, attrs, connection):
104 self.stack.append(name)
105 for lm in self.list_marker:
106 if name.endswith(lm):
107 l = ListElement(self.connection, name, self.list_marker,
108 self.item_marker, self.pythonize_name)
109 self[self.get_name(name)] = l
110 return l
111 if len(self.stack) > 0:
112 element_name = self.stack[-1]
113 e = Element(self.connection, element_name, self.stack, self,
114 self.list_marker, self.item_marker,
115 self.pythonize_name)
116 self[self.get_name(element_name)] = e
117 return (element_name, e)
118 else:
119 return None
120
121 def endElement(self, name, value, connection):
122 if len(self.stack) > 0:
123 self.stack.pop()
124 value = value.strip()
125 if value:
126 if isinstance(self.parent, Element):
127 self.parent[self.get_name(name)] = value
128 elif isinstance(self.parent, ListElement):
129 self.parent.append(value)
130
131
132 class ListElement(list):
133
134 def __init__(self, connection=None, element_name=None,
135 list_marker=['Set'], item_marker=('member', 'item'),
136 pythonize_name=False):
137 list.__init__(self)
138 self.connection = connection
139 self.element_name = element_name
140 self.list_marker = list_marker
141 self.item_marker = item_marker
142 self.pythonize_name = pythonize_name
143
144 def get_name(self, name):
145 if self.pythonize_name:
146 name = utils.pythonize_name(name)
147 return name
148
149 def startElement(self, name, attrs, connection):
150 for lm in self.list_marker:
151 if name.endswith(lm):
152 l = ListElement(self.connection, name,
153 self.list_marker, self.item_marker,
154 self.pythonize_name)
155 setattr(self, self.get_name(name), l)
156 return l
157 if name in self.item_marker:
158 e = Element(self.connection, name, parent=self,
159 list_marker=self.list_marker,
160 item_marker=self.item_marker,
161 pythonize_name=self.pythonize_name)
162 self.append(e)
163 return e
164 else:
165 return None
166
167 def endElement(self, name, value, connection):
168 if name == self.element_name:
169 if len(self) > 0:
170 empty = []
171 for e in self:
172 if isinstance(e, Element):
173 if len(e) == 0:
174 empty.append(e)
175 for e in empty:
176 self.remove(e)
177 else:
178 setattr(self, self.get_name(name), value)
OLDNEW
« no previous file with comments | « third_party/boto/core/credentials.py ('k') | third_party/boto/core/service.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698