def _parse_and_validate_extension(path_filepath, extension, extensions_map):
    r"""
    If an extension is given, validate that the given file path matches
    the given extension.

    If not, parse the file path and return a correct extension. This function
    will handle cases such as file names with periods in.

    Parameters
    ----------
    path_filepath : `Path`
        The file path (normalised).
    extension : `str`
        The extension provided by the user.
    extensions_map : `dict` of `str` -> `callable`
        A dictionary mapping extensions to export callables.

    Returns
    -------
    norm_extension : `str`
        The correct extension, with leading period.

    Raises
    ------
    ValueError
        Unknown extension.
    ValueError
        File path contains extension that does not EXACTLY match the users'
        provided extension.
    """
    # If an explicit extension is passed, it must match exactly. However, file
    # names may contain periods, and therefore we need to try and parse
    # a known extension from the given file path.
    suffixes = path_filepath.suffixes
    i = 1
    while i < len(suffixes) + 1:
        try:
            suffix = ''.join(suffixes[-i:])
            _extension_to_export_function(suffix, extensions_map)
            known_extension = suffix
            break
        except ValueError:
            pass
        i += 1
    else:
        raise ValueError('Unknown file extension passed: ({})'.format(
            ''.join(suffixes)))

    if extension is not None:
        extension = _normalise_extension(extension)
        if extension != known_extension:
            raise ValueError('The file path extension must match the '
                             'requested file extension: ({}) != ({}).'.format(
                               extension, known_extension))
        known_extension = extension
    return known_extension
dataset/ETHPy150Open menpo/menpo/menpo/io/output/base.py/_parse_and_validate_extension/original