def _ShowCompiledFile(self, parsed_url, head, body): """Show the compiled version of a json file given the path to the compiled file. """ api_model = model.Model() request_path = self._GetRequestPath(parsed_url) (file_root, file_ext) = os.path.splitext(request_path) (filedir, filename) = os.path.split(file_root) json_file_path = os.path.normpath(file_root + '.json') try: # Get main json file api_defs = json_schema.Load(json_file_path) namespace = api_model.AddNamespace(api_defs[0], json_file_path) type_generator = cpp_type_generator.CppTypeGenerator( 'previewserver::api', namespace, namespace.unix_name) # Get json file depedencies for dependency in api_defs[0].get('dependencies', []): json_file_path = os.path.join(filedir, dependency + '.json') api_defs = json_schema.Load(json_file_path) referenced_namespace = api_model.AddNamespace( api_defs[0], json_file_path) if referenced_namespace: type_generator.AddNamespace( referenced_namespace, cpp_util.Classname(referenced_namespace.name).lower()) # Generate code if file_ext == '.h': cpp_code = (h_generator.HGenerator( namespace, type_generator).Generate().Render()) elif file_ext == '.cc': cpp_code = (cc_generator.CCGenerator( namespace, type_generator).Generate().Render()) else: self.send_error(404, "File not found: %s" % request_path) return # Do highlighting on the generated code (highlighter_param, style_param) = self._GetHighlighterParams(parsed_url) head.Append('<style>' + self.server.highlighters[highlighter_param].GetCSS( style_param) + '</style>') body.Append( self.server.highlighters[highlighter_param].GetCodeElement( cpp_code, style_param)) except IOError: self.send_error(404, "File not found: %s" % request_path) return except (TypeError, KeyError, AttributeError, AssertionError, NotImplementedError) as error: body.Append('<pre>') body.Append('compiler error: ' + str(error)) body.Append('Check server log for more details') body.Append('</pre>') raise
def _ShowCompiledFile(self, parsed_url, head, body): """Show the compiled version of a json or idl file given the path to the compiled file. """ api_model = model.Model() request_path = self._GetRequestPath(parsed_url) (file_root, file_ext) = os.path.splitext(request_path) (filedir, filename) = os.path.split(file_root) try: # Get main file. (api_def, file_path) = self._LoadModel(filedir, filename) namespace = api_model.AddNamespace(api_def, file_path) type_generator = cpp_type_generator.CppTypeGenerator( api_model, schema_loader.SchemaLoader(filedir), namespace) # Get the model's dependencies. for dependency in api_def.get('dependencies', []): # Dependencies can contain : in which case they don't refer to APIs, # rather, permissions or manifest keys. if ':' in dependency: continue (api_def, file_path) = self._LoadModel(filedir, dependency) referenced_namespace = api_model.AddNamespace(api_def, file_path) if referenced_namespace: type_generator.AddNamespace(referenced_namespace, cpp_util.Classname(referenced_namespace.name).lower()) # Generate code cpp_namespace = 'generated_api_schemas' if file_ext == '.h': cpp_code = (h_generator.HGenerator(type_generator, cpp_namespace) .Generate(namespace).Render()) elif file_ext == '.cc': cpp_code = (cc_generator.CCGenerator(type_generator, cpp_namespace) .Generate(namespace).Render()) else: self.send_error(404, "File not found: %s" % request_path) return # Do highlighting on the generated code (highlighter_param, style_param) = self._GetHighlighterParams(parsed_url) head.Append('<style>' + self.server.highlighters[highlighter_param].GetCSS(style_param) + '</style>') body.Append(self.server.highlighters[highlighter_param] .GetCodeElement(cpp_code, style_param)) except IOError: self.send_error(404, "File not found: %s" % request_path) return except (TypeError, KeyError, AttributeError, AssertionError, NotImplementedError) as error: body.Append('<pre>') body.Append('compiler error: %s' % error) body.Append('Check server log for more details') body.Append('</pre>') raise
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, schema_filename in zip(api_defs, filenames): namespace = api_model.AddNamespace(target_namespace, relpath) path, filename = os.path.split(schema_filename) short_filename, extension = os.path.splitext(filename) # Filenames are checked against the unix_names of the namespaces they # generate because the gyp uses the names of the JSON files to generate # the names of the .cc and .h files. We want these to be using unix_names. if namespace.unix_name != short_filename: sys.exit( "Filename %s is illegal. Name files using unix_hacker style." % schema_filename) 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 = schema_bundle_generator.SchemaBundleGenerator( api_model, api_defs, type_generator) api_h_code = generator.GenerateAPIHeader().Render() schemas_h_code = generator.GenerateSchemasHeader().Render() schemas_cc_code = generator.GenerateSchemasCC().Render() if dest_dir: basedir = os.path.join(dest_dir, 'chrome/common/extensions/api') with open(os.path.join(basedir, 'generated_api.h'), 'w') as h_file: h_file.write(api_h_code) with open(os.path.join(basedir, 'generated_schemas.h'), 'w') as h_file: h_file.write(schemas_h_code) with open(os.path.join(basedir, 'generated_schemas.cc'), 'w') as cc_file: cc_file.write(schemas_cc_code) else: print 'generated_api.h' print print api_h_code print print 'generated_schemas.h' print print schemas_h_code print print 'generated_schemas.cc' print print schemas_cc_code
def _ShowCompiledFile(self, parsed_url, head, body): """Show the compiled version of a json or idl file given the path to the compiled file. """ api_model = model.Model() request_path = self._GetRequestPath(parsed_url) (file_root, file_ext) = os.path.splitext(request_path) (filedir, filename) = os.path.split(file_root) schema_loader = SchemaLoader("./", filedir, self.server.include_rules, self.server.cpp_namespace_pattern) try: # Get main file. namespace = schema_loader.ResolveNamespace(filename) type_generator = cpp_type_generator.CppTypeGenerator( api_model, schema_loader, namespace) # Generate code cpp_namespace = 'generated_api_schemas' if file_ext == '.h': cpp_code = (h_generator.HGenerator(type_generator) .Generate(namespace).Render()) elif file_ext == '.cc': cpp_code = (cc_generator.CCGenerator(type_generator) .Generate(namespace).Render()) else: self.send_error(404, "File not found: %s" % request_path) return # Do highlighting on the generated code (highlighter_param, style_param) = self._GetHighlighterParams(parsed_url) head.Append('<style>' + self.server.highlighters[highlighter_param].GetCSS(style_param) + '</style>') body.Append(self.server.highlighters[highlighter_param] .GetCodeElement(cpp_code, style_param)) except IOError: self.send_error(404, "File not found: %s" % request_path) return except (TypeError, KeyError, AttributeError, AssertionError, NotImplementedError) as error: body.Append('<pre>') body.Append('compiler error: %s' % error) body.Append('Check server log for more details') body.Append('</pre>') raise
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 = schema_bundle_generator.SchemaBundleGenerator( api_model, api_defs, type_generator) api_h_code = generator.GenerateAPIHeader().Render() schemas_h_code = generator.GenerateSchemasHeader().Render() schemas_cc_code = generator.GenerateSchemasCC().Render() if dest_dir: basedir = os.path.join(dest_dir, 'chrome/common/extensions/api') with open(os.path.join(basedir, 'generated_api.h'), 'w') as h_file: h_file.write(api_h_code) with open(os.path.join(basedir, 'generated_schemas.h'), 'w') as h_file: h_file.write(schemas_h_code) with open(os.path.join(basedir, 'generated_schemas.cc'), 'w') as cc_file: cc_file.write(schemas_cc_code) else: print 'generated_api.h' print print api_h_code print print 'generated_schemas.h' print print schemas_h_code print print 'generated_schemas.cc' print print schemas_cc_code
def handle_single_schema(filename, dest_dir, root, root_namespace): schema = os.path.normpath(filename) schema_filename, schema_extension = os.path.splitext(schema) path, short_filename = os.path.split(schema_filename) api_defs = json_schema.DeleteNocompileNodes(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. # TODO(miket): do we need this in IDL? for referenced_schema in referenced_schemas: referenced_schema_path = os.path.join( os.path.dirname(schema), referenced_schema + '.json') referenced_api_defs = json_schema.Load(referenced_schema_path) for namespace in referenced_api_defs: api_model.AddNamespace(namespace, os.path.relpath(referenced_schema_path, opts.root)) # Gets the relative path from opts.root to the schema to correctly determine # the include path. relpath = os.path.relpath(schema, opts.root) namespace = api_model.AddNamespace(target_namespace, relpath) if not namespace: continue if short_filename != namespace.unix_name: sys.exit("Filename %s is illegal. Name files using unix_hacker style." % filename) # The output filename must match the input filename for gyp to deal with it # properly. out_file = namespace.unix_name type_generator = cpp_type_generator.CppTypeGenerator( root_namespace, namespace, namespace.unix_name) for referenced_namespace in api_model.namespaces.values(): if referenced_namespace == namespace: continue type_generator.AddNamespace( referenced_namespace, referenced_namespace.unix_name) h_code = (h_generator.HGenerator(namespace, type_generator) .Generate().Render()) cc_code = (cc_generator.CCGenerator(namespace, type_generator) .Generate().Render()) if dest_dir: with open( os.path.join(dest_dir, namespace.source_file_dir, out_file + '.cc'), 'w') as cc_file: cc_file.write(cc_code) with open( os.path.join(dest_dir, namespace.source_file_dir, out_file + '.h'), 'w') as h_file: h_file.write(h_code) else: print '%s.h' % out_file print print h_code print print '%s.cc' % out_file print print cc_code