import unittest


def config_is_available(config_section, config_names, config_read,
                        set_wps_host=False):
    """Check if a config section & parameters are available for tests.

    Parameters
    ----------
    config_section : string
        section of a cfg file.
    config_names : list of string
        name of parameters to check.
    config_read : result from read method of ConfigParser.RawConfigParser
    set_wps_host : bool
        whether to set a default wps_host in the output config dictionary,
        if there is already one, it is not overwritten.

    Returns
    -------
    out : dict
        dictionary of parameter:value for all parameters of the given section

    """

    if not hasattr(config_names, '__iter__'):
        config_names = [config_names]
    if config_section not in config_read.sections():
        raise unittest.SkipTest(
            "{0} section not defined in config.".format(config_section))
    section = config_read.items(config_section)
    section_d = {}
    for item in section:
        section_d[item[0]] = item[1]
    for config_name in config_names:
        if (config_name not in section_d) or (not section_d[config_name]):
            raise unittest.SkipTest(
                "{0} not defined in config.".format(config_name))

    if set_wps_host:
        if 'wps_host' in section_d:
            # wps_host might be set, but empty. If that's the case, set to None
            if not section_d['wps_host']:
                section_d['wps_host'] = None
        else:
            section_d['wps_host'] = None

    return section_d