def test_unknown_resource():
    set_ament_prefix_path(['prefix1'])
    exists = has_resource('resource_type4', 'bar')
    assert not exists, 'Resource should not exist'

    with pytest.raises(LookupError):
        get_resource('resource_type4', 'bar')
Esempio n. 2
0
def test_resource():
    set_ament_prefix_path(['prefix1'])
    exists = has_resource('resource_type4', 'foo')
    assert exists, 'Resource should exist'

    resource = get_resource('resource_type4', 'foo')
    assert resource == 'foo', 'Expected different content'
Esempio n. 3
0
def import_type_support(pkg_name, subfolder, rosidl_name, rmw_implementation):
    """Import the appropriate type support module for a given rosidl and rmw implementation.

    This function will determine the correct type support package to import
    from based on the rmw implementation given.
    For example, giving `opensplice_static` as the rmw implementation would
    result in the `rosidl_typesupport_opensplice_c` type support package being
    used when importing.

    :param pkg_name str: name of the package which contains the rosidl
    :param subfolder str: name of the rosidl containing folder, e.g. `msg`, `srv`, etc.
    :param rosidl_name str: name of the rosidl
    :param rmw_implementation str: name of the rmw implementation
    :returns: the type support Python module for this specific rosidl and rmw implementation pair
    """
    if not ament_index_python.has_resource('rmw_typesupport_c', rmw_implementation):
        raise UnsupportedTypeSupport(rmw_implementation)

    type_support_name = ament_index_python.get_resource('rmw_typesupport_c', rmw_implementation)
    import_package = '{pkg_name}'.format(
        pkg_name=pkg_name,
    )
    module_name = '.{pkg_name}_s__{type_support_name}'.format(
        pkg_name=pkg_name,
        type_support_name=type_support_name,
    )
    return importlib.import_module(module_name, package=import_package)
Esempio n. 4
0
def import_type_support(pkg_name, rmw_implementation):
    """
    Import the appropriate type support module of a package for a given rmw implementation.

    This function will determine the correct type support module to import based on the rmw
    implementation given.
    This module will provide the c type support of the given rmw implementation for the rosidl
    files in the specified package, such that the ROS message structures in the package can be
    converted to and from message structures used by the rmw implementation.

    :param pkg_name str: name of the package
    :param rmw_implementation str: name of the rmw implementation
    :returns: the type support Python module for the specified package and rmw implementation pair
    """
    if not ament_index_python.has_resource('rmw_typesupport_c',
                                           rmw_implementation):
        raise UnsupportedTypeSupport(rmw_implementation)

    type_support_name, _ = ament_index_python.get_resource(
        'rmw_typesupport_c', rmw_implementation)
    import_package = '{pkg_name}'.format(pkg_name=pkg_name, )
    module_name = '.{pkg_name}_s__{type_support_name}'.format(
        pkg_name=pkg_name,
        type_support_name=type_support_name,
    )
    return importlib.import_module(module_name, package=import_package)
Esempio n. 5
0
def import_type_support(pkg_name, rmw_implementation):
    """
    Import the appropriate type support module of a package for a given rmw implementation.

    This function will determine the correct type support module to import based on the rmw
    implementation given.
    This module will provide the c type support of the given rmw implementation for the rosidl
    files in the specified package, such that the ROS message structures in the package can be
    converted to and from message structures used by the rmw implementation.

    :param pkg_name str: name of the package
    :param rmw_implementation str: name of the rmw implementation
    :returns: the type support Python module for the specified package and rmw implementation pair
    """
    if not ament_index_python.has_resource('rmw_typesupport_c', rmw_implementation):
        raise UnsupportedTypeSupport(rmw_implementation)

    type_support_name = ament_index_python.get_resource('rmw_typesupport_c', rmw_implementation)
    import_package = '{pkg_name}'.format(
        pkg_name=pkg_name,
    )
    module_name = '.{pkg_name}_s__{type_support_name}'.format(
        pkg_name=pkg_name,
        type_support_name=type_support_name,
    )
    return importlib.import_module(module_name, package=import_package)
Esempio n. 6
0
def get_action_path(package_name, action_name):
    action_types = get_action_types(package_name)
    if action_name not in action_types:
        raise LookupError('Unknown action type')
    prefix_path = has_resource('packages', package_name)
    # TODO(jacobperron) this logic should come from a rosidl related package
    return os.path.join(prefix_path, 'share', package_name, 'action', action_name + '.action')
