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

Unified Diff: tools/json_schema_compiler/json_parse.py

Issue 11079010: Extensions Docs Server: Preserve JSON declaration order in extensions documentation (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fixes Created 8 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 side-by-side diff with in-line comments
Download patch
Index: tools/json_schema_compiler/json_parse.py
diff --git a/tools/json_schema_compiler/json_parse.py b/tools/json_schema_compiler/json_parse.py
new file mode 100644
index 0000000000000000000000000000000000000000..a31ac4aaa21852e01340520b94fb94259dd46ca1
--- /dev/null
+++ b/tools/json_schema_compiler/json_parse.py
@@ -0,0 +1,139 @@
+# 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 json
+import logging
+import operator
+import os
+import sys
+
+SYS_PATH = sys.path[:]
+try:
+ _FILE_PATH = os.path.dirname(os.path.realpath(__file__))
+ _SIMPLE_JSON_PATH = os.path.join(_FILE_PATH,
+ os.pardir,
+ os.pardir,
+ 'simplejson')
+ sys.path.insert(0, _SIMPLE_JSON_PATH)
+ import simplejson
+ _COMMENT_EATER_PATH = os.path.join(_FILE_PATH, os.pardir)
+ sys.path.insert(0, _COMMENT_EATER_PATH)
+ import json_comment_eater
+finally:
+ sys.path = SYS_PATH
+
+try:
+ from collections import OrderedDict
+
+ # Successfully imported, so we're running Python >= 2.7, and json.loads
+ # supports object_pairs_hook.
+ def Parse(json_str):
+ return json.loads(json_comment_eater.Nom(json_str),
+ object_pairs_hook=OrderedDict)
+
+except ImportError:
+ # Failed to import, so we're running Python < 2.7, and json.loads doesn't
+ # support object_pairs_hook. simplejson however does, but it's slow. We also
+ # need to provide our own OrderedDict implementation.
+ logging.warning('Using simplejson to parse, this might be slow! Upgrade to '
+ 'Python 2.7.')
+
+ class _OrderedDictIterator(object):
+ def __init__(self, keys, dict_, func):
+ self._keys = keys
+ self._dict = dict_
+ self._current = 0
+ self._func = func
+
+ def __iter__(self):
+ return self
+
+ def next(self):
+ if self._current > len(self._keys) - 1:
+ raise StopIteration
+ else:
+ self._current += 1
+ key = self._keys[self._current - 1]
+ return self._func((key, self._dict[key]))
+
+ 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._keys = [i[0] for i in items]
+ self._dict = dict(items)
+ else:
+ self._keys = []
+ 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 not in self._keys:
+ self._keys.append(key)
+
+ def values(self):
+ return [self._dict[k] for k in self._keys]
+
+ def keys(self):
+ return self._keys
+
+ def iteritems(self):
+ return _OrderedDictIterator(self._keys, self._dict, lambda x: x)
+
+ def items(self):
+ return [(k, self._dict[k]) for k in self._keys]
+
+ def update(self, other):
+ for k, v in other.iteritems():
+ self[k] = v
+
+ def __iter__(self):
+ return _OrderedDictIterator(self._keys,
+ self._dict,
+ operator.itemgetter(0))
+
+ def __contains__(self, key):
+ return key in self._keys
+
+ def __repr__(self):
+ return 'OrderedDict(%s)' % str(self.items())
+
+ def pop(self, item):
+ self._keys.remove(item)
+ return self._dict.pop(item)
+
+ def __delitem__(self, item):
+ self.pop(item)
+
+ def __eq__(self, other):
+ if isinstance(other, dict):
+ return self._dict == other
+ elif isinstance(other, OrderedDict):
+ return self._keys == other._keys and self._dict == other._dict
+ return False
+
+ def __ne__(self, other):
+ return not (self == other)
+
+ def __len__(self):
+ return len(self._keys)
+
+ def Parse(json_str):
+ return simplejson.loads(json_comment_eater.Nom(json_str),
+ object_pairs_hook=OrderedDict)
+
+def IsDict(item):
+ return isinstance(item, (dict, OrderedDict))

Powered by Google App Engine
This is Rietveld 408576698