import six


def _transform(providers, transform_fun, out_mapping_type=dict):
    """Syntactic sugar for transforming a providers dict.

    Args:
        providers: provider dictionary
        transform_fun: transform_fun takes a (vpkg, pset) mapping and runs
            it on each pair in nested dicts.
        out_mapping_type: type to be used internally on the
            transformed (vpkg, pset)

    Returns:
        Transformed mapping
    """
    def mapiter(mappings):
        if isinstance(mappings, dict):
            return six.iteritems(mappings)
        else:
            return iter(mappings)

    return dict(
        (name, out_mapping_type(
            [transform_fun(vpkg, pset) for vpkg, pset in mapiter(mappings)]
        ))
        for name, mappings in providers.items())