import logging


def _test_usability(imported_module, entity_type, entity_ids_arr):
    """
    This returns None if a module of a script is usable, otherwise an error message which explains
    why this script cannot be used in this context: Wrong platform, unavailable resources etc...
    """

    # PROBLEM: When an entire directory is not Usable because the file __init__.py
    # has a function Usable which returns False, then it still displays a directory, alone.
    # Unusable scripts are not displayed in the menu of the scripts of an entity,
    # except if a special flag is given, and in this case these error messages are displayed.
    try:
        is_usable = imported_module.Usable(entity_type, entity_ids_arr)
    except AttributeError as exc:
        logging.info("entity_type=%s entity_ids_arr=%s module %s has no Usable attribute: Caught %s",
                        entity_type, str(entity_ids_arr), imported_module.__name__, exc)
        return None
    except Exception as exc:
        logging.error("entity_type=%s entity_ids_arr=%s module %s: Caught %s",
                        entity_type, str(entity_ids_arr), imported_module.__name__, exc)
        return None

    if is_usable:
        return None

    error_msg = imported_module.Usable.__doc__
    if error_msg:
        error_msg = error_msg.strip()
        # Just take the first line of __doc__ string.
        error_msg = error_msg.split("\n")[0]
    else:
        error_msg = imported_module.__name__
        if error_msg:
            error_msg += " not usable"
        else:
            error_msg = "No message"
    return error_msg