def generate_code(self, definitions, interface_name, idl_pickle_filename, only_if_changed): """Returns .h/.cpp code as (header_text, cpp_text).""" try: interface = definitions.interfaces[interface_name] except KeyError: raise Exception('%s not in IDL definitions' % interface_name) # Store other interfaces for introspection interfaces.update(definitions.interfaces) # Set local type info IdlType.set_callback_functions(definitions.callback_functions.keys()) IdlType.set_enums((enum.name, enum.values) for enum in definitions.enumerations.values()) # Select appropriate Jinja template and contents function if interface.is_callback: header_template_filename = 'callback_interface_h.template' cpp_template_filename = 'callback_interface_cpp.template' generate_contents = dart_callback_interface.generate_callback_interface else: header_template_filename = 'interface_h.template' cpp_template_filename = 'interface_cpp.template' generate_contents = dart_interface.generate_interface header_template = self.jinja_env.get_template(header_template_filename) cpp_template = self.jinja_env.get_template(cpp_template_filename) # Generate contents (input parameters for Jinja) template_contents = generate_contents(interface) template_contents['code_generator'] = module_pyname # Add includes for interface itself and any dependencies interface_info = self.interfaces_info[interface_name] template_contents['header_includes'].add( interface_info['include_path']) template_contents['header_includes'] = sorted( template_contents['header_includes']) includes.update(interface_info.get('dependencies_include_paths', [])) # Remove includes that are not needed for Dart and trigger fatal # compile warnings if included. These IDL files need to be # imported by Dart to generate the list of events but the # associated header files do not contain any code used by Dart. includes.discard('core/dom/GlobalEventHandlers.h') includes.discard('core/frame/DOMWindowEventHandlers.h') template_contents['cpp_includes'] = sorted(includes) idl_world = {'interface': None, 'callback': None} # Load the pickle file for this IDL. if os.path.isfile(idl_pickle_filename): with open(idl_pickle_filename) as idl_pickle_file: idl_global_data = pickle.load(idl_pickle_file) idl_pickle_file.close() idl_world['interface'] = idl_global_data['interface'] idl_world['callback'] = idl_global_data['callback'] if 'interface_name' in template_contents: interface_global = { 'name': template_contents['interface_name'], 'parent_interface': template_contents['parent_interface'], 'is_active_dom_object': template_contents['is_active_dom_object'], 'is_event_target': template_contents['is_event_target'], 'has_resolver': template_contents['interface_name'] not in INTERFACES_WITHOUT_RESOLVERS, 'is_node': template_contents['is_node'], 'conditional_string': template_contents['conditional_string'], } idl_world['interface'] = interface_global else: callback_global = {'name': template_contents['cpp_class']} idl_world['callback'] = callback_global write_pickle_file(idl_pickle_filename, idl_world, only_if_changed) # Render Jinja templates header_text = header_template.render(template_contents) cpp_text = cpp_template.render(template_contents) return header_text, cpp_text
def generate_code(self, definitions, interface_name, idl_pickle_filename, only_if_changed): """Returns .h/.cpp code as (header_text, cpp_text).""" try: interface = definitions.interfaces[interface_name] except KeyError: raise Exception('%s not in IDL definitions' % interface_name) # Store other interfaces for introspection interfaces.update(definitions.interfaces) # Set local type info IdlType.set_callback_functions(definitions.callback_functions.keys()) IdlType.set_enums((enum.name, enum.values) for enum in definitions.enumerations.values()) # Select appropriate Jinja template and contents function if interface.is_callback: header_template_filename = 'callback_interface_h.template' cpp_template_filename = 'callback_interface_cpp.template' generate_contents = dart_callback_interface.generate_callback_interface else: header_template_filename = 'interface_h.template' cpp_template_filename = 'interface_cpp.template' generate_contents = dart_interface.generate_interface header_template = self.jinja_env.get_template(header_template_filename) cpp_template = self.jinja_env.get_template(cpp_template_filename) # Generate contents (input parameters for Jinja) template_contents = generate_contents(interface) template_contents['code_generator'] = module_pyname # Add includes for interface itself and any dependencies interface_info = self.interfaces_info[interface_name] template_contents['header_includes'].add(interface_info['include_path']) template_contents['header_includes'] = sorted(template_contents['header_includes']) includes.update(interface_info.get('dependencies_include_paths', [])) # Remove includes that are not needed for Dart and trigger fatal # compile warnings if included. These IDL files need to be # imported by Dart to generate the list of events but the # associated header files do not contain any code used by Dart. includes.discard('core/dom/GlobalEventHandlers.h') includes.discard('core/frame/DOMWindowEventHandlers.h') template_contents['cpp_includes'] = sorted(includes) idl_world = {'interface': None, 'callback': None} # Load the pickle file for this IDL. if os.path.isfile(idl_pickle_filename): with open(idl_pickle_filename) as idl_pickle_file: idl_global_data = pickle.load(idl_pickle_file) idl_pickle_file.close() idl_world['interface'] = idl_global_data['interface'] idl_world['callback'] = idl_global_data['callback'] if 'interface_name' in template_contents: interface_global = {'name': template_contents['interface_name'], 'parent_interface': template_contents['parent_interface'], 'is_active_dom_object': template_contents['is_active_dom_object'], 'is_event_target': template_contents['is_event_target'], 'has_resolver': template_contents['interface_name'] not in INTERFACES_WITHOUT_RESOLVERS, 'is_node': template_contents['is_node'], 'conditional_string': template_contents['conditional_string'], } idl_world['interface'] = interface_global else: callback_global = {'name': template_contents['cpp_class']} idl_world['callback'] = callback_global write_pickle_file(idl_pickle_filename, idl_world, only_if_changed) # Render Jinja templates header_text = header_template.render(template_contents) cpp_text = cpp_template.render(template_contents) return header_text, cpp_text