示例#1
0
def generate_c(generator_arguments_file):
    args = read_generator_arguments(generator_arguments_file)

    template_dir = args['template_dir']
    mapping = {
        os.path.join(template_dir, 'msg.h.template'): '%s.h',
        os.path.join(template_dir, 'msg__functions.c.template'): '%s__functions.c',
        os.path.join(template_dir, 'msg__functions.h.template'): '%s__functions.h',
        os.path.join(template_dir, 'msg__struct.h.template'): '%s__struct.h',
        os.path.join(template_dir, 'msg__type_support.h.template'): '%s__type_support.h',
    }
    for template_file in mapping.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.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)
    return 0
示例#2
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.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
示例#3
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
示例#4
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
示例#5
0
def generate_cpp(args, message_specs, service_specs, known_msg_types):
    template_dir = args['template_dir']
    mapping_msgs = {
        os.path.join(template_dir, 'msg__type_support.hpp.em'): '%s__type_support.hpp',
        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',
        os.path.join(template_dir, 'srv__type_support.hpp.em'):
        '%s__type_support.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,
    }
    # generate_dds_connext_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, spec in message_specs:
        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_connext', 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)

    for spec in service_specs:
        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_connext', 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
示例#6
0
def generate_swift(generator_arguments_file, typesupport_impls):
    mapping = {
        'idl.swift.em': '%s.swift',
        'idl.c.em': '%s.c',
        'idl.h.em': 'rclswift_%s.h',
    }
    
    generated_files = generate_files(generator_arguments_file, mapping)
    header_files = list(filter(lambda file: os.path.splitext(file)[1] == '.h', generated_files))
    args = read_generator_arguments(generator_arguments_file)
    data = {
        'package_name': args['package_name'],
        'headers': header_files,
    }
    template_basepath = pathlib.Path(args['template_dir'])
    generated_file = os.path.join(
                    args['output_dir'], 'module.modulemap')
    latest_target_timestamp = get_newest_modification_time(args['target_dependencies'])
    expand_template(os.path.basename('idl.modulemap.em'), data, generated_file, minimum_timestamp=latest_target_timestamp, template_basepath=template_basepath)
    return 0
示例#7
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
示例#8
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
示例#9
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.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
示例#10
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
示例#11
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
示例#12
0
def generate_py(generator_arguments_file, typesupport_impls):
    mapping = {
        '_idl.py.em': '_%s.py',
        '_idl_support.c.em': '_%s_s.c',
    }
    generate_files(generator_arguments_file, mapping)

    args = read_generator_arguments(generator_arguments_file)
    package_name = args['package_name']

    # expand init modules for each directory
    modules = {}
    idl_content = IdlContent()
    for idl_tuple in args.get('idl_tuples', []):
        idl_parts = idl_tuple.rsplit(':', 1)
        assert len(idl_parts) == 2

        idl_rel_path = pathlib.Path(idl_parts[1])
        idl_stems = modules.setdefault(str(idl_rel_path.parent), set())
        idl_stems.add(idl_rel_path.stem)

        locator = IdlLocator(*idl_parts)
        idl_file = parse_idl_file(locator)
        idl_content.elements += idl_file.content.elements

    for subfolder in modules.keys():
        with open(os.path.join(args['output_dir'], subfolder, '__init__.py'),
                  'w') as f:
            for idl_stem in sorted(modules[subfolder]):
                module_name = '_' + \
                    convert_camel_case_to_lower_case_underscore(idl_stem)
                f.write('from {package_name}.{subfolder}.{module_name} import '
                        '{idl_stem}  # noqa: F401\n'.format_map(locals()))

    # expand templates per available typesupport implementation
    template_dir = args['template_dir']
    type_support_impl_by_filename = {
        '_%s_s.ep.{0}.c'.format(impl): impl
        for impl in typesupport_impls
    }
    mapping_msg_pkg_extension = {
        os.path.join(template_dir, '_idl_pkg_typesupport_entry_point.c.em'):
        type_support_impl_by_filename.keys(),
    }

    for template_file in mapping_msg_pkg_extension.keys():
        assert os.path.exists(
            template_file), 'Could not find template: ' + template_file

    latest_target_timestamp = get_newest_modification_time(
        args['target_dependencies'])

    for template_file, generated_filenames in mapping_msg_pkg_extension.items(
    ):
        for generated_filename in generated_filenames:
            package_name = args['package_name']
            data = {
                'package_name':
                args['package_name'],
                'content':
                idl_content,
                'typesupport_impl':
                type_support_impl_by_filename.get(generated_filename, ''),
            }
            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
