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

Side by Side Diff: swarm_client/third_party/chromium/python26_polyfill.py

Issue 69143004: Delete swarm_client. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/tools/
Patch Set: Created 7 years, 1 month 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
OLDNEW
(Empty)
1 # Copyright 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 """A collection of backports to make recipes+tests work on python 2.6."""
6
7 import collections
8 if not hasattr(collections, 'OrderedDict'):
9 # from:
10 # http://code.activestate.com/recipes/576693-ordered-dictionary-for-py24/
11 # Backport of OrderedDict() class that runs on Python 2.4, 2.5, 2.6, 2.7 and
12 # pypy. Passes Python2.7's test suite and incorporates all the latest updates.
13
14 try:
15 from thread import get_ident as _get_ident
16 except ImportError:
17 from dummy_thread import get_ident as _get_ident
18
19 try:
20 from _abcoll import KeysView, ValuesView, ItemsView
21 except ImportError:
22 pass
23
24
25 class OrderedDict(dict):
26 'Dictionary that remembers insertion order'
27 # An inherited dict maps keys to values.
28 # The inherited dict provides __getitem__, __len__, __contains__, and get.
29 # The remaining methods are order-aware.
30 # Big-O running times for all methods are the same as for regular
31 # dictionaries.
32
33 # The internal self.__map dictionary maps keys to links in a doubly linked
34 # list.
35 # The circular doubly linked list starts and ends with a sentinel element.
36 # The sentinel element never gets deleted (this simplifies the algorithm).
37 # Each link is stored as a list of length three: [PREV, NEXT, KEY].
38
39 def __init__(self, *args, **kwds):
40 '''Initialize an ordered dictionary. Signature is the same as for
41 regular dictionaries, but keyword arguments are not recommended
42 because their insertion order is arbitrary.
43
44 '''
45 # pylint: disable=W0231
46 if len(args) > 1:
47 raise TypeError('expected at most 1 arguments, got %d' % len(args))
48 try:
49 self.__root
50 except AttributeError:
51 self.__root = root = [] # sentinel node
52 root[:] = [root, root, None]
53 self.__map = {}
54 self.__update(*args, **kwds)
55
56 def __setitem__(self, key, value, dict_setitem=dict.__setitem__):
57 'od.__setitem__(i, y) <==> od[i]=y'
58 # Setting a new item creates a new link which goes at the end of the
59 # linked list, and the inherited dictionary is updated with the new
60 # key/value pair.
61 if key not in self:
62 root = self.__root
63 last = root[0]
64 last[1] = root[0] = self.__map[key] = [last, root, key]
65 dict_setitem(self, key, value)
66
67 def __delitem__(self, key, dict_delitem=dict.__delitem__):
68 'od.__delitem__(y) <==> del od[y]'
69 # Deleting an existing item uses self.__map to find the link which is
70 # then removed by updating the links in the predecessor and successor
71 # nodes.
72 dict_delitem(self, key)
73 link_prev, link_next, key = self.__map.pop(key)
74 link_prev[1] = link_next
75 link_next[0] = link_prev
76
77 def __iter__(self):
78 'od.__iter__() <==> iter(od)'
79 root = self.__root
80 curr = root[1]
81 while curr is not root:
82 yield curr[2]
83 curr = curr[1]
84
85 def __reversed__(self):
86 'od.__reversed__() <==> reversed(od)'
87 root = self.__root
88 curr = root[0]
89 while curr is not root:
90 yield curr[2]
91 curr = curr[0]
92
93 def clear(self):
94 'od.clear() -> None. Remove all items from od.'
95 try:
96 for node in self.__map.itervalues():
97 del node[:]
98 root = self.__root
99 root[:] = [root, root, None]
100 self.__map.clear()
101 except AttributeError:
102 pass
103 dict.clear(self)
104
105 def popitem(self, last=True):
106 '''od.popitem() -> (k, v), return and remove a (key, value) pair.
107 Pairs are returned in LIFO order if last is true or FIFO order if false.
108
109 '''
110 if not self:
111 raise KeyError('dictionary is empty')
112 root = self.__root
113 if last:
114 link = root[0]
115 link_prev = link[0]
116 link_prev[1] = root
117 root[0] = link_prev
118 else:
119 link = root[1]
120 link_next = link[1]
121 root[1] = link_next
122 link_next[0] = root
123 key = link[2]
124 del self.__map[key]
125 value = dict.pop(self, key)
126 return key, value
127
128 # -- the following methods do not depend on the internal structure --
129
130 def keys(self):
131 'od.keys() -> list of keys in od'
132 return list(self)
133
134 def values(self):
135 'od.values() -> list of values in od'
136 return [self[key] for key in self]
137
138 def items(self):
139 'od.items() -> list of (key, value) pairs in od'
140 return [(key, self[key]) for key in self]
141
142 def iterkeys(self):
143 'od.iterkeys() -> an iterator over the keys in od'
144 return iter(self)
145
146 def itervalues(self):
147 'od.itervalues -> an iterator over the values in od'
148 for k in self:
149 yield self[k]
150
151 def iteritems(self):
152 'od.iteritems -> an iterator over the (key, value) items in od'
153 for k in self:
154 yield (k, self[k])
155
156 def update(*args, **kwds): # pylint: disable=E0211
157 '''od.update(E, **F) -> None. Update od from dict/iterable E and F.
158
159 If E is a dict instance, does: for k in E: od[k] = E[k]
160 If E has a .keys() method, does: for k in E.keys(): od[k] = E[k]
161 Or if E is an iterable of items, does: for k, v in E: od[k] = v
162 In either case, this is followed by: for k, v in F.items(): od[k] = v
163
164 '''
165 if len(args) > 2:
166 raise TypeError('update() takes at most 2 positional '
167 'arguments (%d given)' % (len(args),))
168 elif not args:
169 raise TypeError('update() takes at least 1 argument (0 given)')
170 self = args[0]
171 # Make progressively weaker assumptions about "other"
172 other = ()
173 if len(args) == 2:
174 other = args[1]
175 if isinstance(other, dict):
176 for key in other:
177 self[key] = other[key]
178 elif hasattr(other, 'keys'):
179 for key in other.keys():
180 self[key] = other[key]
181 else:
182 for key, value in other:
183 self[key] = value
184 for key, value in kwds.items():
185 self[key] = value
186
187 # let subclasses override update without breaking __init__
188 __update = update
189
190 __marker = object()
191
192 def pop(self, key, default=__marker):
193 '''od.pop(k[,d]) -> v, remove specified key and return the corresponding
194 value. If key is not found, d is returned if given, otherwise KeyError is
195 raised.
196 '''
197 if key in self:
198 result = self[key]
199 del self[key]
200 return result
201 if default is self.__marker:
202 raise KeyError(key)
203 return default
204
205 def setdefault(self, key, default=None):
206 'od.setdefault(k[,d]) -> od.get(k,d), also set od[k]=d if k not in od'
207 if key in self:
208 return self[key]
209 self[key] = default
210 return default
211
212 def __repr__(self, _repr_running={}): # pylint: disable=W0102
213 'od.__repr__() <==> repr(od)'
214 call_key = id(self), _get_ident()
215 if call_key in _repr_running:
216 return '...'
217 _repr_running[call_key] = 1
218 try:
219 if not self:
220 return '%s()' % (self.__class__.__name__,)
221 return '%s(%r)' % (self.__class__.__name__, self.items())
222 finally:
223 del _repr_running[call_key]
224
225 def __reduce__(self):
226 'Return state information for pickling'
227 items = [[k, self[k]] for k in self]
228 inst_dict = vars(self).copy()
229 for k in vars(OrderedDict()):
230 inst_dict.pop(k, None)
231 if inst_dict:
232 return (self.__class__, (items,), inst_dict)
233 return self.__class__, (items,)
234
235 def copy(self):
236 'od.copy() -> a shallow copy of od'
237 return self.__class__(self)
238
239 @classmethod
240 def fromkeys(cls, iterable, value=None):
241 '''OD.fromkeys(S[, v]) -> New ordered dictionary with keys from S
242 and values equal to v (which defaults to None).
243
244 '''
245 d = cls()
246 for key in iterable:
247 d[key] = value
248 return d
249
250 def __eq__(self, other):
251 '''od.__eq__(y) <==> od==y. Comparison to another OD is order-sensitive
252 while comparison to a regular mapping is order-insensitive.
253
254 '''
255 if isinstance(other, OrderedDict):
256 return len(self)==len(other) and self.items() == other.items()
257 return dict.__eq__(self, other)
258
259 def __ne__(self, other):
260 return not self == other
261
262 # -- the following methods are only used in Python 2.7 --
263
264 def viewkeys(self):
265 "od.viewkeys() -> a set-like object providing a view on od's keys"
266 return KeysView(self)
267
268 def viewvalues(self):
269 "od.viewvalues() -> an object providing a view on od's values"
270 return ValuesView(self)
271
272 def viewitems(self):
273 "od.viewitems() -> a set-like object providing a view on od's items"
274 return ItemsView(self)
275
276 collections.OrderedDict = OrderedDict
277
278
279 import sys
280 if sys.version_info < (2, 7):
281 import unittest
282 class SupportTestLoaderProtocol(unittest.TestLoader):
283 def loadTestsFromModule(self, module, *_ignore_27_args):
284 tests = super(SupportTestLoaderProtocol, self).loadTestsFromModule(module)
285 load_tests = getattr(module, 'load_tests', None)
286 if load_tests is not None:
287 return load_tests(self, tests, None)
288 return tests
289 unittest.defaultTestLoader = SupportTestLoaderProtocol()
290 old_defaults = unittest.main.__init__.im_func.func_defaults
291 # Default arguments bind at declaration-time, and main binds to the old
292 # defaultTestLoader instance. Apply hacks.
293 unittest.main.__init__.im_func.func_defaults = tuple(
294 (unittest.defaultTestLoader if isinstance(x, unittest.TestLoader) else x)
295 for x in old_defaults)
OLDNEW
« no previous file with comments | « swarm_client/third_party/chromium/README.swarming ('k') | swarm_client/third_party/colorama/LICENSE.txt » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698