Esempio n. 7
0
def get_service_path(package_name, service_name):
    service_types = get_service_types(package_name)
    if service_name not in service_types:
        raise LookupError('Unknown service name')
    prefix_path = has_resource('packages', package_name)
    # TODO(dirk-thomas) this logic should come from a rosidl related package
    return os.path.join(prefix_path, 'share', package_name, 'srv',
                        service_name + '.srv')
Esempio n. 8
0
def get_message_path(package_name, message_name):
    message_types = get_message_types(package_name)
    if message_name not in message_types:
        raise LookupError('Unknown message name')
    prefix_path = has_resource('packages', package_name)
    # TODO(dirk-thomas) this logic should come from a rosidl related package
    return os.path.join(prefix_path, 'share', package_name, 'msg',
                        message_name + '.msg')
Esempio n. 9
0
def get_message_types(package_name):
    if not has_resource('packages', package_name):
        raise LookupError('Unknown package name')
    try:
        content, _ = get_resource('rosidl_interfaces', package_name)
    except LookupError:
        return []
    interface_names = content.splitlines()
    # TODO(dirk-thomas) this logic should come from a rosidl related package
    return [n[:-4] for n in interface_names if n.endswith('.msg')]
Esempio n. 10
0
def get_interface_path(parts):
    prefix_path = has_resource('packages', parts[0])
    joined = '/'.join(parts)
    if len(parts[-1].rsplit('.', 1)) == 1:
        joined += '.idl'
    interface_path = os.path.join(prefix_path, 'share', joined)
    if not os.path.exists(interface_path):
        raise LookupError(
            'Could not find the interface {!r}'.format(interface_path))
    return interface_path
Esempio n. 11
0
def get_rmw_output_filter(rmw_implementation):
    prefix_with_resource = ament_index_python.has_resource(
        'rmw_output_filter', rmw_implementation)
    if not prefix_with_resource:
        return []

    rmw_output_filter = ament_index_python.get_resource('rmw_output_filter', rmw_implementation)
    additional_filtered_prefixes = [
        str.encode(l) for l in rmw_output_filter.splitlines()]
    return additional_filtered_prefixes
Esempio n. 12
0
def get_package_component_types(*, package_name=None):
    """
    Get all component types registered in the ament index for the given package.

    :param package_name: whose component types are to be retrieved.
    :return: a list of component type names.
    """
    if not has_resource(COMPONENTS_RESOURCE_TYPE, package_name):
        return []
    component_registry, _ = get_resource(COMPONENTS_RESOURCE_TYPE, package_name)
    return [line.split(';')[0] for line in component_registry.splitlines()]
Esempio n. 13
0
def get_interface(package_name):
    if not has_resource('packages', package_name):
        raise LookupError('Unknown package {}'.format(package_name))
    try:
        content, _ = get_resource('rosidl_interfaces', package_name)
    except LookupError:
        return []
    interface_names = content.splitlines()
    return list(
        sorted({n.rsplit('.', 1)[0]
                for n in interface_names if '_' not in n}))
Esempio n. 14
0
def test_unknown_resource():
    set_ament_prefix_path(['prefix1'])
    exists = has_resource('resource_type4', 'bar')
    assert not exists, 'Resource should not exist'

    try:
        get_resource('resource_type4', 'bar')
        assert False, 'get_resource() failed to raise exception'
    except LookupError:
        pass
    except Exception as e:
        assert False, 'get_resource() raised wrong exception: ' + type(e)
Esempio n. 15
0
def test_unknown_resource():
    set_ament_prefix_path(['prefix1'])
    exists = has_resource('resource_type4', 'bar')
    assert not exists, 'Resource should not exist'

    try:
        get_resource('resource_type4', 'bar')
        assert False, 'get_resource() failed to raise exception'
    except LookupError:
        pass
    except Exception as e:
        assert False, 'get_resource() raised wrong exception: ' + type(e)
Esempio n. 16
0
def get_service_types(package_name):
    if not has_resource('packages', package_name):
        raise LookupError('Unknown package name')
    try:
        content, _ = get_resource('rosidl_interfaces', package_name)
    except LookupError:
        return []
    interface_names = content.splitlines()
    # TODO(dirk-thomas) this logic should come from a rosidl related package
    # Only return services in srv folder
    return [
        n[4:-4] for n in interface_names
        if n.startswith('srv/') and n.endswith('.srv')
    ]
