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

Side by Side Diff: third_party/gsutil/boto/boto/jsonresponse.py

Issue 12317103: Added gsutil to depot tools (Closed) Base URL: https://chromium.googlesource.com/chromium/tools/depot_tools.git@master
Patch Set: 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
OLDNEW
(Empty)
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 import utils
25
26 class XmlHandler(xml.sax.ContentHandler):
27
28 def __init__(self, root_node, connection):
29 self.connection = connection
30 self.nodes = [('root', root_node)]
31 self.current_text = ''
32
33 def startElement(self, name, attrs):
34 self.current_text = ''
35 t = self.nodes[-1][1].startElement(name, attrs, self.connection)
36 if t != None:
37 if isinstance(t, tuple):
38 self.nodes.append(t)
39 else:
40 self.nodes.append((name, t))
41
42 def endElement(self, name):
43 self.nodes[-1][1].endElement(name, self.current_text, self.connection)
44 if self.nodes[-1][0] == name:
45 self.nodes.pop()
46 self.current_text = ''
47
48 def characters(self, content):
49 self.current_text += content
50
51 def parse(self, s):
52 xml.sax.parseString(s, self)
53
54 class Element(dict):
55
56 def __init__(self, connection=None, element_name=None,
57 stack=None, parent=None, list_marker=('Set',),
58 item_marker=('member', 'item'),
59 pythonize_name=False):
60 dict.__init__(self)
61 self.connection = connection
62 self.element_name = element_name
63 self.list_marker = utils.mklist(list_marker)
64 self.item_marker = utils.mklist(item_marker)
65 if stack is None:
66 self.stack = []
67 else:
68 self.stack = stack
69 self.pythonize_name = pythonize_name
70 self.parent = parent
71
72 def __getattr__(self, key):
73 if key in self:
74 return self[key]
75 for k in self:
76 e = self[k]
77 if isinstance(e, Element):
78 try:
79 return getattr(e, key)
80 except AttributeError:
81 pass
82 raise AttributeError
83
84 def get_name(self, name):
85 if self.pythonize_name:
86 name = utils.pythonize_name(name)
87 return name
88
89 def startElement(self, name, attrs, connection):
90 self.stack.append(name)
91 for lm in self.list_marker:
92 if name.endswith(lm):
93 l = ListElement(self.connection, name, self.list_marker,
94 self.item_marker, self.pythonize_name)
95 self[self.get_name(name)] = l
96 return l
97 if len(self.stack) > 0:
98 element_name = self.stack[-1]
99 e = Element(self.connection, element_name, self.stack, self,
100 self.list_marker, self.item_marker,
101 self.pythonize_name)
102 self[self.get_name(element_name)] = e
103 return (element_name, e)
104 else:
105 return None
106
107 def endElement(self, name, value, connection):
108 if len(self.stack) > 0:
109 self.stack.pop()
110 value = value.strip()
111 if value:
112 if isinstance(self.parent, Element):
113 self.parent[self.get_name(name)] = value
114 elif isinstance(self.parent, ListElement):
115 self.parent.append(value)
116
117 class ListElement(list):
118
119 def __init__(self, connection=None, element_name=None,
120 list_marker=['Set'], item_marker=('member', 'item'),
121 pythonize_name=False):
122 list.__init__(self)
123 self.connection = connection
124 self.element_name = element_name
125 self.list_marker = list_marker
126 self.item_marker = item_marker
127 self.pythonize_name = pythonize_name
128
129 def get_name(self, name):
130 if self.pythonize_name:
131 name = utils.pythonize_name(name)
132 return name
133
134 def startElement(self, name, attrs, connection):
135 for lm in self.list_marker:
136 if name.endswith(lm):
137 l = ListElement(self.connection, name,
138 self.list_marker, self.item_marker,
139 self.pythonize_name)
140 setattr(self, self.get_name(name), l)
141 return l
142 if name in self.item_marker:
143 e = Element(self.connection, name, parent=self,
144 list_marker=self.list_marker,
145 item_marker=self.item_marker,
146 pythonize_name=self.pythonize_name)
147 self.append(e)
148 return e
149 else:
150 return None
151
152 def endElement(self, name, value, connection):
153 if name == self.element_name:
154 if len(self) > 0:
155 empty = []
156 for e in self:
157 if isinstance(e, Element):
158 if len(e) == 0:
159 empty.append(e)
160 for e in empty:
161 self.remove(e)
162 else:
163 setattr(self, self.get_name(name), value)
OLDNEW
« no previous file with comments | « third_party/gsutil/boto/boto/https_connection.py ('k') | third_party/gsutil/boto/boto/manage/__init__.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698