diff options
author | asargent@chromium.org <asargent@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-03-24 23:40:55 +0000 |
---|---|---|
committer | asargent@chromium.org <asargent@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-03-24 23:40:55 +0000 |
commit | 9cbda7ad7de97451a31ed2c95b1b2b97dedd568b (patch) | |
tree | face7d6e7375ce55c3847b297cfec1282c9b7ac3 /tools/json_schema_compiler | |
parent | a8cafbc5e71459e96edf442a6093469d652c4ca1 (diff) | |
download | chromium_src-9cbda7ad7de97451a31ed2c95b1b2b97dedd568b.zip chromium_src-9cbda7ad7de97451a31ed2c95b1b2b97dedd568b.tar.gz chromium_src-9cbda7ad7de97451a31ed2c95b1b2b97dedd568b.tar.bz2 |
Fix two broken json_schema_compiler python unit tests.
These were broken by some of our recent changes, thus highlighting the need
to get them running in an automated fashion. For now, I've fixed them and
done a couple of performance tweaks to speed them up dramatically (30s -> 0.1s).
BEFORE
------
cpp_type_generator_test.py
real 0m29.787s
model_test.py
real 0m27.287s
AFTER
-----
cpp_type_generator_test.py
real 0m0.151s
model_test.py
real 0m0.117s
BUG=none
TEST=none
Review URL: http://codereview.chromium.org/9834063
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@128793 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'tools/json_schema_compiler')
-rw-r--r-- | tools/json_schema_compiler/cpp_type_generator_test.py | 8 | ||||
-rw-r--r-- | tools/json_schema_compiler/json_schema.py | 67 | ||||
-rw-r--r-- | tools/json_schema_compiler/model_test.py | 8 |
3 files changed, 65 insertions, 18 deletions
diff --git a/tools/json_schema_compiler/cpp_type_generator_test.py b/tools/json_schema_compiler/cpp_type_generator_test.py index e58dbac..ca40815 100644 --- a/tools/json_schema_compiler/cpp_type_generator_test.py +++ b/tools/json_schema_compiler/cpp_type_generator_test.py @@ -3,22 +3,22 @@ # found in the LICENSE file. from cpp_type_generator import CppTypeGenerator -from json_schema import LoadJSON +from json_schema import CachedLoad import model import unittest class CppTypeGeneratorTest(unittest.TestCase): def setUp(self): self.model = model.Model() - self.permissions_json = LoadJSON('test/permissions.json') + self.permissions_json = CachedLoad('test/permissions.json') self.model.AddNamespace(self.permissions_json[0], 'path/to/permissions.json') self.permissions = self.model.namespaces.get('permissions') - self.windows_json = LoadJSON('test/windows.json') + self.windows_json = CachedLoad('test/windows.json') self.model.AddNamespace(self.windows_json[0], 'path/to/window.json') self.windows = self.model.namespaces.get('windows') - self.tabs_json = LoadJSON('test/tabs.json') + self.tabs_json = CachedLoad('test/tabs.json') self.model.AddNamespace(self.tabs_json[0], 'path/to/tabs.json') self.tabs = self.model.namespaces.get('tabs') diff --git a/tools/json_schema_compiler/json_schema.py b/tools/json_schema_compiler/json_schema.py index 779ec87..46ca487 100644 --- a/tools/json_schema_compiler/json_schema.py +++ b/tools/json_schema_compiler/json_schema.py @@ -2,18 +2,65 @@ # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. +import copy import json -import os.path -import sys -# We need to get json_minify from the third_party directory. -# This is similar to what is done in chrome/common/extensions/docs/build.py -third_party_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), - os.pardir, os.pardir, 'third_party/') -if third_party_path not in sys.path: - sys.path.insert(0, third_party_path) -import json_minify as minify +def StripJSONComments(stream): + """Strips //-style comments from a stream of JSON. Allows un-escaped // + inside string values. + """ + # Previously we used json_minify to strip comments, but it seems to be pretty + # slow and does more than we need. This implementation does a lot less work - + # it just strips comments from the beginning of the '//' delimiter until end + # of line, but only if we're not inside a string. For example: + # + # {"url": "http://www.example.com"} + # + # will work properly, as will: + # + # { + # "url": "http://www.example.com" // some comment + # } + result = "" + last_char = None + inside_string = False + inside_comment = False + buf = "" + for char in stream: + if inside_comment: + if char == '\n': + inside_comment = False + else: + continue + else: + if char == '/' and not inside_string: + if last_char == '/': + inside_comment = True + last_char = char + continue + else: + if last_char == '/' and not inside_string: + result += '/' + if char == '"': + inside_string = not inside_string + last_char = char + result += char + + return result def Load(filename): with open(filename, 'r') as handle: - return json.loads(minify.json_minify(handle.read())) + return json.loads(StripJSONComments(handle.read())) + + +# A dictionary mapping |filename| to the object resulting from loading the JSON +# at |filename|. +_cache = {} + +def CachedLoad(filename): + """Equivalent to Load(filename), but caches results for subsequent calls""" + if filename not in _cache: + _cache[filename] = Load(filename) + # Return a copy of the object so that any changes a caller makes won't affect + # the next caller. + return copy.deepcopy(_cache[filename]) diff --git a/tools/json_schema_compiler/model_test.py b/tools/json_schema_compiler/model_test.py index a588968..0590ddd 100644 --- a/tools/json_schema_compiler/model_test.py +++ b/tools/json_schema_compiler/model_test.py @@ -2,22 +2,22 @@ # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. -from json_schema import LoadJSON +from json_schema import CachedLoad import model import unittest class ModelTest(unittest.TestCase): def setUp(self): self.model = model.Model() - self.permissions_json = LoadJSON('test/permissions.json') + self.permissions_json = CachedLoad('test/permissions.json') self.model.AddNamespace(self.permissions_json[0], 'path/to/permissions.json') self.permissions = self.model.namespaces.get('permissions') - self.windows_json = LoadJSON('test/windows.json') + self.windows_json = CachedLoad('test/windows.json') self.model.AddNamespace(self.windows_json[0], 'path/to/window.json') self.windows = self.model.namespaces.get('windows') - self.tabs_json = LoadJSON('test/tabs.json') + self.tabs_json = CachedLoad('test/tabs.json') self.model.AddNamespace(self.tabs_json[0], 'path/to/tabs.json') self.tabs = self.model.namespaces.get('tabs') |