示例#13
0
def generate_rs(generator_arguments_file, typesupport_impls):
    args = read_generator_arguments(generator_arguments_file)
    package_name = args['package_name']

    # expand init modules for each directory
    modules = {}
    idl_content = IdlContent()
    for idl_tuple in args.get('idl_tuples', []):
        idl_parts = idl_tuple.rsplit(':', 1)
        assert len(idl_parts) == 2

        idl_rel_path = pathlib.Path(idl_parts[1])
        idl_stems = modules.setdefault(str(idl_rel_path.parent), set())
        idl_stems.add(idl_rel_path.stem)

        locator = IdlLocator(*idl_parts)
        idl_file = parse_idl_file(locator)
        idl_content.elements += idl_file.content.elements

    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(),
    }

    # Ensure the required templates exist
    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 message in idl_content.get_elements_of_type(Message):
        data['msg_specs'].append(('msg', message))

    for service in idl_content.get_elements_of_type(Service):
        data['srv_specs'].append(('srv', service))

    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
示例#14
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):
    mapping = {
        '_idl.py.em': '_%s.py',
        '_idl_support.c.em': '_%s_s.c',
    }
    generate_files(generator_arguments_file, mapping)

    args = read_generator_arguments(generator_arguments_file)
    package_name = args['package_name']

    # expand init modules for each directory
    modules = {}
    idl_content = IdlContent()
    for idl_tuple in args.get('idl_tuples', []):
        idl_parts = idl_tuple.rsplit(':', 1)
        assert len(idl_parts) == 2

        idl_rel_path = pathlib.Path(idl_parts[1])
        idl_stems = modules.setdefault(str(idl_rel_path.parent), set())
        idl_stems.add(idl_rel_path.stem)

        locator = IdlLocator(*idl_parts)
        idl_file = parse_idl_file(locator)
        idl_content.elements += idl_file.content.elements

    # NOTE(sam): remove when a language specific name mangling is implemented

    def print_warning_if_reserved_keyword(member_name, interface_type,
                                          interface_name):
        if (keyword.iskeyword(member.name)):
            print("Member name '{}' in the {} '{}' is a "
                  'reserved keyword in Python and is not supported '
                  'at the moment. Please use a different name.'.format(
                      member_name, interface_type, interface_name),
                  file=sys.stderr)

    for message in idl_content.get_elements_of_type(Message):
        for member in message.structure.members:
            print_warning_if_reserved_keyword(
                member.name, 'message', message.structure.namespaced_type.name)

    for service in idl_content.get_elements_of_type(Service):
        for member in service.request_message.structure.members:
            print_warning_if_reserved_keyword(member.name, 'service request',
                                              service.namespaced_type.name)
        for member in service.response_message.structure.members:
            print_warning_if_reserved_keyword(member.name, 'service response',
                                              service.namespaced_type.name)

    for action in idl_content.get_elements_of_type(Action):
        for member in action.goal.structure.members:
            print_warning_if_reserved_keyword(member.name, 'action goal',
                                              action.namespaced_type.name)
        for member in action.feedback.structure.members:
            print_warning_if_reserved_keyword(member.name, 'action feedback',
                                              action.namespaced_type.name)
        for member in action.result.structure.members:
            print_warning_if_reserved_keyword(member.name, 'action result',
                                              action.namespaced_type.name)

    for subfolder in modules.keys():
        with open(os.path.join(args['output_dir'], subfolder, '__init__.py'),
                  'w') as f:
            for idl_stem in sorted(modules[subfolder]):
                module_name = '_' + \
                    convert_camel_case_to_lower_case_underscore(idl_stem)
                f.write(
                    f'from {package_name}.{subfolder}.{module_name} import '
                    f'{idl_stem}  # noqa: F401\n')

    # expand templates per available typesupport implementation
    template_dir = args['template_dir']
    type_support_impl_by_filename = {
        '_%s_s.ep.{0}.c'.format(impl): impl
        for impl in typesupport_impls
    }
    mapping_msg_pkg_extension = {
        os.path.join(template_dir, '_idl_pkg_typesupport_entry_point.c.em'):
        type_support_impl_by_filename.keys(),
    }

    for template_file in mapping_msg_pkg_extension.keys():
        assert os.path.exists(
            template_file), 'Could not find template: ' + template_file

    latest_target_timestamp = get_newest_modification_time(
        args['target_dependencies'])

    for template_file, generated_filenames in mapping_msg_pkg_extension.items(
    ):
        for generated_filename in generated_filenames:
            package_name = args['package_name']
            data = {
                'package_name':
                args['package_name'],
                'content':
                idl_content,
                'typesupport_impl':
                type_support_impl_by_filename.get(generated_filename, ''),
            }
            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
