def check_application(client, appl_name_or_id):
    """
    Check if a given application name is a valid name or a valid
    application identifier.

    :return  a pair (application_identifier, application_name) if correct,
             raise an exception otherwise.
    """

    apps = client.call_director_get("listapp")
    app_ids = [appl['aid'] for appl in apps]

    if not apps:
        raise Exception('No existing applications.')

    if appl_name_or_id is None:
        return (None, None)

    try:
        appl_id = int(appl_name_or_id)
    except ValueError:
        app_ids = [appl['aid'] for appl in apps if appl['name'] == appl_name_or_id]
        if len(app_ids) == 0:
            raise Exception("Unknown application name '%s'" % appl_name_or_id)
        elif len(app_ids) > 1:
            raise Exception("%s applications match the application name '%s'"
                            % (len(app_ids), appl_name_or_id))
        else:
            appl_id = app_ids[0]

    if not appl_id in app_ids:
        raise Exception("Unknown application id %s" % appl_id)

    appl_name = [appl['name'] for appl in apps if appl['aid'] == appl_id][0]

    return appl_id, appl_name