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

Side by Side Diff: tools/json_schema_compiler/compiler.py

Issue 10272021: Files generated by the JSON schema compiler are named incorrectly (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Check bundle schema filenames Created 8 years, 7 months 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 unified diff | Download patch
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 """Generator for C++ structs from api json files. 5 """Generator for C++ structs from api json files.
6 6
7 The purpose of this tool is to remove the need for hand-written code that 7 The purpose of this tool is to remove the need for hand-written code that
8 converts to and from base::Value types when receiving javascript api calls. 8 converts to and from base::Value types when receiving javascript api calls.
9 Originally written for generating code for extension apis. Reference schemas 9 Originally written for generating code for extension apis. Reference schemas
10 are in chrome/common/extensions/api. 10 are in chrome/common/extensions/api.
(...skipping 20 matching lines...) Expand all
31 def load_schema(schema): 31 def load_schema(schema):
32 schema_filename, schema_extension = os.path.splitext(schema) 32 schema_filename, schema_extension = os.path.splitext(schema)
33 33
34 if schema_extension == '.json': 34 if schema_extension == '.json':
35 api_defs = json_schema.Load(schema) 35 api_defs = json_schema.Load(schema)
36 elif schema_extension == '.idl': 36 elif schema_extension == '.idl':
37 api_defs = idl_schema.Load(schema) 37 api_defs = idl_schema.Load(schema)
38 else: 38 else:
39 sys.exit("Did not recognize file extension %s for schema %s" % 39 sys.exit("Did not recognize file extension %s for schema %s" %
40 (schema_extension, schema)) 40 (schema_extension, schema))
41 if len(api_defs) != 1:
42 sys.exit("File %s has multiple schemas. Files are only allowed to contain a"
43 " single schema." % schema)
41 44
42 return api_defs 45 return api_defs
43 46
44 def handle_single_schema(filename, dest_dir, root, root_namespace): 47 def handle_single_schema(filename, dest_dir, root, root_namespace):
45 schema = os.path.normpath(filename) 48 schema = os.path.normpath(filename)
46 schema_filename, schema_extension = os.path.splitext(schema) 49 schema_filename, schema_extension = os.path.splitext(schema)
47 api_defs = load_schema(schema) 50 api_defs = load_schema(schema)
48 51
49 api_model = model.Model() 52 api_model = model.Model()
50 53
(...skipping 10 matching lines...) Expand all
61 api_model.AddNamespace(namespace, 64 api_model.AddNamespace(namespace,
62 os.path.relpath(referenced_schema_path, opts.root)) 65 os.path.relpath(referenced_schema_path, opts.root))
63 66
64 # Gets the relative path from opts.root to the schema to correctly determine 67 # Gets the relative path from opts.root to the schema to correctly determine
65 # the include path. 68 # the include path.
66 relpath = os.path.relpath(schema, opts.root) 69 relpath = os.path.relpath(schema, opts.root)
67 namespace = api_model.AddNamespace(target_namespace, relpath) 70 namespace = api_model.AddNamespace(target_namespace, relpath)
68 if not namespace: 71 if not namespace:
69 continue 72 continue
70 73
74 path, schema_filename = os.path.split(schema_filename)
not at google - send to devlin 2012/05/12 00:19:37 Rather than doing this every loop, just do it at t
cduvall 2012/05/12 00:59:37 Done.
75 if schema_filename != namespace.unix_name:
76 sys.exit("Filename %s%s is illegal. Name files using unix_hacker style." %
77 (schema_filename, schema_extension))
78
71 # The output filename must match the input filename for gyp to deal with it 79 # The output filename must match the input filename for gyp to deal with it
72 # properly. 80 # properly.
73 out_file = namespace.name 81 out_file = namespace.unix_name
74 type_generator = cpp_type_generator.CppTypeGenerator( 82 type_generator = cpp_type_generator.CppTypeGenerator(
75 root_namespace, namespace, namespace.unix_name) 83 root_namespace, namespace, namespace.unix_name)
76 for referenced_namespace in api_model.namespaces.values(): 84 for referenced_namespace in api_model.namespaces.values():
77 if referenced_namespace == namespace: 85 if referenced_namespace == namespace:
78 continue 86 continue
79 type_generator.AddNamespace( 87 type_generator.AddNamespace(
80 referenced_namespace, 88 referenced_namespace,
81 referenced_namespace.name) 89 referenced_namespace.unix_name)
82 90
83 h_code = (h_generator.HGenerator(namespace, type_generator) 91 h_code = (h_generator.HGenerator(namespace, type_generator)
84 .Generate().Render()) 92 .Generate().Render())
85 cc_code = (cc_generator.CCGenerator(namespace, type_generator) 93 cc_code = (cc_generator.CCGenerator(namespace, type_generator)
86 .Generate().Render()) 94 .Generate().Render())
87 95
88 if dest_dir: 96 if dest_dir:
89 with open( 97 with open(
90 os.path.join(dest_dir, namespace.source_file_dir, out_file + '.cc'), 98 os.path.join(dest_dir, namespace.source_file_dir, out_file + '.cc'),
91 'w') as cc_file: 99 'w') as cc_file:
(...skipping 14 matching lines...) Expand all
106 def handle_bundle_schema(filenames, dest_dir, root, root_namespace): 114 def handle_bundle_schema(filenames, dest_dir, root, root_namespace):
107 # Merge the source files into a single list of schemas. 115 # Merge the source files into a single list of schemas.
108 api_defs = [] 116 api_defs = []
109 for filename in filenames: 117 for filename in filenames:
110 schema = os.path.normpath(filename) 118 schema = os.path.normpath(filename)
111 schema_filename, schema_extension = os.path.splitext(schema) 119 schema_filename, schema_extension = os.path.splitext(schema)
112 api_defs.extend(load_schema(schema)) 120 api_defs.extend(load_schema(schema))
113 121
114 api_model = model.Model() 122 api_model = model.Model()
115 relpath = os.path.relpath(os.path.normpath(filenames[0]), root) 123 relpath = os.path.relpath(os.path.normpath(filenames[0]), root)
116 for target_namespace in api_defs: 124 for target_namespace, schema_filename in zip(api_defs, filenames):
117 api_model.AddNamespace(target_namespace, relpath) 125 namespace = api_model.AddNamespace(target_namespace, relpath)
126 filename, extension = os.path.splitext(schema_filename)
127 if namespace.unix_name != filename:
not at google - send to devlin 2012/05/12 00:19:37 could you add a comment here explaining why (just
cduvall 2012/05/12 00:59:37 Done.
128 sys.exit("Filename %s is illegal. Name files using unix_hacker style." %
129 schema_filename)
118 130
119 type_generator = cpp_type_generator.CppTypeGenerator(root_namespace) 131 type_generator = cpp_type_generator.CppTypeGenerator(root_namespace)
120 for referenced_namespace in api_model.namespaces.values(): 132 for referenced_namespace in api_model.namespaces.values():
121 type_generator.AddNamespace( 133 type_generator.AddNamespace(
122 referenced_namespace, 134 referenced_namespace,
123 referenced_namespace.name) 135 referenced_namespace.unix_name)
124 136
125 generator = schema_bundle_generator.SchemaBundleGenerator( 137 generator = schema_bundle_generator.SchemaBundleGenerator(
126 api_model, api_defs, type_generator) 138 api_model, api_defs, type_generator)
127 api_h_code = generator.GenerateAPIHeader().Render() 139 api_h_code = generator.GenerateAPIHeader().Render()
128 schemas_h_code = generator.GenerateSchemasHeader().Render() 140 schemas_h_code = generator.GenerateSchemasHeader().Render()
129 schemas_cc_code = generator.GenerateSchemasCC().Render() 141 schemas_cc_code = generator.GenerateSchemasCC().Render()
130 142
131 if dest_dir: 143 if dest_dir:
132 basedir = os.path.join(dest_dir, 'chrome/common/extensions/api') 144 basedir = os.path.join(dest_dir, 'chrome/common/extensions/api')
133 with open(os.path.join(basedir, 'generated_api.h'), 'w') as h_file: 145 with open(os.path.join(basedir, 'generated_api.h'), 'w') as h_file:
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
168 180
169 if not args: 181 if not args:
170 sys.exit(0) # This is OK as a no-op 182 sys.exit(0) # This is OK as a no-op
171 dest_dir = opts.destdir 183 dest_dir = opts.destdir
172 root_namespace = opts.namespace 184 root_namespace = opts.namespace
173 185
174 if opts.bundle: 186 if opts.bundle:
175 handle_bundle_schema(args, dest_dir, opts.root, root_namespace) 187 handle_bundle_schema(args, dest_dir, opts.root, root_namespace)
176 else: 188 else:
177 handle_single_schema(args[0], dest_dir, opts.root, root_namespace) 189 handle_single_schema(args[0], dest_dir, opts.root, root_namespace)
OLDNEW
« no previous file with comments | « tools/json_schema_compiler/cc_generator.py ('k') | tools/json_schema_compiler/cpp_type_generator_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698