<commit_before>"""
Resolve environment, role, and host choices into actions.

If a user specifies only an environment, confab should target all hosts
and roles in that environment. If one or more roles -- or one or more hosts --
are specified explicilty, confab should target a subset.
"""
from warnings import warn
from confab.model import get_roles_for_host, get_hosts_for_environment


def resolve_hosts_and_roles(environment, hosts=None, roles=None):
    """
    Given an environment, (possibly empty) list of hosts, and a (possibly empty)
    list of roles, return a mapping from host to roles to target.

    Raises an exception if any targeted host would have no roles after resolution.
    """
    # Validate environment
    if not environment:
        raise Exception("Environment was not specified")

    # Determine hosts for the environment
    environment_hosts = get_hosts_for_environment(environment)
    if environment_hosts is None:
        raise Exception("Environment '{}' is not recognized.".format(environment))
    elif not environment_hosts:
        warn("Environment '{}' does not have any hosts configured.".format(environment))

    # Use all environment hosts if none are specified
    hosts = hosts or environment_hosts

    # Forbid any hosts not in environment
    for host in hosts:
        if host not in environment_hosts:
            raise Exception("Host '{host}' is not a member of the environment '{env}'"
                            .format(host=host, env=environment))

    # Determine roles for hosts
    if roles:
        # If roles were specified, restrict mapping to those roles
        valid_roles = lambda role: role in roles
        hosts_to_roles = dict([(host, filter(valid_roles, get_roles_for_host(host)))
                               for host in hosts])
    else:
        # Otherwise, use all roles
        hosts_to_roles = dict([(host, get_roles_for_host(host)) for host in hosts])

    # Validate that all hosts have at least one role
    for host, host_roles in hosts_to_roles.iteritems():
        if not host_roles:
            if roles:
                raise Exception("Host '{}' does not have any of the specified roles".format(host))
            else:
                raise Exception("Host '{}' does not have any configured roles".format(host))

    return hosts_to_roles<commit_msg>Remove host from hosts_to_roles if no roles match.<commit_after>"""
Resolve environment, role, and host choices into actions.

If a user specifies only an environment, confab should target all hosts
and roles in that environment. If one or more roles -- or one or more hosts --
are specified explicilty, confab should target a subset.
"""
from warnings import warn
from confab.model import get_roles_for_host, get_hosts_for_environment


def resolve_hosts_and_roles(environment, hosts=None, roles=None):
    """
    Given an environment, (possibly empty) list of hosts, and a (possibly empty)
    list of roles, return a mapping from host to roles to target.

    Raises an exception if any targeted host would have no roles after resolution.
    """
    # Validate environment
    if not environment:
        raise Exception("Environment was not specified")

    # Determine hosts for the environment
    environment_hosts = get_hosts_for_environment(environment)
    if environment_hosts is None:
        raise Exception("Environment '{}' is not recognized.".format(environment))
    elif not environment_hosts:
        warn("Environment '{}' does not have any hosts configured.".format(environment))

    # Use all environment hosts if none are specified
    hosts = hosts or environment_hosts

    # Forbid any hosts not in environment
    for host in hosts:
        if host not in environment_hosts:
            raise Exception("Host '{host}' is not a member of the environment '{env}'"
                            .format(host=host, env=environment))

    # Determine roles for hosts
    if roles:
        # If roles were specified, restrict mapping to those roles
        valid_roles = lambda role: role in roles
        hosts_to_roles = {}
        for host in hosts:
            host_roles = filter(valid_roles, get_roles_for_host(host))
            if host_roles:
                hosts_to_roles[host] = host_roles
    else:
        # Otherwise, use all roles
        hosts_to_roles = dict([(host, get_roles_for_host(host)) for host in hosts])

    # Validate that all hosts have at least one role
    for host, host_roles in hosts_to_roles.iteritems():
        if not host_roles:
            if roles:
                raise Exception("Host '{}' does not have any of the specified roles".format(host))
            else:
                raise Exception("Host '{}' does not have any configured roles".format(host))

    return hosts_to_roles
{'fraction_non_alphanumeric': 0.0697286012526096, 'fraction_numerical': 0.0, 'mean_word_length': 2.94320987654321, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6768033', 'n_tokens_mistral': 1252, 'n_tokens_neox': 1210, 'n_words': 512}