Beispiel #1
0
def generate_msg_and_srv(generator_arguments_file):
    """Write 'msg' and 'srv' files from 'action' files."""
    args = read_generator_arguments(generator_arguments_file)

    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 == '.action':
            action = parse_action_file(args['package_name'],
                                       ros_interface_file)

            # create folder if necessary
            os.makedirs(os.path.join(args['output_dir'], subfolder),
                        exist_ok=True)

            generated_folder = os.path.join(args['output_dir'], subfolder)
            for service in [action.goal_service, action.result_service]:
                srv_file = os.path.join(generated_folder,
                                        service.srv_name + '.srv')
                req_file = os.path.join(generated_folder,
                                        service.srv_name + '_Request.msg')
                rsp_file = os.path.join(generated_folder,
                                        service.srv_name + '_Response.msg')
                with open(srv_file, 'w+') as fout:
                    fout.write(str(service))
                with open(req_file, 'w+') as fout:
                    fout.write(str(service.request))
                with open(rsp_file, 'w+') as fout:
                    fout.write(str(service.response))

            generated_file = os.path.join(args['output_dir'], subfolder,
                                          action.feedback.msg_name + '.msg')
            with open(generated_file, 'w+') as fout:
                fout.write(str(action.feedback))
Beispiel #2
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
Beispiel #3
0
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',
    }
    mapping_action = {
        os.path.join(template_dir, 'action.h.em'): '%s.h',
        os.path.join(template_dir, 'action__type_support.h.em'): '%s__type_support.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, 'subfolder': subfolder}
                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)
        elif extension == '.action':
            spec = parse_action_file(args['package_name'], ros_interface_file)
            for template_file, generated_filename in mapping_action.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.action_name))
                expand_template(
                    template_file, data, generated_file,
                    minimum_timestamp=latest_target_timestamp)
    return 0
Beispiel #4
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)
            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