示例#16
0
def generate_cpp(args, message_specs, service_specs, known_msg_types):
    template_dir = args['template_dir']
    mapping_msgs = {
        os.path.join(template_dir, 'msg__rosidl_typesupport_coredx_cpp.hpp.em'):
        '%s__rosidl_typesupport_coredx_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_coredx_cpp.hpp.em'):
        '%s__rosidl_typesupport_coredx_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

    functions = {
        'get_header_filename_from_msg_name':
        convert_camel_case_to_lower_case_underscore,
    }
    # generate_dds_coredx_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, spec in message_specs:
        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_coredx')
            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)
            # print(" ... expanding msg " + template_file + " for idl_file "
            #     + idl_file + " to " + generated_file)
            expand_template(template_file,
                            data,
                            generated_file,
                            minimum_timestamp=latest_target_timestamp)

    for spec in service_specs:
        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_coredx')
            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)
            # print(" ... expanding srv " + template_file + " for idl_file " +
            #   idl_file + " to " + generated_file)
            expand_template(template_file,
                            data,
                            generated_file,
                            minimum_timestamp=latest_target_timestamp)

    return 0
示例#17
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
示例#18
0
def generate_rs(generator_arguments_file, typesupport_impls):
    args = read_generator_arguments(generator_arguments_file)
    package_name = args['package_name']

    # expand init modules for each directory
    modules = {}
    idl_content = IdlContent()
    dependency_packages = set()

    (Path(args['output_dir']) / 'rust/src').mkdir(parents=True, exist_ok=True)

    for dep_tuple in args.get('ros_interface_dependencies', []):
        dep_parts = dep_tuple.rsplit(':', 1)
        assert len(dep_parts) == 2
        if dep_parts[0] != package_name:
            dependency_packages.add(dep_parts[0])

    for idl_tuple in args.get('idl_tuples', []):
        idl_parts = idl_tuple.rsplit(':', 1)
        assert len(idl_parts) == 2

        idl_rel_path = pathlib.Path(idl_parts[1])
        idl_stems = modules.setdefault(str(idl_rel_path.parent), set())
        idl_stems.add(idl_rel_path.stem)

        locator = IdlLocator(*idl_parts)
        idl_file = parse_idl_file(locator)
        idl_content.elements += idl_file.content.elements

    typesupport_impls = typesupport_impls.split(';')

    template_dir = args['template_dir']

    mapping_msgs = {
        os.path.join(template_dir, 'msg.rs.em'): ['rust/src/%s.rs'],
    }

    mapping_srvs = {
        os.path.join(template_dir, 'srv.rs.em'): ['rust/src/%s.rs'],
    }

    # Ensure the required templates exist
    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_rmw_rs_type': make_get_rmw_rs_type(args['package_name']),
        'get_rs_name': get_rs_name,
        'get_idiomatic_rs_type':
        make_get_idiomatic_rs_type(args['package_name']),
        '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,
        'msg_specs': [],
        'srv_specs': [],
        'package_name': args['package_name'],
        'typesupport_impls': typesupport_impls,
    }

    latest_target_timestamp = get_newest_modification_time(
        args['target_dependencies'])

    for message in idl_content.get_elements_of_type(Message):
        data['msg_specs'].append(('msg', message))

    for service in idl_content.get_elements_of_type(Service):
        data['srv_specs'].append(('srv', service))

    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)

    cargo_toml_data = {
        'dependency_packages': dependency_packages,
        'package_name': args['package_name'],
        'package_version': args['package_version'],
    }
    expand_template(os.path.join(template_dir, 'Cargo.toml.em'),
                    cargo_toml_data,
                    os.path.join(args['output_dir'], 'rust/Cargo.toml'),
                    minimum_timestamp=latest_target_timestamp)

    expand_template(os.path.join(template_dir, 'build.rs.em'), {},
                    os.path.join(args['output_dir'], 'rust/build.rs'),
                    minimum_timestamp=latest_target_timestamp)

    return 0
示例#19
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
示例#20
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
示例#21
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
示例#22
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
示例#23
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
示例#24
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