summaryrefslogtreecommitdiffstats
path: root/tools/json_schema_compiler
diff options
context:
space:
mode:
authormiket@chromium.org <miket@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-03-16 17:08:32 +0000
committermiket@chromium.org <miket@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-03-16 17:08:32 +0000
commit1a702f6f809da15b2e1a8dd3fd5b0259c89e74c4 (patch)
tree47a3c5204ed371b1abfcc8471379c27cfe2c6831 /tools/json_schema_compiler
parent26c4f2e43212a484cede7bcb0f6e1889e8fa7b50 (diff)
downloadchromium_src-1a702f6f809da15b2e1a8dd3fd5b0259c89e74c4.zip
chromium_src-1a702f6f809da15b2e1a8dd3fd5b0259c89e74c4.tar.gz
chromium_src-1a702f6f809da15b2e1a8dd3fd5b0259c89e74c4.tar.bz2
Revert 127159 - Refactor extension_function_dispatcher to extract ExtensionFunctionRegistry. This allows us to generate an additional code block that takes an ExtensionFunctionRegistry and asks it to register generated API functions.
Then switch DnsResolve over to get registered this way. Along the way, notice that DNSResolve is capitalized using an untenable style. Fix that. BUG=none (essential plumbing work) TEST=same as before Review URL: http://codereview.chromium.org/9666059 TBR=miket@chromium.org Review URL: https://chromiumcodereview.appspot.com/9701105 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@127188 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'tools/json_schema_compiler')
-rw-r--r--tools/json_schema_compiler/compiler.py95
-rw-r--r--tools/json_schema_compiler/cpp_type_generator.py7
-rw-r--r--tools/json_schema_compiler/cpp_util.py13
-rw-r--r--tools/json_schema_compiler/h_bundle_generator.py79
-rw-r--r--tools/json_schema_compiler/h_generator.py13
-rw-r--r--tools/json_schema_compiler/test/json_schema_compiler_tests.gyp3
6 files changed, 37 insertions, 173 deletions
diff --git a/tools/json_schema_compiler/compiler.py b/tools/json_schema_compiler/compiler.py
index bd9805d..f782f72 100644
--- a/tools/json_schema_compiler/compiler.py
+++ b/tools/json_schema_compiler/compiler.py
@@ -18,7 +18,6 @@ Usage example:
import cc_generator
import cpp_type_generator
-import h_bundle_generator
import h_generator
import idl_schema
import json_schema
@@ -27,9 +26,30 @@ import optparse
import os.path
import sys
-def load_schema(schema):
- schema_filename, schema_extension = os.path.splitext(schema)
+if __name__ == '__main__':
+ parser = optparse.OptionParser(
+ description='Generates a C++ model of an API from JSON schema',
+ usage='usage: %prog [option]... schema')
+ parser.add_option('-r', '--root', default='.',
+ help='logical include root directory. Path to schema files from specified'
+ 'dir will be the include path.')
+ parser.add_option('-d', '--destdir',
+ help='root directory to output generated files.')
+ parser.add_option('-n', '--namespace', default='generated_api_schemas',
+ help='C++ namespace for generated files. e.g extensions::api.')
+
+ (opts, args) = parser.parse_args()
+ if not args:
+ sys.exit(parser.get_usage())
+ dest_dir = opts.destdir
+ root_namespace = opts.namespace
+
+ schema = os.path.normpath(args[0])
+ api_model = model.Model()
+
+ # Actually generate for source file.
+ schema_filename, schema_extension = os.path.splitext(schema)
if schema_extension == '.json':
api_defs = json_schema.Load(schema)
elif schema_extension == '.idl':
@@ -38,15 +58,6 @@ def load_schema(schema):
sys.exit("Did not recognize file extension %s for schema %s" %
(schema_extension, schema))
- return api_defs
-
-def handle_single_schema(filename, dest_dir, root, root_namespace):
- schema = os.path.normpath(filename)
- schema_filename, schema_extension = os.path.splitext(schema)
- api_defs = load_schema(schema)
-
- api_model = model.Model()
-
for target_namespace in api_defs:
referenced_schemas = target_namespace.get('dependencies', [])
# Load type dependencies into the model.
@@ -99,63 +110,3 @@ def handle_single_schema(filename, dest_dir, root, root_namespace):
print '%s.cc' % out_file
print
print cc_code
-
-def handle_bundle_schema(filenames, dest_dir, root, root_namespace):
- # Merge the source files into a single list of schemas.
- api_defs = []
- for filename in filenames:
- schema = os.path.normpath(filename)
- schema_filename, schema_extension = os.path.splitext(schema)
- api_defs.extend(load_schema(schema))
-
- api_model = model.Model()
- relpath = os.path.relpath(os.path.normpath(filenames[0]), root)
- for target_namespace in api_defs:
- api_model.AddNamespace(target_namespace, relpath)
-
- type_generator = cpp_type_generator.CppTypeGenerator(root_namespace)
- for referenced_namespace in api_model.namespaces.values():
- type_generator.AddNamespace(
- referenced_namespace,
- referenced_namespace.unix_name)
-
- generator = h_bundle_generator.HBundleGenerator(api_model, type_generator)
- h_bundle_code = generator.Generate().Render()
-
- out_file = 'generated_api'
- if dest_dir:
- with open(
- os.path.join(dest_dir, 'chrome/common/extensions/api/generated_api.h'),
- 'w') as h_file:
- h_file.write(h_bundle_code)
- else:
- print '%s.h' % out_file
- print
- print h_bundle_code
-
-if __name__ == '__main__':
- parser = optparse.OptionParser(
- description='Generates a C++ model of an API from JSON schema',
- usage='usage: %prog [option]... schema')
- parser.add_option('-r', '--root', default='.',
- help='logical include root directory. Path to schema files from specified'
- 'dir will be the include path.')
- parser.add_option('-d', '--destdir',
- help='root directory to output generated files.')
- parser.add_option('-n', '--namespace', default='generated_api_schemas',
- help='C++ namespace for generated files. e.g extensions::api.')
- parser.add_option('-b', '--bundle', action="store_true", help=
-'''if supplied, causes compiler to generate bundle files for the given set of
-source files.''')
-
- (opts, args) = parser.parse_args()
-
- if not args:
- sys.exit(0) # This is OK as a no-op
- dest_dir = opts.destdir
- root_namespace = opts.namespace
-
- if opts.bundle:
- handle_bundle_schema(args, dest_dir, opts.root, root_namespace)
- else:
- handle_single_schema(args[0], dest_dir, opts.root, root_namespace)
diff --git a/tools/json_schema_compiler/cpp_type_generator.py b/tools/json_schema_compiler/cpp_type_generator.py
index 482819c..44e7b59 100644
--- a/tools/json_schema_compiler/cpp_type_generator.py
+++ b/tools/json_schema_compiler/cpp_type_generator.py
@@ -11,17 +11,16 @@ class CppTypeGenerator(object):
"""Manages the types of properties and provides utilities for getting the
C++ type out of a model.Property
"""
- def __init__(self, root_namespace, namespace=None, cpp_namespace=None):
+ def __init__(self, root_namespace, namespace, cpp_namespace):
"""Creates a cpp_type_generator. The given root_namespace should be of the
format extensions::api::sub. The generator will generate code suitable for
use in the given namespace.
"""
self._type_namespaces = {}
+ self._namespace = namespace
self._root_namespace = root_namespace.split('::')
self._cpp_namespaces = {}
- if namespace and cpp_namespace:
- self._namespace = namespace
- self.AddNamespace(namespace, cpp_namespace)
+ self.AddNamespace(namespace, cpp_namespace)
def AddNamespace(self, namespace, cpp_namespace):
"""Maps a model.Namespace to its C++ namespace name. All mappings are
diff --git a/tools/json_schema_compiler/cpp_util.py b/tools/json_schema_compiler/cpp_util.py
index 0886826..3b65f5d 100644
--- a/tools/json_schema_compiler/cpp_util.py
+++ b/tools/json_schema_compiler/cpp_util.py
@@ -6,7 +6,6 @@
from datetime import datetime
from model import PropertyType
-import os
CHROMIUM_LICENSE = (
"""// Copyright (c) %d The Chromium Authors. All rights reserved.
@@ -17,10 +16,6 @@ GENERATED_FILE_MESSAGE = """// GENERATED FROM THE API DEFINITION IN
// %s
// DO NOT EDIT.
"""
-GENERATED_BUNDLE_FILE_MESSAGE = """// GENERATED FROM THE API DEFINITIONS IN
-// %s
-// DO NOT EDIT.
-"""
def Classname(s):
"""Translates a namespace name or function name into something more
@@ -72,11 +67,3 @@ def GetParameterDeclaration(param, type_):
'type': type_,
'name': param.unix_name,
}
-
-def GenerateIfndefName(path, filename):
- """Formats a path and filename as a #define name.
-
- e.g chrome/extensions/gen, file.h becomes CHROME_EXTENSIONS_GEN_FILE_H__.
- """
- return (('%s_%s_H__' % (path, filename))
- .upper().replace(os.sep, '_').replace('/', '_'))
diff --git a/tools/json_schema_compiler/h_bundle_generator.py b/tools/json_schema_compiler/h_bundle_generator.py
deleted file mode 100644
index 5aa22d4..0000000
--- a/tools/json_schema_compiler/h_bundle_generator.py
+++ /dev/null
@@ -1,79 +0,0 @@
-# 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 model import PropertyType
-import code
-import cpp_util
-import model
-
-SOURCE_BASE_PATH = 'chrome/common/extensions/api'
-
-class HBundleGenerator(object):
- """A .h generator for namespace bundle functionality.
- """
- def __init__(self, model, cpp_type_generator):
- self._cpp_type_generator = cpp_type_generator
- self._model = model
-
- def Generate(self):
- """Generates a code.Code object with the .h for a bundle.
- """
- c = code.Code()
- (c.Append(cpp_util.CHROMIUM_LICENSE)
- .Append()
- .Append(cpp_util.GENERATED_BUNDLE_FILE_MESSAGE % SOURCE_BASE_PATH)
- .Append()
- )
-
- ifndef_name = cpp_util.GenerateIfndefName(SOURCE_BASE_PATH,
- 'generated_api')
- (c.Append('#ifndef %s' % ifndef_name)
- .Append('#define %s' % ifndef_name)
- .Append('#pragma once')
- .Append()
- .Append('#include <string>')
- .Append()
- .Append('#include "base/basictypes.h"'))
-
- for namespace in self._model.namespaces.values():
- namespace_name = namespace.name.replace(
- "experimental.", "")
- c.Append('#include "chrome/browser/extensions/api/%s/%s_api.h"' % (
- namespace_name, namespace_name))
-
- (c.Append()
- .Append("class ExtensionFunctionRegistry;")
- .Append())
-
- c.Concat(self._cpp_type_generator.GetRootNamespaceStart())
-
- for namespace in self._model.namespaces.values():
- c.Append("// TODO(miket): emit code for %s" % (namespace.unix_name))
- c.Append()
-
- c.Concat(self.GenerateFunctionRegistry())
-
- (c.Concat(self._cpp_type_generator.GetRootNamespaceEnd())
- .Append()
- .Append('#endif // %s' % ifndef_name)
- .Append()
- )
- return c
-
- def GenerateFunctionRegistry(self):
- c = code.Code()
- c.Sblock("class GeneratedFunctionRegistry {")
- c.Append("public:")
- c.Sblock("static void RegisterAll(ExtensionFunctionRegistry* registry) {")
- for namespace in self._model.namespaces.values():
- for function in namespace.functions.values():
- namespace_name = namespace.name.replace(
- "experimental.", "").capitalize()
- function_name = namespace_name + function.name.capitalize()
- c.Append("registry->RegisterFunction<%sFunction>();" % (
- function_name))
- c.Eblock("}")
- c.Eblock("};")
- c.Append()
- return c
diff --git a/tools/json_schema_compiler/h_generator.py b/tools/json_schema_compiler/h_generator.py
index dfc99cb..3c97bc6 100644
--- a/tools/json_schema_compiler/h_generator.py
+++ b/tools/json_schema_compiler/h_generator.py
@@ -27,8 +27,7 @@ class HGenerator(object):
.Append()
)
- ifndef_name = cpp_util.GenerateIfndefName(self._namespace.source_file_dir,
- self._target_namespace)
+ ifndef_name = self._GenerateIfndefName()
(c.Append('#ifndef %s' % ifndef_name)
.Append('#define %s' % ifndef_name)
.Append('#pragma once')
@@ -264,3 +263,13 @@ class HGenerator(object):
c.Eblock('};')
return c
+
+ def _GenerateIfndefName(self):
+ """Formats a path and filename as a #define name.
+
+ e.g chrome/extensions/gen, file.h becomes CHROME_EXTENSIONS_GEN_FILE_H__.
+ """
+ return (('%s_%s_H__' %
+ (self._namespace.source_file_dir, self._target_namespace))
+ .upper().replace(os.sep, '_'))
+
diff --git a/tools/json_schema_compiler/test/json_schema_compiler_tests.gyp b/tools/json_schema_compiler/test/json_schema_compiler_tests.gyp
index 0310014..58e50b7 100644
--- a/tools/json_schema_compiler/test/json_schema_compiler_tests.gyp
+++ b/tools/json_schema_compiler/test/json_schema_compiler_tests.gyp
@@ -20,14 +20,11 @@
'objects.json',
'simple_api.json',
],
- 'idl_schema_files': [
- ],
'cc_dir': 'tools/json_schema_compiler/test',
'root_namespace': 'test::api',
},
'sources': [
'<@(json_schema_files)',
- '<@(idl_schema_files)',
],
'includes': ['../../../build/json_schema_compile.gypi'],
},