diff options
author | kalman@chromium.org <kalman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-04-17 02:11:09 +0000 |
---|---|---|
committer | kalman@chromium.org <kalman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-04-17 02:11:09 +0000 |
commit | a1f77497010d814003986ed8690b321208eea402 (patch) | |
tree | 3e365566672ab9c5971be51291de13a7aafcd1bd /tools/json_schema_compiler | |
parent | 18929a9d3aaa97967efa8a93def0c7b818b429be (diff) | |
download | chromium_src-a1f77497010d814003986ed8690b321208eea402.zip chromium_src-a1f77497010d814003986ed8690b321208eea402.tar.gz chromium_src-a1f77497010d814003986ed8690b321208eea402.tar.bz2 |
Make json_schema_compiler remove 'nocompile' nodes from JSON at the JSON level
rather than at the model level. This gives us automatic nocompile of all
properties; previously not all were supported.
TEST=tools/json_schema_compiler/json_schema_test.py, unit_tests --gtest_filter=JsonSchemaCompiler*
Review URL: http://codereview.chromium.org/10108005
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@132514 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'tools/json_schema_compiler')
-rw-r--r-- | tools/json_schema_compiler/json_schema.py | 21 | ||||
-rwxr-xr-x | tools/json_schema_compiler/json_schema_test.py | 78 | ||||
-rw-r--r-- | tools/json_schema_compiler/model.py | 12 | ||||
-rwxr-xr-x | tools/json_schema_compiler/model_test.py | 16 | ||||
-rwxr-xr-x | tools/json_schema_compiler/previewserver.py | 6 | ||||
-rw-r--r-- | tools/json_schema_compiler/test/json_schema_test.json | 101 |
6 files changed, 203 insertions, 31 deletions
diff --git a/tools/json_schema_compiler/json_schema.py b/tools/json_schema_compiler/json_schema.py index 46ca487..240a168 100644 --- a/tools/json_schema_compiler/json_schema.py +++ b/tools/json_schema_compiler/json_schema.py @@ -48,9 +48,28 @@ def StripJSONComments(stream): return result +def DeleteNocompileNodes(item): + def HasNocompile(thing): + return type(thing) == dict and thing.get('nocompile', False) + + if type(item) == dict: + toDelete = [] + for key, value in item.items(): + if HasNocompile(value): + toDelete.append(key) + else: + DeleteNocompileNodes(value) + for key in toDelete: + del item[key] + elif type(item) == list: + item[:] = [DeleteNocompileNodes(thing) + for thing in item if not HasNocompile(thing)] + + return item + def Load(filename): with open(filename, 'r') as handle: - return json.loads(StripJSONComments(handle.read())) + return DeleteNocompileNodes(json.loads(StripJSONComments(handle.read()))) # A dictionary mapping |filename| to the object resulting from loading the JSON diff --git a/tools/json_schema_compiler/json_schema_test.py b/tools/json_schema_compiler/json_schema_test.py new file mode 100755 index 0000000..305431b --- /dev/null +++ b/tools/json_schema_compiler/json_schema_test.py @@ -0,0 +1,78 @@ +#!/usr/bin/env python +# 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. + +from json_schema import CachedLoad +import json_schema_test +import unittest + +class JsonSchemaUnittest(unittest.TestCase): + def testNocompile(self): + compiled = [ + { + "namespace": "compile", + "functions": [], + "types": {} + }, + + { + "namespace": "functions", + "functions": [ + { + "id": "two" + }, + { + "id": "four" + } + ], + + "types": { + "one": { "key": "value" } + } + }, + + { + "namespace": "types", + "functions": [ + { "id": "one" } + ], + "types": { + "two": { + "key": "value" + }, + "four": { + "key": "value" + } + } + }, + + { + "namespace": "nested", + "properties": { + "sync": { + "functions": [ + { + "id": "two" + }, + { + "id": "four" + } + ], + "types": { + "two": { + "key": "value" + }, + "four": { + "key": "value" + } + } + } + } + } + ] + + self.assertEquals(compiled, CachedLoad('test/json_schema_test.json')) + +if __name__ == '__main__': + unittest.main() diff --git a/tools/json_schema_compiler/model.py b/tools/json_schema_compiler/model.py index 53b3aee..9a5c55f 100644 --- a/tools/json_schema_compiler/model.py +++ b/tools/json_schema_compiler/model.py @@ -16,12 +16,8 @@ class Model(object): self.namespaces = {} def AddNamespace(self, json, source_file): - """Add a namespace's json to the model if it doesn't have "nocompile" - property set to true. Returns the new namespace or None if a namespace - wasn't added. + """Add a namespace's json to the model and returns the namespace. """ - if json.get('nocompile', False): - return None namespace = Namespace(json, source_file) self.namespaces[namespace.name] = namespace return namespace @@ -54,8 +50,7 @@ class Namespace(object): type_ = Type(self, type_json['id'], type_json) self.types[type_.name] = type_ for function_json in json.get('functions', []): - if not function_json.get('nocompile', False): - self.functions[function_json['name']] = Function(self, function_json) + self.functions[function_json['name']] = Function(self, function_json) class Type(object): """A Type defined in the json. @@ -94,8 +89,7 @@ class Type(object): self.functions = {} self.parent = parent for function_json in json.get('functions', []): - if not function_json.get('nocompile', False): - self.functions[function_json['name']] = Function(self, function_json) + self.functions[function_json['name']] = Function(self, function_json) props = [] for prop_name, prop_json in json.get('properties', {}).items(): # TODO(calamity): support functions (callbacks) as properties. The model diff --git a/tools/json_schema_compiler/model_test.py b/tools/json_schema_compiler/model_test.py index d91acb5..10f8656 100755 --- a/tools/json_schema_compiler/model_test.py +++ b/tools/json_schema_compiler/model_test.py @@ -27,26 +27,10 @@ class ModelTest(unittest.TestCase): self.assertEquals(3, len(self.model.namespaces)) self.assertTrue(self.permissions) - def testNamespaceNoCompile(self): - self.permissions_json[0]['namespace'] = 'something' - self.permissions_json[0]['nocompile'] = True - self.model.AddNamespace(self.permissions_json[0], - 'path/to/something.json') - self.assertEquals(3, len(self.model.namespaces)) - def testHasFunctions(self): self.assertEquals(["contains", "getAll", "remove", "request"], sorted(self.permissions.functions.keys())) - def testFunctionNoCompile(self): - # tabs.json has 2 functions marked as nocompile (connect, sendRequest) - self.assertEquals(["captureVisibleTab", "create", "detectLanguage", - "executeScript", "get", "getAllInWindow", "getCurrent", - "getSelected", "highlight", "insertCSS", "move", "query", "reload", - "remove", "update"], - sorted(self.tabs.functions.keys()) - ) - def testHasTypes(self): self.assertEquals(['Tab'], self.tabs.types.keys()) self.assertEquals(['Permissions'], self.permissions.types.keys()) diff --git a/tools/json_schema_compiler/previewserver.py b/tools/json_schema_compiler/previewserver.py index 65df25b..2d4105a 100755 --- a/tools/json_schema_compiler/previewserver.py +++ b/tools/json_schema_compiler/previewserver.py @@ -190,12 +190,8 @@ updateEverything(); # Get main json file api_defs = json_schema.Load(json_file_path) namespace = api_model.AddNamespace(api_defs[0], json_file_path) - if not namespace: - body.Append("<pre>Target file %s is marked nocompile</pre>" % - json_file_path) - return type_generator = cpp_type_generator.CppTypeGenerator( - 'preview::api', namespace, namespace.unix_name) + 'previewserver::api', namespace, namespace.unix_name) # Get json file depedencies for dependency in api_defs[0].get('dependencies', []): diff --git a/tools/json_schema_compiler/test/json_schema_test.json b/tools/json_schema_compiler/test/json_schema_test.json new file mode 100644 index 0000000..0ac9131 --- /dev/null +++ b/tools/json_schema_compiler/test/json_schema_test.json @@ -0,0 +1,101 @@ +[ + { + "namespace": "compile", + "functions": [], + "types": {} + }, + + { + "namespace": "nocompile", + "nocompile": true, + "functions": [], + "types": {} + }, + + { + "namespace": "functions", + "functions": [ + { + "id": "one", + "nocompile": true + }, + { + "id": "two" + }, + { + "id": "three", + "nocompile": true + }, + { + "id": "four" + } + ], + + "types": { + "one": { "key": "value" } + } + }, + + { + "namespace": "types", + "functions": [ + { "id": "one" } + ], + "types": { + "one": { + "key": "value", + "nocompile": true + }, + "two": { + "key": "value" + }, + "three": { + "key": "value", + "nocompile": true + }, + "four": { + "key": "value" + } + } + }, + + { + "namespace": "nested", + "properties": { + "sync": { + "functions": [ + { + "id": "one", + "nocompile": true + }, + { + "id": "two" + }, + { + "id": "three", + "nocompile": true + }, + { + "id": "four" + } + ], + "types": { + "one": { + "key": "value", + "nocompile": true + }, + "two": { + "key": "value" + }, + "three": { + "key": "value", + "nocompile": true + }, + "four": { + "key": "value" + } + } + } + } + } +] |