import gzip


def _get_open_function_from_extension(filename, kind="yaml"):
    """Returns the function open is the extension is ``kind`` or
    'gzip.open' if it is ``kind``.gz'; otherwise, raises ValueError
    """
    if filename.endswith(".{}.gz".format(kind)):
        return gzip.open
    elif filename.endswith(".{}".format(kind)):
        return open
    else:
        raise ValueError("Invalid filename. Should be .{} or .{}.gz".format(kind, kind))