def filter_by_resources(ctrl_list, resources, hardware_interface=None,
    match_any=False):
    """
    Filter controller state list by claimed resources.

    @param ctrl_list: Controller state list
    @type ctrl_list: [controller_manager_msgs/ControllerState]
    @param resources: Controller resources
    @type resources: [str]
    @param hardware_interface Controller hardware interface where to look for
    resources. If specified, the requested resources will only be searched for
    in this interface. If unspecified, all controller hardware interfaces will
    be searched for; i.e., if a controller claims resources from multiple
    interfaces, the method will succeed if _any_ interface contains the
    requested resources (any or all, depending on the value of C{match_any}).
    Specifying this parameter allows finer control over determining which
    interfaces claim specific resources.
    @param match_any: If set to False, all elements in C{resources} must
    be claimed by the interface specified in C{hardware_interface} (or _any_
    interface, if C{hardware_interface} is unspecified) for a positive match.
    Note that a controller's resources can contain additional entries than
    those in C{resources}).
    If set to True, at least one element in C{resources} must be claimed by
    the interface specified in C{hardware_interface} (or _any_ interface, if
    C{hardware_interface} is unspecified) for a positive match.
    @type match_any: bool
    @return: Controllers matching the specified hardware interface
    @rtype: [controller_manager_msgs/ControllerState]
    """
    list_out = []
    for ctrl in ctrl_list:
        for resource_set in ctrl.claimed_resources:
            if (hardware_interface is None or hardware_interface ==
                resource_set.hardware_interface):
                for res in resources:
                    add_ctrl = not match_any
                    if res in resource_set.resources:
                        if match_any:
                            add_ctrl = True
                            break
                    elif not match_any:
                        add_ctrl = False
                        break
                if add_ctrl:
                    list_out.append(ctrl)
                    break
    return list_out
