Index: tools/json_schema_compiler/ordered_dict.py |
diff --git a/tools/json_schema_compiler/ordered_dict.py b/tools/json_schema_compiler/ordered_dict.py |
new file mode 100644 |
index 0000000000000000000000000000000000000000..7d21e758c143d3a99f3d03ae358dd5ceef76ead4 |
--- /dev/null |
+++ b/tools/json_schema_compiler/ordered_dict.py |
@@ -0,0 +1,100 @@ |
+# Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+# Use of this source code is governed by a BSD-style license that can be |
+# found in the LICENSE file. |
+ |
+import operator |
+ |
+class _OrderedDictIterator(object): |
+ def __init__(self, items, func): |
+ self._items = items |
+ self._current = 0 |
+ self._func = func |
+ |
+ def __iter__(self): |
+ return self |
+ |
+ def next(self): |
+ if self._current > len(self._items) - 1: |
+ raise StopIteration |
+ else: |
+ self._current += 1 |
+ return self._func(self._items[self._current - 1]) |
+ |
+class OrderedDict(object): |
+ """This class is used because it makes sense for the documentation to have the |
+ functions, events, types, and properties in the order they were declared in |
+ the schemas. In Python 2.6, there is no OrderedDict class, so this will be |
+ used. |
+ """ |
+ def __init__(self, items=None): |
+ if items is not None: |
+ self._list = [(k, v) for k, v in items] |
+ self._dict = dict(self._list) |
Aaron Boodman
2012/10/24 01:52:22
You should make one of these a pointer into the ot
cduvall
2012/10/26 01:09:57
Done.
|
+ else: |
+ self._list = [] |
+ self._dict = {} |
+ |
+ def __getitem__(self, key): |
+ return self._dict[key] |
+ |
+ def get(self, key, default=None): |
+ if key not in self: |
+ return default |
+ return self[key] |
+ |
+ def __setitem__(self, key, value): |
+ self._dict[key] = value |
+ if key in self: |
Aaron Boodman
2012/10/24 01:52:22
Won't this always be true due to the above line?
cduvall
2012/10/26 01:09:57
Done.
|
+ for i, item in enumerate(self._list): |
+ if item[0] == key: |
+ self._list[i] = (key, value) |
+ return |
+ self._list.append((key, value)) |
+ |
+ def values(self): |
+ return [v for k, v in self._list] |
+ |
+ def keys(self): |
+ return [k for k, v in self._list] |
+ |
+ def iteritems(self): |
+ return _OrderedDictIterator(self._list, lambda x: x) |
+ |
+ def items(self): |
+ return self._list[:] |
+ |
+ def update(self, other): |
+ self._dict.update(other) |
Aaron Boodman
2012/10/24 01:52:22
Seems this is unnecessary?
cduvall
2012/10/26 01:09:57
Done.
|
+ for k, v in other.iteritems(): |
+ self[k] = v |
+ |
+ def __iter__(self): |
+ return _OrderedDictIterator(self._list, operator.itemgetter(0)) |
+ |
+ def __contains__(self, key): |
+ return key in self._dict |
+ |
+ def __repr__(self): |
+ return 'OrderedDict(%s)' % str(self._list) |
+ |
+ def pop(self, item): |
+ del self._dict[item] |
+ for i, k in enumerate(self.keys()): |
+ if k == item: |
+ return self._list.pop(i)[1] |
+ |
+ def __delitem__(self, item): |
+ self.pop(item) |
+ |
+ def __eq__(self, other): |
+ if isinstance(other, dict): |
+ return dict(self._list) == other |
+ elif isinstance(other, OrderedDict): |
+ return self._list == other._list |
+ return False |
+ |
+ def __ne__(self, other): |
+ return not (self == other) |
+ |
+ def __len__(self): |
+ return len(self._list) |