def generate_cpp(generator_arguments_file): args = read_generator_arguments(generator_arguments_file) template_dir = args['template_dir'] mapping_msgs = { os.path.join(template_dir, 'msg.hpp.em'): '%s.hpp', os.path.join(template_dir, 'msg__struct.hpp.em'): '%s__struct.hpp', os.path.join(template_dir, 'msg__traits.hpp.em'): '%s__traits.hpp', } mapping_srvs = { os.path.join(template_dir, 'srv.hpp.em'): '%s.hpp', os.path.join(template_dir, 'srv__struct.hpp.em'): '%s__struct.hpp', os.path.join(template_dir, 'srv__traits.hpp.em'): '%s__traits.hpp', } for template_file in mapping_msgs.keys(): assert os.path.exists( template_file), 'Could not find template: ' + template_file for template_file in mapping_srvs.keys(): assert os.path.exists( template_file), 'Could not find template: ' + template_file functions = { 'get_header_filename_from_msg_name': convert_camel_case_to_lower_case_underscore, } latest_target_timestamp = get_newest_modification_time( args['target_dependencies']) for ros_interface_file in args['ros_interface_files']: extension = os.path.splitext(ros_interface_file)[1] subfolder = os.path.basename(os.path.dirname(ros_interface_file)) if extension == '.msg': spec = parse_message_file(args['package_name'], ros_interface_file) for template_file, generated_filename in mapping_msgs.items(): data = {'spec': spec, 'subfolder': subfolder} data.update(functions) generated_file = os.path.join( args['output_dir'], subfolder, generated_filename % convert_camel_case_to_lower_case_underscore( spec.base_type.type)) expand_template(template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) elif extension == '.srv': spec = parse_service_file(args['package_name'], ros_interface_file) for template_file, generated_filename in mapping_srvs.items(): data = {'spec': spec} data.update(functions) generated_file = os.path.join( args['output_dir'], subfolder, generated_filename % convert_camel_case_to_lower_case_underscore(spec.srv_name)) expand_template(template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) return 0
def generate_cpp(generator_arguments_file): args = read_generator_arguments(generator_arguments_file) template_dir = args['template_dir'] mapping_msgs = { os.path.join(template_dir, 'msg__type_support.cpp.template'): '%s__type_support.cpp', } mapping_srvs = { os.path.join(template_dir, 'srv__type_support.cpp.template'): '%s__type_support.cpp', } for template_file in mapping_msgs.keys(): assert os.path.exists(template_file), 'Could not find template: ' + template_file for template_file in mapping_srvs.keys(): assert os.path.exists(template_file), 'Could not find template: ' + template_file pkg_name = args['package_name'] known_msg_types = extract_message_types( pkg_name, args['ros_interface_files'], args.get('ros_interface_dependencies', [])) functions = { 'get_header_filename_from_msg_name': convert_camel_case_to_lower_case_underscore, } latest_target_timestamp = get_newest_modification_time(args['target_dependencies']) for ros_interface_file in args['ros_interface_files']: extension = os.path.splitext(ros_interface_file)[1] subfolder = os.path.basename(os.path.dirname(ros_interface_file)) if extension == '.msg': spec = parse_message_file(pkg_name, ros_interface_file) validate_field_types(spec, known_msg_types) for template_file, generated_filename in mapping_msgs.items(): generated_file = os.path.join( args['output_dir'], subfolder, generated_filename % convert_camel_case_to_lower_case_underscore(spec.base_type.type)) data = {'spec': spec, 'subfolder': subfolder} data.update(functions) expand_template( template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) elif extension == '.srv': spec = parse_service_file(pkg_name, ros_interface_file) validate_field_types(spec, known_msg_types) for template_file, generated_filename in mapping_srvs.items(): generated_file = os.path.join( args['output_dir'], subfolder, generated_filename % convert_camel_case_to_lower_case_underscore(spec.srv_name)) data = {'spec': spec} data.update(functions) expand_template( template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) return 0
def generate_py(generator_arguments_file): args = read_generator_arguments(generator_arguments_file) template_dir = args['template_dir'] mapping_msgs = { os.path.join(template_dir, '_msg.py.template'): '_%s.py', } mapping_srvs = { os.path.join(template_dir, '_srv.py.template'): '_%s.py', } for template_file in mapping_msgs.keys(): assert os.path.exists(template_file), \ 'Messages template file %s not found' % template_file for template_file in mapping_srvs.keys(): assert os.path.exists(template_file), \ 'Services template file %s not found' % template_file functions = { 'constant_value_to_py': constant_value_to_py, 'value_to_py': value_to_py, } latest_target_timestamp = get_newest_modification_time(args['target_dependencies']) modules = defaultdict(list) for ros_interface_file in args['ros_interface_files']: extension = os.path.splitext(ros_interface_file)[1] subfolder = os.path.basename(os.path.dirname(ros_interface_file)) if extension == '.msg': spec = parse_message_file(args['package_name'], ros_interface_file) mapping = mapping_msgs type_name = spec.base_type.type elif extension == '.srv': spec = parse_service_file(args['package_name'], ros_interface_file) mapping = mapping_srvs type_name = spec.srv_name else: continue module_name = convert_camel_case_to_lower_case_underscore(type_name) modules[subfolder].append((module_name, type_name)) for template_file, generated_filename in mapping.items(): data = {'package_name': args['package_name'], 'spec': spec} data.update(functions) generated_file = os.path.join( args['output_dir'], subfolder, generated_filename % module_name) expand_template( template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) open(os.path.join(args['output_dir'], '__init__.py'), 'w').close() for module in modules: with open(os.path.join(args['output_dir'], module, '__init__.py'), 'w') as f: for module_, type_ in modules[module]: f.write('from %s.msg._%s import %s\n' % (args['package_name'], module_, type_)) return 0
def load_ros2_service(ros2_srv): srv_path = os.path.join( ros2_srv.prefix_path, 'share', ros2_srv.package_name, 'srv', ros2_srv.message_name + '.srv') try: spec = rosidl_parser.parse_service_file(ros2_srv.package_name, srv_path) except rosidl_parser.InvalidSpecification: return None return spec
def generate_c(generator_arguments_file): args = read_generator_arguments(generator_arguments_file) template_dir = args['template_dir'] mapping_msgs = { os.path.join(template_dir, 'msg.h.em'): '%s.h', os.path.join(template_dir, 'msg__functions.c.em'): '%s__functions.c', os.path.join(template_dir, 'msg__functions.h.em'): '%s__functions.h', os.path.join(template_dir, 'msg__struct.h.em'): '%s__struct.h', os.path.join(template_dir, 'msg__type_support.h.em'): '%s__type_support.h', } mapping_srvs = { os.path.join(template_dir, 'srv.h.em'): '%s.h', } for template_file in list(mapping_msgs.keys()) + list(mapping_srvs.keys()): assert os.path.exists(template_file), 'Could not find template: ' + template_file functions = { 'get_header_filename_from_msg_name': convert_camel_case_to_lower_case_underscore, } latest_target_timestamp = get_newest_modification_time(args['target_dependencies']) for ros_interface_file in args['ros_interface_files']: extension = os.path.splitext(ros_interface_file)[1] subfolder = os.path.basename(os.path.dirname(ros_interface_file)) if extension == '.msg': spec = parse_message_file(args['package_name'], ros_interface_file) for template_file, generated_filename in mapping_msgs.items(): generated_file = os.path.join( args['output_dir'], subfolder, generated_filename % convert_camel_case_to_lower_case_underscore(spec.base_type.type)) data = { 'spec': spec, 'pkg': spec.base_type.pkg_name, 'msg': spec.msg_name, 'type': spec.base_type.type, 'subfolder': subfolder, } data.update(functions) expand_template( template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) elif extension == '.srv': spec = parse_service_file(args['package_name'], ros_interface_file) for template_file, generated_filename in mapping_srvs.items(): data = {'spec': spec} data.update(functions) generated_file = os.path.join( args['output_dir'], subfolder, generated_filename % convert_camel_case_to_lower_case_underscore(spec.srv_name)) expand_template( template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) return 0
def parse_ros_interface_files(pkg_name, ros_interface_files): message_specs = [] service_specs = [] for idl_file in ros_interface_files: extension = os.path.splitext(idl_file)[1] if extension == '.msg': message_spec = parse_message_file(pkg_name, idl_file) message_specs.append((idl_file, message_spec)) elif extension == '.srv': service_spec = parse_service_file(pkg_name, idl_file) service_specs.append(service_spec) return (message_specs, service_specs)
def generate_dds_idl(generator_arguments_file, subfolders, extension_module_name): args = read_generator_arguments(generator_arguments_file) template_file = os.path.join(args['template_dir'], 'msg.idl.em') assert os.path.exists(template_file), \ "The template '%s' does not exist" % template_file # look for extensions for the default functions functions = { 'get_include_directives': get_include_directives, 'get_post_struct_lines': get_post_struct_lines, 'msg_type_to_idl': msg_type_to_idl, } if extension_module_name is not None: pkg = __import__(extension_module_name) module_name = extension_module_name.rsplit('.', 1)[1] if hasattr(pkg, module_name): module = getattr(pkg, module_name) for function_name in functions.keys(): if hasattr(module, function_name): functions[function_name] = \ getattr(module, function_name) pkg_name = args['package_name'] latest_target_timestamp = get_newest_modification_time(args['target_dependencies']) for ros_interface_file in args['ros_interface_files']: subfolder = os.path.basename(os.path.dirname(ros_interface_file)) extension = os.path.splitext(ros_interface_file)[1] output_path = os.path.join(args['output_dir'], subfolder) for sub in subfolders: output_path = os.path.join(output_path, sub) if extension == '.msg': spec = parse_message_file(pkg_name, ros_interface_file) generated_file = os.path.join(output_path, '%s_.idl' % spec.base_type.type) data = {'spec': spec, 'subfolder': subfolder, 'subfolders': subfolders} data.update(functions) expand_template( template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) elif extension == '.srv': srv_spec = parse_service_file(pkg_name, ros_interface_file) request_fields = [ Field( Type('uint64', context_package_name=pkg_name), 'client_guid_0', default_value_string=None), Field( Type('uint64', context_package_name=pkg_name), 'client_guid_1', default_value_string=None), Field( Type('int64', context_package_name=pkg_name), 'sequence_number', default_value_string=None), Field( Type('%s_Request' % srv_spec.srv_name, context_package_name=pkg_name), 'request', default_value_string=None) ] response_fields = [ Field( Type('uint64', context_package_name=pkg_name), 'client_guid_0', default_value_string=None), Field( Type('uint64', context_package_name=pkg_name), 'client_guid_1', default_value_string=None), Field( Type('int64', context_package_name=pkg_name), 'sequence_number', default_value_string=None), Field( Type('%s_Response' % srv_spec.srv_name, context_package_name=pkg_name), 'response', default_value_string=None) ] constants = [] sample_spec_request = MessageSpecification( srv_spec.pkg_name, 'Sample_%s_Request' % srv_spec.srv_name, request_fields, constants) sample_spec_response = MessageSpecification( srv_spec.pkg_name, 'Sample_%s_Response' % srv_spec.srv_name, response_fields, constants) generated_files = [ (sample_spec_request, os.path.join( output_path, '%s_.idl' % sample_spec_request.base_type.type)), (sample_spec_response, os.path.join( output_path, '%s_.idl' % sample_spec_response.base_type.type)), ] for spec, generated_file in generated_files: data = {'spec': spec, 'subfolder': 'srv', 'subfolders': subfolders} data.update(functions) expand_template( template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) return 0
def generate_cpp(generator_arguments_file): args = read_generator_arguments(generator_arguments_file) template_dir = args['template_dir'] mapping_msgs = { os.path.join(template_dir, 'msg__rosidl_typesupport_introspection_cpp.hpp.em'): '%s__rosidl_typesupport_introspection_cpp.hpp', os.path.join(template_dir, 'msg__type_support.cpp.em'): '%s__type_support.cpp', } mapping_srvs = { os.path.join(template_dir, 'srv__rosidl_typesupport_introspection_cpp.hpp.em'): '%s__rosidl_typesupport_introspection_cpp.hpp', os.path.join(template_dir, 'srv__type_support.cpp.em'): '%s__type_support.cpp', } for template_file in mapping_msgs.keys(): assert os.path.exists( template_file), 'Could not find template: ' + template_file for template_file in mapping_srvs.keys(): assert os.path.exists( template_file), 'Could not find template: ' + template_file pkg_name = args['package_name'] known_msg_types = extract_message_types( pkg_name, args['ros_interface_files'], args.get('ros_interface_dependencies', [])) functions = { 'get_header_filename_from_msg_name': convert_camel_case_to_lower_case_underscore, } latest_target_timestamp = get_newest_modification_time( args['target_dependencies']) for ros_interface_file in args['ros_interface_files']: extension = os.path.splitext(ros_interface_file)[1] subfolder = os.path.basename(os.path.dirname(ros_interface_file)) if extension == '.msg': spec = parse_message_file(pkg_name, ros_interface_file) validate_field_types(spec, known_msg_types) for template_file, generated_filename in mapping_msgs.items(): generated_file = os.path.join( args['output_dir'], subfolder, generated_filename % convert_camel_case_to_lower_case_underscore( spec.base_type.type)) data = { 'spec': spec, 'subfolder': subfolder, 'cpp_primitives': MSG_TYPE_TO_CPP } data.update(functions) expand_template(template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) elif extension == '.srv': spec = parse_service_file(pkg_name, ros_interface_file) validate_field_types(spec, known_msg_types) for template_file, generated_filename in mapping_srvs.items(): generated_file = os.path.join( args['output_dir'], subfolder, generated_filename % convert_camel_case_to_lower_case_underscore(spec.srv_name)) data = {'spec': spec, 'subfolder': subfolder} data.update(functions) expand_template(template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) return 0
def generate_typesupport_freertps_cpp(template_dir, pkg_name, ros_interface_files, ros_interface_dependencies, target_dependencies, additional_files, output_dir): mapping_msgs = { os.path.join(template_dir, 'msg__type_support.hpp.template'): '%s__type_support.hpp', os.path.join(template_dir, 'msg__type_support.cpp.template'): '%s__type_support.cpp', } mapping_srvs = { os.path.join(template_dir, 'srv__type_support.cpp.template'): '%s__type_support.cpp', } for template_file in mapping_msgs.keys(): assert os.path.exists(template_file), 'Could not find template: ' + template_file for template_file in mapping_srvs.keys(): assert os.path.exists(template_file), 'Could not find template: ' + template_file known_msg_types = extract_message_types( pkg_name, ros_interface_files, ros_interface_dependencies) functions = { 'get_header_filename_from_msg_name': convert_camel_case_to_lower_case_underscore, 'enforce_alignment': enforce_alignment, 'enforce_read_alignment': enforce_read_alignment, 'enforce_alignment_buffer_size': enforce_alignment_buffer_size, 'get_alignment': get_alignment, 'msg_type_to_cpp': msg_type_to_cpp, } # generate_dds_freertps_cpp() and therefore the make target depend on the additional files # therefore they must be listed here even if the generated type support files are independent latest_target_timestamp = get_newest_modification_time( target_dependencies + additional_files) for idl_file in ros_interface_files: extension = os.path.splitext(idl_file)[1] if extension == '.msg': spec = parse_message_file(pkg_name, idl_file) validate_field_types(spec, known_msg_types) subfolder = os.path.basename(os.path.dirname(idl_file)) for template_file, generated_filename in mapping_msgs.items(): generated_file = os.path.join( output_dir, subfolder, 'freertps', generated_filename % convert_camel_case_to_lower_case_underscore(spec.base_type.type)) data = {'spec': spec, 'subfolder': subfolder} data.update(functions) expand_template( template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) elif extension == '.srv': spec = parse_service_file(pkg_name, idl_file) validate_field_types(spec, known_msg_types) for template_file, generated_filename in mapping_srvs.items(): generated_file = os.path.join( output_dir, 'srv', 'freertps', generated_filename % convert_camel_case_to_lower_case_underscore(spec.srv_name)) data = {'spec': spec} data.update(functions) expand_template( template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) return 0
def generate_dds_idl(generator_arguments_file, subfolders, extension_module_name): args = read_generator_arguments(generator_arguments_file) template_file = os.path.join(args['template_dir'], 'msg.idl.template') assert os.path.exists(template_file), \ "The template '%s' does not exist" % template_file # look for extensions for the default functions functions = { 'get_include_directives': get_include_directives, 'get_post_struct_lines': get_post_struct_lines, 'msg_type_to_idl': msg_type_to_idl, } if extension_module_name is not None: pkg = __import__(extension_module_name) module_name = extension_module_name.rsplit('.', 1)[1] if hasattr(pkg, module_name): module = getattr(pkg, module_name) for function_name in functions.keys(): if hasattr(module, function_name): functions[function_name] = \ getattr(module, function_name) pkg_name = args['package_name'] latest_target_timestamp = get_newest_modification_time(args['target_dependencies']) for ros_interface_file in args['ros_interface_files']: subfolder = os.path.basename(os.path.dirname(ros_interface_file)) extension = os.path.splitext(ros_interface_file)[1] output_path = os.path.join(args['output_dir'], subfolder) for sub in subfolders: output_path = os.path.join(output_path, sub) if extension == '.msg': spec = parse_message_file(pkg_name, ros_interface_file) generated_file = os.path.join(output_path, '%s_.idl' % spec.base_type.type) data = {'spec': spec, 'subfolder': subfolder, 'subfolders': subfolders} data.update(functions) expand_template( template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) elif extension == '.srv': srv_spec = parse_service_file(pkg_name, ros_interface_file) request_fields = [ Field( Type('uint64', context_package_name=pkg_name), 'client_guid_0', default_value_string=None), Field( Type('uint64', context_package_name=pkg_name), 'client_guid_1', default_value_string=None), Field( Type('int64', context_package_name=pkg_name), 'sequence_number', default_value_string=None), Field( Type('%s_Request' % srv_spec.srv_name, context_package_name=pkg_name), 'request', default_value_string=None) ] response_fields = [ Field( Type('uint64', context_package_name=pkg_name), 'client_guid_0', default_value_string=None), Field( Type('uint64', context_package_name=pkg_name), 'client_guid_1', default_value_string=None), Field( Type('int64', context_package_name=pkg_name), 'sequence_number', default_value_string=None), Field( Type('%s_Response' % srv_spec.srv_name, context_package_name=pkg_name), 'response', default_value_string=None) ] constants = [] sample_spec_request = MessageSpecification( srv_spec.pkg_name, 'Sample_%s_Request' % srv_spec.srv_name, request_fields, constants) sample_spec_response = MessageSpecification( srv_spec.pkg_name, 'Sample_%s_Response' % srv_spec.srv_name, response_fields, constants) generated_files = [ (sample_spec_request, os.path.join( output_path, '%s_.idl' % sample_spec_request.base_type.type)), (sample_spec_response, os.path.join( output_path, '%s_.idl' % sample_spec_response.base_type.type)), ] for spec, generated_file in generated_files: data = {'spec': spec, 'subfolder': 'srv', 'subfolders': subfolders} data.update(functions) expand_template( template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) return 0
def generate_typesupport_opensplice_c(args): template_dir = args['template_dir'] mapping_msgs = { os.path.join(template_dir, 'msg__type_support_c.cpp.template'): '%s__type_support_c.cpp', } mapping_srvs = { os.path.join(template_dir, 'srv__type_support_c.cpp.template'): '%s__type_support_c.cpp', } for template_file in mapping_msgs.keys(): assert os.path.exists(template_file), 'Could not find template: ' + template_file for template_file in mapping_srvs.keys(): assert os.path.exists(template_file), 'Could not find template: ' + template_file pkg_name = args['package_name'] known_msg_types = extract_message_types( pkg_name, args['ros_interface_files'], args.get('ros_interface_dependencies', [])) functions = { 'get_header_filename_from_msg_name': convert_camel_case_to_lower_case_underscore, } # generate_dds_opensplice_cpp() and therefore the make target depend on the additional files # therefore they must be listed here even if the generated type support files are independent latest_target_timestamp = get_newest_modification_time( args['target_dependencies'] + args.get('additional_files', [])) for idl_file in args['ros_interface_files']: extension = os.path.splitext(idl_file)[1] if extension == '.msg': spec = parse_message_file(pkg_name, idl_file) validate_field_types(spec, known_msg_types) subfolder = os.path.basename(os.path.dirname(idl_file)) for template_file, generated_filename in mapping_msgs.items(): generated_file = os.path.join( args['output_dir'], subfolder, 'dds_opensplice_c', generated_filename % convert_camel_case_to_lower_case_underscore(spec.base_type.type)) data = { 'spec': spec, 'pkg': spec.base_type.pkg_name, 'msg': spec.msg_name, 'type': spec.base_type.type, 'subfolder': subfolder, } data.update(functions) try: expand_template( template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) except Exception: print("\nException when expanding '{0}' into '{1}':\n" .format(template_file, generated_file)) raise elif extension == '.srv': spec = parse_service_file(pkg_name, idl_file) validate_field_types(spec, known_msg_types) for template_file, generated_filename in mapping_srvs.items(): generated_file = os.path.join( args['output_dir'], 'srv', 'dds_opensplice_c', generated_filename % convert_camel_case_to_lower_case_underscore(spec.srv_name)) data = {'spec': spec} data.update(functions) expand_template( template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) return 0
def generate_py(generator_arguments_file, typesupport_impls): args = read_generator_arguments(generator_arguments_file) template_dir = args['template_dir'] type_support_impl_by_filename = { '_%s_s.ep.{0}.c'.format(impl): impl for impl in typesupport_impls } mapping_msgs = { os.path.join(template_dir, '_msg.py.em'): ['_%s.py'], os.path.join(template_dir, '_msg_support.c.em'): ['_%s_s.c'], } mapping_msg_pkg_extension = { os.path.join(template_dir, '_msg_pkg_typesupport_entry_point.c.em'): type_support_impl_by_filename.keys(), } mapping_srvs = { os.path.join(template_dir, '_srv.py.em'): ['_%s.py'], } mapping_actions = { os.path.join(template_dir, '_action.py.em'): ['_%s.py'], } for template_file in mapping_msgs.keys(): assert os.path.exists( template_file), 'Could not find template: ' + template_file for template_file in mapping_msg_pkg_extension.keys(): assert os.path.exists( template_file), 'Could not find template: ' + template_file for template_file in mapping_srvs.keys(): assert os.path.exists( template_file), 'Could not find template: ' + template_file for template_file in mapping_actions.keys(): assert os.path.exists( template_file), 'Could not find template: ' + template_file functions = { 'constant_value_to_py': constant_value_to_py, 'get_python_type': get_python_type, 'primitive_msg_type_to_c': primitive_msg_type_to_c, 'value_to_py': value_to_py, 'convert_camel_case_to_lower_case_underscore': convert_camel_case_to_lower_case_underscore, } latest_target_timestamp = get_newest_modification_time( args['target_dependencies']) modules = defaultdict(list) message_specs = [] service_specs = [] action_specs = [] for ros_interface_file in args['ros_interface_files']: extension = os.path.splitext(ros_interface_file)[1] subfolder = os.path.basename(os.path.dirname(ros_interface_file)) if extension == '.msg': spec = parse_message_file(args['package_name'], ros_interface_file) message_specs.append((spec, subfolder)) mapping = mapping_msgs type_name = spec.base_type.type elif extension == '.srv': spec = parse_service_file(args['package_name'], ros_interface_file) service_specs.append((spec, subfolder)) mapping = mapping_srvs type_name = spec.srv_name elif extension == '.action': spec = parse_action_file(args['package_name'], ros_interface_file) action_specs.append((spec, subfolder)) mapping = mapping_actions type_name = spec.action_name else: continue module_name = convert_camel_case_to_lower_case_underscore(type_name) modules[subfolder].append((module_name, type_name)) for template_file, generated_filenames in mapping.items(): for generated_filename in generated_filenames: data = { 'module_name': module_name, 'package_name': args['package_name'], 'spec': spec, 'subfolder': subfolder, } data.update(functions) generated_file = os.path.join(args['output_dir'], subfolder, generated_filename % module_name) expand_template(template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) for subfolder in modules.keys(): import_list = {} for module_name, type_ in modules[subfolder]: if (subfolder == 'srv' or subfolder == 'action') and \ (type_.endswith('Request') or type_.endswith('Response')): continue import_list['%s # noqa\n' % type_] = 'from %s.%s._%s import %s\n' % \ (args['package_name'], subfolder, module_name, type_) path_to_module = os.path.join(args['output_dir'], subfolder, '__init__.py') content = '' if os.path.isfile(path_to_module): with open(path_to_module, 'r') as f: content = f.read() with open(path_to_module, 'w') as f: block_name = args['package_name'] if action_specs: block_name += '_action' content = re.sub( r'# BEGIN %s$.*^# END %s' % (block_name, block_name), '', content, 0, re.M | re.S) content = re.sub(r'^\s*$', '', content, 0, re.M) content = ''.join(['# BEGIN %s\n' % block_name] + sorted(import_list.values()) + # import_line sorted(import_list.keys()) + # noqa_line ['# END %s\n' % block_name]) + content f.write(content) for template_file, generated_filenames in mapping_msg_pkg_extension.items( ): for generated_filename in generated_filenames: package_name = args['package_name'] if action_specs: package_name += '_action' data = { 'package_name': package_name, 'action_specs': action_specs, 'message_specs': message_specs, 'service_specs': service_specs, 'typesupport_impl': type_support_impl_by_filename.get(generated_filename, ''), } data.update(functions) generated_file = os.path.join(args['output_dir'], generated_filename % package_name) expand_template(template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) return 0
def generate_java(generator_arguments_file, typesupport_impl, typesupport_impls): args = read_generator_arguments(generator_arguments_file) typesupport_impls = typesupport_impls.split(';') template_dir = args['template_dir'] type_support_impl_by_filename = { '%s.ep.{0}.cpp'.format(impl): impl for impl in typesupport_impls } mapping_msgs = { os.path.join(template_dir, 'msg.java.em'): ['%s.java'], os.path.join(template_dir, 'msg.cpp.em'): type_support_impl_by_filename.keys(), } mapping_srvs = { os.path.join(template_dir, 'srv.java.em'): ['%s.java'], os.path.join(template_dir, 'srv.cpp.em'): type_support_impl_by_filename.keys(), } for template_file in mapping_msgs.keys(): assert os.path.exists(template_file), \ 'Messages template file %s not found' % template_file for template_file in mapping_srvs.keys(): assert os.path.exists(template_file), \ 'Services template file %s not found' % template_file functions = {'get_java_type': get_java_type, } latest_target_timestamp = get_newest_modification_time(args['target_dependencies']) modules = defaultdict(list) for ros_interface_file in args['ros_interface_files']: extension = os.path.splitext(ros_interface_file)[1] subfolder = os.path.basename(os.path.dirname(ros_interface_file)) if extension == '.msg': spec = parse_message_file(args['package_name'], ros_interface_file) mapping = mapping_msgs type_name = spec.base_type.type elif extension == '.srv': spec = parse_service_file(args['package_name'], ros_interface_file) mapping = mapping_srvs type_name = spec.srv_name else: continue module_name = convert_camel_case_to_lower_case_underscore(type_name) modules[subfolder].append((module_name, type_name)) package_name = args['package_name'] jni_package_name = package_name.replace('_', '_1') jni_type_name = type_name.replace('_', '_1') for template_file, generated_filenames in mapping.items(): for generated_filename in generated_filenames: data = { 'constant_value_to_java': constant_value_to_java, 'value_to_java': value_to_java, 'convert_camel_case_to_lower_case_underscore': convert_camel_case_to_lower_case_underscore, 'convert_lower_case_underscore_to_camel_case': convert_lower_case_underscore_to_camel_case, 'get_builtin_java_type': get_builtin_java_type, 'module_name': module_name, 'package_name': package_name, 'jni_package_name': jni_package_name, 'jni_type_name': jni_type_name, 'spec': spec, 'subfolder': subfolder, 'typesupport_impl': type_support_impl_by_filename.get(generated_filename, ''), 'typesupport_impls': typesupport_impls, 'type_name': type_name, } data.update(functions) generated_file = os.path.join(args['output_dir'], subfolder, generated_filename % type_name) expand_template( template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) return 0
def generate_py(generator_arguments_file, typesupport_impls): args = read_generator_arguments(generator_arguments_file) template_dir = args['template_dir'] type_support_impl_by_filename = { '_%s_s.ep.{0}.c'.format(impl): impl for impl in typesupport_impls } mapping_msgs = { os.path.join(template_dir, '_msg.py.em'): ['_%s.py'], os.path.join(template_dir, '_msg_support.c.em'): ['_%s_s.c'], } mapping_extension_msgs = { os.path.join(template_dir, '_msg_support.entry_point.c.em'): type_support_impl_by_filename.keys(), } mapping_srvs = { os.path.join(template_dir, '_srv.py.em'): ['_%s.py'], } for template_file in mapping_msgs.keys(): assert os.path.exists(template_file), 'Could not find template: ' + template_file for template_file in mapping_extension_msgs.keys(): assert os.path.exists(template_file), 'Could not find template: ' + template_file for template_file in mapping_srvs.keys(): assert os.path.exists(template_file), 'Could not find template: ' + template_file functions = { 'constant_value_to_py': constant_value_to_py, 'get_python_type': get_python_type, 'primitive_msg_type_to_c': primitive_msg_type_to_c, 'value_to_py': value_to_py, 'convert_camel_case_to_lower_case_underscore': convert_camel_case_to_lower_case_underscore, } latest_target_timestamp = get_newest_modification_time(args['target_dependencies']) modules = defaultdict(list) message_specs = [] service_specs = [] for ros_interface_file in args['ros_interface_files']: extension = os.path.splitext(ros_interface_file)[1] subfolder = os.path.basename(os.path.dirname(ros_interface_file)) if extension == '.msg': spec = parse_message_file(args['package_name'], ros_interface_file) message_specs.append((spec, subfolder)) mapping = mapping_msgs type_name = spec.base_type.type elif extension == '.srv': spec = parse_service_file(args['package_name'], ros_interface_file) service_specs.append((spec, subfolder)) mapping = mapping_srvs type_name = spec.srv_name else: continue module_name = convert_camel_case_to_lower_case_underscore(type_name) modules[subfolder].append((module_name, type_name)) for template_file, generated_filenames in mapping.items(): for generated_filename in generated_filenames: data = { 'module_name': module_name, 'package_name': args['package_name'], 'spec': spec, 'subfolder': subfolder, 'typesupport_impl': type_support_impl_by_filename.get(generated_filename, ''), } data.update(functions) generated_file = os.path.join( args['output_dir'], subfolder, generated_filename % module_name) expand_template( template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) for subfolder in modules.keys(): import_list = {} for module_name, type_ in modules[subfolder]: if subfolder == 'srv' and (type_.endswith('Request') or type_.endswith('Response')): continue import_list['%s # noqa\n' % type_] = 'from %s.%s._%s import %s\n' % \ (args['package_name'], subfolder, module_name, type_) with open(os.path.join(args['output_dir'], subfolder, '__init__.py'), 'w') as f: for import_line in sorted(import_list.values()): f.write(import_line) for noqa_line in sorted(import_list.keys()): f.write(noqa_line) for template_file, generated_filenames in mapping_extension_msgs.items(): for generated_filename in generated_filenames: data = { 'module_name': module_name, 'package_name': args['package_name'], 'message_specs': message_specs, 'service_specs': service_specs, 'typesupport_impl': type_support_impl_by_filename.get(generated_filename, ''), } data.update(functions) generated_file = os.path.join( args['output_dir'], generated_filename % args['package_name']) expand_template( template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) return 0
def generate_rs(generator_arguments_file, typesupport_impls): args = read_generator_arguments(generator_arguments_file) typesupport_impls = typesupport_impls.split(';') template_dir = args['template_dir'] type_support_impl_by_filename = { '%s_rs.ep.{0}.c'.format(impl): impl for impl in typesupport_impls } mapping_msgs = { os.path.join(template_dir, 'msg.rs.em'): ['rust/src/%s.rs'], os.path.join(template_dir, 'msg.c.em'): type_support_impl_by_filename.keys(), } mapping_srvs = { os.path.join(template_dir, 'srv.rs.em'): ['rust/src/%s.rs'], os.path.join(template_dir, 'srv.c.em'): type_support_impl_by_filename.keys(), } for template_file in mapping_msgs.keys(): assert os.path.exists(template_file), \ 'Messages template file %s not found' % template_file for template_file in mapping_srvs.keys(): assert os.path.exists(template_file), \ 'Services template file %s not found' % template_file data = { 'get_c_type': get_c_type, 'get_rs_type': get_rs_type, 'constant_value_to_rs': constant_value_to_rs, 'value_to_rs': value_to_rs, 'convert_camel_case_to_lower_case_underscore': convert_camel_case_to_lower_case_underscore, 'convert_lower_case_underscore_to_camel_case': convert_lower_case_underscore_to_camel_case, 'get_builtin_rs_type': get_builtin_rs_type, 'msg_specs': [], 'srv_specs': [], 'package_name': args['package_name'], 'typesupport_impls': typesupport_impls, } latest_target_timestamp = get_newest_modification_time( args['target_dependencies']) for ros_interface_file in args['ros_interface_files']: extension = os.path.splitext(ros_interface_file)[1] subfolder = os.path.basename(os.path.dirname(ros_interface_file)) if extension == '.msg': data['msg_specs'].append( (subfolder, parse_message_file(args['package_name'], ros_interface_file))) elif extension == '.srv': data['srv_specs'].append( (subfolder, parse_service_file(args['package_name'], ros_interface_file))) else: continue if data['msg_specs']: for template_file, generated_filenames in mapping_msgs.items(): for generated_filename in generated_filenames: generated_file = os.path.join(args['output_dir'], generated_filename % 'msg') expand_template(os.path.join(template_dir, template_file), data.copy(), generated_file, minimum_timestamp=latest_target_timestamp) if data['srv_specs']: for template_file, generated_filenames in mapping_srvs.items(): for generated_filename in generated_filenames: generated_file = os.path.join(args['output_dir'], generated_filename % 'srv') expand_template(os.path.join(template_dir, template_file), data.copy(), generated_file, minimum_timestamp=latest_target_timestamp) expand_template(os.path.join(template_dir, 'lib.rs.em'), data.copy(), os.path.join(args['output_dir'], 'rust/src/lib.rs'), minimum_timestamp=latest_target_timestamp) return 0
def generate_objc(generator_arguments_file, typesupport_impl, typesupport_impls): args = read_generator_arguments(generator_arguments_file) typesupport_impls = typesupport_impls.split(';') template_dir = args['template_dir'] type_support_impl_by_filename = { '%s_s.ep.{0}.m'.format(impl): impl for impl in typesupport_impls } mapping_msgs = { os.path.join(template_dir, 'msg.h.template'): ['%s.h'], os.path.join(template_dir, 'msg.m.template'): type_support_impl_by_filename.keys(), } mapping_srvs = { os.path.join(template_dir, 'srv.h.template'): ['%s.h'], os.path.join(template_dir, 'srv.m.template'): type_support_impl_by_filename.keys(), } for template_file in mapping_msgs.keys(): assert os.path.exists(template_file), \ 'Messages template file %s not found' % template_file for template_file in mapping_srvs.keys(): assert os.path.exists(template_file), \ 'Services template file %s not found' % template_file functions = { 'get_objc_type': get_objc_type, } latest_target_timestamp = get_newest_modification_time(args['target_dependencies']) modules = defaultdict(list) for ros_interface_file in args['ros_interface_files']: extension = os.path.splitext(ros_interface_file)[1] subfolder = os.path.basename(os.path.dirname(ros_interface_file)) output_dir = args['output_dir'] if extension == '.msg': spec = parse_message_file(args['package_name'], ros_interface_file) mapping = mapping_msgs type_name = spec.base_type.type elif extension == '.srv': spec = parse_service_file(args['package_name'], ros_interface_file) mapping = mapping_srvs type_name = spec.srv_name else: continue module_name = convert_camel_case_to_lower_case_underscore(type_name) modules[subfolder].append((module_name, type_name)) package_name = args['package_name'] jni_package_name = package_name.replace('_', '_1') jni_type_name = type_name.replace('_', '_1') for template_file, generated_filenames in mapping.items(): for generated_filename in generated_filenames: data = { 'constant_value_to_objc': constant_value_to_objc, 'convert_camel_case_to_lower_case_underscore': convert_camel_case_to_lower_case_underscore, 'get_builtin_objc_type': get_builtin_objc_type, 'module_name': module_name, 'package_name': package_name, 'jni_package_name': jni_package_name, 'jni_type_name': jni_type_name, 'spec': spec, 'subfolder': subfolder, 'typesupport_impl': type_support_impl_by_filename.get(generated_filename, ''), 'typesupport_impls': typesupport_impls, 'type_name': type_name, } data.update(functions) generated_file = os.path.join( output_dir, subfolder, generated_filename % type_name) expand_template( template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) return 0
def generate_rs(generator_arguments_file, typesupport_impls): args = read_generator_arguments(generator_arguments_file) typesupport_impls = typesupport_impls.split(';') template_dir = args['template_dir'] type_support_impl_by_filename = { '%s_rs.ep.{0}.c'.format(impl): impl for impl in typesupport_impls } mapping_msgs = { os.path.join(template_dir, 'msg.rs.em'): ['rust/src/%s.rs'], os.path.join(template_dir, 'msg.c.em'): type_support_impl_by_filename.keys(), } mapping_srvs = { os.path.join(template_dir, 'srv.rs.em'): ['rust/src/%s.rs'], os.path.join(template_dir, 'srv.c.em'): type_support_impl_by_filename.keys(), } for template_file in mapping_msgs.keys(): assert os.path.exists(template_file), \ 'Messages template file %s not found' % template_file for template_file in mapping_srvs.keys(): assert os.path.exists(template_file), \ 'Services template file %s not found' % template_file data = { 'get_c_type': get_c_type, 'get_rs_type': get_rs_type, 'constant_value_to_rs': constant_value_to_rs, 'value_to_rs': value_to_rs, 'convert_camel_case_to_lower_case_underscore': convert_camel_case_to_lower_case_underscore, 'convert_lower_case_underscore_to_camel_case': convert_lower_case_underscore_to_camel_case, 'get_builtin_rs_type': get_builtin_rs_type, 'msg_specs': [], 'srv_specs': [], 'package_name': args['package_name'], 'typesupport_impls': typesupport_impls, } latest_target_timestamp = get_newest_modification_time( args['target_dependencies']) for ros_interface_file in args['ros_interface_files']: extension = os.path.splitext(ros_interface_file)[1] subfolder = os.path.basename(os.path.dirname(ros_interface_file)) if extension == '.msg': data['msg_specs'].append((subfolder, parse_message_file( args['package_name'], ros_interface_file))) elif extension == '.srv': data['srv_specs'].append((subfolder, parse_service_file( args['package_name'], ros_interface_file))) else: continue if data['msg_specs']: for template_file, generated_filenames in mapping_msgs.items(): for generated_filename in generated_filenames: generated_file = os.path.join(args['output_dir'], generated_filename % 'msg') expand_template( os.path.join(template_dir, template_file), data.copy(), generated_file, minimum_timestamp=latest_target_timestamp) if data['srv_specs']: for template_file, generated_filenames in mapping_srvs.items(): for generated_filename in generated_filenames: generated_file = os.path.join(args['output_dir'], generated_filename % 'srv') expand_template( os.path.join(template_dir, template_file), data.copy(), generated_file, minimum_timestamp=latest_target_timestamp) expand_template( os.path.join(template_dir, 'lib.rs.em'), data.copy(), os.path.join(args['output_dir'], 'rust/src/lib.rs'), minimum_timestamp=latest_target_timestamp) return 0
def generate_typesupport_opensplice_cpp(args): template_dir = args['template_dir'] mapping_msgs = { os.path.join(template_dir, 'msg__rosidl_typesupport_opensplice_cpp.hpp.em'): '%s__rosidl_typesupport_opensplice_cpp.hpp', os.path.join(template_dir, 'msg__type_support.cpp.em'): '%s__type_support.cpp', } mapping_srvs = { os.path.join(template_dir, 'srv__rosidl_typesupport_opensplice_cpp.hpp.em'): '%s__rosidl_typesupport_opensplice_cpp.hpp', os.path.join(template_dir, 'srv__type_support.cpp.em'): '%s__type_support.cpp', } for template_file in mapping_msgs.keys(): assert os.path.exists(template_file), 'Could not find template: ' + template_file for template_file in mapping_srvs.keys(): assert os.path.exists(template_file), 'Could not find template: ' + template_file pkg_name = args['package_name'] known_msg_types = extract_message_types( pkg_name, args['ros_interface_files'], args.get('ros_interface_dependencies', [])) functions = { 'get_header_filename_from_msg_name': convert_camel_case_to_lower_case_underscore, } # generate_dds_opensplice_cpp() and therefore the make target depend on the additional files # therefore they must be listed here even if the generated type support files are independent latest_target_timestamp = get_newest_modification_time( args['target_dependencies'] + args.get('additional_files', [])) for idl_file in args['ros_interface_files']: extension = os.path.splitext(idl_file)[1] if extension == '.msg': spec = parse_message_file(pkg_name, idl_file) validate_field_types(spec, known_msg_types) subfolder = os.path.basename(os.path.dirname(idl_file)) for template_file, generated_filename in mapping_msgs.items(): generated_file = os.path.join(args['output_dir'], subfolder) if generated_filename.endswith('.cpp'): generated_file = os.path.join(generated_file, 'dds_opensplice') generated_file = os.path.join( generated_file, generated_filename % convert_camel_case_to_lower_case_underscore(spec.base_type.type)) data = {'spec': spec, 'subfolder': subfolder} data.update(functions) expand_template( template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) elif extension == '.srv': spec = parse_service_file(pkg_name, idl_file) validate_field_types(spec, known_msg_types) for template_file, generated_filename in mapping_srvs.items(): generated_file = os.path.join(args['output_dir'], 'srv') if generated_filename.endswith('.cpp'): generated_file = os.path.join(generated_file, 'dds_opensplice') generated_file = os.path.join( generated_file, generated_filename % convert_camel_case_to_lower_case_underscore(spec.srv_name)) data = {'spec': spec} data.update(functions) expand_template( template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) return 0
def generate_cpp(generator_arguments_file, type_supports): args = read_generator_arguments(generator_arguments_file) template_dir = args['template_dir'] mapping_msgs = { os.path.join(template_dir, 'msg__type_support.cpp.em'): '%s__type_support.cpp', } mapping_srvs = { os.path.join(template_dir, 'srv__type_support.cpp.em'): '%s__type_support.cpp', } mapping_actions = { os.path.join(template_dir, 'action__type_support.cpp.em'): '%s__type_support.cpp', } for template_file in mapping_msgs.keys(): assert os.path.exists( template_file), 'Could not find template: ' + template_file for template_file in mapping_srvs.keys(): assert os.path.exists( template_file), 'Could not find template: ' + template_file for template_file in mapping_actions.keys(): assert os.path.exists( template_file), 'Could not find template: ' + template_file pkg_name = args['package_name'] known_msg_types = extract_message_types( pkg_name, args['ros_interface_files'], args.get('ros_interface_dependencies', [])) functions = { 'get_header_filename_from_msg_name': convert_camel_case_to_lower_case_underscore, } latest_target_timestamp = get_newest_modification_time( args['target_dependencies']) for ros_interface_file in args['ros_interface_files']: extension = os.path.splitext(ros_interface_file)[1] subfolder = os.path.basename(os.path.dirname(ros_interface_file)) if extension == '.msg': spec = parse_message_file(pkg_name, ros_interface_file) validate_field_types(spec, known_msg_types) for template_file, generated_filename in mapping_msgs.items(): generated_file = os.path.join( args['output_dir'], subfolder, generated_filename % convert_camel_case_to_lower_case_underscore( spec.base_type.type)) data = { 'spec': spec, 'subfolder': subfolder, 'type_supports': type_supports } data.update(functions) expand_template(template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) elif extension == '.srv': spec = parse_service_file(pkg_name, ros_interface_file) validate_field_types(spec, known_msg_types) for template_file, generated_filename in mapping_srvs.items(): generated_file = os.path.join( args['output_dir'], subfolder, generated_filename % convert_camel_case_to_lower_case_underscore(spec.srv_name)) data = { 'spec': spec, 'subfolder': subfolder, 'type_supports': type_supports } data.update(functions) expand_template(template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) elif extension == '.action': spec = parse_action_file(pkg_name, ros_interface_file) # TODO(sloretz) validate field types when overall action is generated with msg and srv # https://github.com/ros2/rosidl/issues/348#issuecomment-462874513 # validate_field_types(spec, known_msg_types) for template_file, generated_filename in mapping_actions.items(): generated_file = os.path.join( args['output_dir'], subfolder, generated_filename % convert_camel_case_to_lower_case_underscore( spec.action_name)) data = {'spec': spec, 'subfolder': subfolder} data.update(functions) expand_template(template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) return 0