Esempio n. 17
0
def get_action_types(package_name):
    if not has_resource('packages', package_name):
        raise LookupError('Unknown package name')
    try:
        content, _ = get_resource('rosidl_interfaces', package_name)
    except LookupError:
        return []
    interface_names = content.splitlines()
    # TODO(jacobperron) this logic should come from a rosidl related package
    # Only return actions in action folder
    return list(sorted({
        n[7:-7]
        for n in interface_names
        if n.startswith('action/') and n[-7:] in ('.idl', '.action')}))
Esempio n. 18
0
def _get_interfaces(package_names: Iterable[str] = []) -> Dict[str, List[str]]:
    interfaces = {}
    if len(package_names) == 0:
        package_names = get_resources('rosidl_interfaces')

    for package_name in package_names:
        if not has_resource('packages', package_name):
            raise LookupError(f"Unknown package '{package_name}'")
        try:
            content, _ = get_resource('rosidl_interfaces', package_name)
        except LookupError:
            continue
        interfaces[package_name] = content.splitlines()
    return interfaces
Esempio n. 19
0
def get_message_types(package_name):
    if not has_resource('packages', package_name):
        raise LookupError('Unknown package name')
    try:
        content, _ = get_resource('rosidl_interfaces', package_name)
    except LookupError:
        return []
    interface_names = content.splitlines()
    # TODO(dirk-thomas) this logic should come from a rosidl related package
    # Only return messages in msg folder
    return {
        n[4:-4]
        for n in interface_names
        if n.startswith('msg/') and n[-4:] in ('.idl', '.msg')
    }
Esempio n. 20
0
def get_rmw_output_filter(rmw_implementation, filter_type):
    supported_filter_types = ['prefixes', 'patterns']
    if filter_type not in supported_filter_types:
        raise TypeError(
            'Unsupported filter_type "{0}". Supported types: {1}'.format(
                filter_type, supported_filter_types))
    resource_name = 'rmw_output_' + filter_type
    prefix_with_resource = ament_index_python.has_resource(
        resource_name, rmw_implementation)
    if not prefix_with_resource:
        return []

    # Treat each line of the resource as an independent filter.
    rmw_output_filter, _ = ament_index_python.get_resource(
        resource_name, rmw_implementation)
    return [str.encode(l) for l in rmw_output_filter.splitlines()]
Esempio n. 21
0
    def _get_message_types(self, package_name):
        """
        Implementation taken from ros2cli.

        https://github.com/ros2/ros2cli/blob/master/ros2msg/ros2msg/api/__init__.py
        """
        if not has_resource('packages', package_name):
            raise LookupError('Unknown package name "{}"'.format(package_name))
        try:
            content, _ = get_resource('rosidl_interfaces', package_name)
        except LookupError:
            return []
        interface_names = content.splitlines()
        # TODO(dirk-thomas) this logic should come from a rosidl related package
        # Only return messages in msg folder
        return [
            n[4:-4] for n in interface_names
            if n.startswith('msg/') and n.endswith('.msg')
        ]
Esempio n. 22
0
def get_interface_path(interface_name: str) -> str:
    """
    Get the path to an interface definition file.

    :param interface_name: The name of the interface (e.g. builtin_interfaces/msg/Time.msg)
      If no dot-separated suffix is provided, then it is inferred from the namespace.
    :return: The path to the interface definition file.
    :raises ValueError: If the interface name is malformed.
    :raises LookupError: If the package or interface cannot be found.
    """
    # Split up the name for analysis
    parts = interface_name.split('/')
    # By convention we expect there to be at least two parts to the interface
    if len(parts) < 2:
        raise ValueError(
            f"Invalid name '{interface_name}'. Expected at least two parts separated by '/'"
        )
    if not all(parts):
        raise ValueError(
            f"Invalid name '{interface_name}'. Must not contain empty parts")
    if '..' in parts:
        raise ValueError(
            f"Invalid name '{interface_name}'. Must not contain '..'")
    # By convention we expect the first part to be the package name
    prefix_path = has_resource('packages', parts[0])
    if not prefix_path:
        raise LookupError(f"Unknown package '{parts[0]}'")

    interface_path = os.path.join(prefix_path, 'share', interface_name)
    # Check if there is a dot-separated suffix
    if len(parts[-1].rsplit('.', 1)) == 1:
        # If there is no suffix, try appending parent namespace (e.g. '.msg', '.srv', '.action')
        interface_path_with_suffix = interface_path + '.' + parts[-2]
        if os.path.exists(interface_path_with_suffix):
            return interface_path_with_suffix
        # Finally, try appending '.idl'
        interface_path += '.idl'

    if not os.path.exists(interface_path):
        raise LookupError(f"Could not find the interface '{interface_path}'")
    return interface_path