Given a path and options for a format (ext, suffix, prefix), return the corresponding base path

def base_path(main_path, fmt):
    """Given a path and options for a format (ext, suffix, prefix), return the corresponding base path"""
    if not fmt:
        return os.path.splitext(main_path)[0]

    fmt = long_form_one_format(fmt)
    fmt_ext = fmt['extension']
    suffix = fmt.get('suffix')
    prefix = fmt.get('prefix')

    base, ext = os.path.splitext(main_path)
    if ext != fmt_ext:
        raise InconsistentPath(u"Notebook path '{}' was expected to have extension '{}'".format(main_path, fmt_ext))

    if suffix:
        if not base.endswith(suffix):
            raise InconsistentPath(u"Notebook name '{}' was expected to end with suffix '{}'".format(base, suffix))
        base = base[:-len(suffix)]

    if not prefix:
        return base

    prefix_dir, prefix_file_name = os.path.split(prefix)
    notebook_dir, notebook_file_name = os.path.split(base)
    sep = base[len(notebook_dir):-len(notebook_file_name)]

    if prefix_file_name:
        if not notebook_file_name.startswith(prefix_file_name):
            raise InconsistentPath(u"Notebook name '{}' was expected to start with prefix '{}'"
                                   .format(notebook_file_name, prefix_file_name))
        notebook_file_name = notebook_file_name[len(prefix_file_name):]

    if prefix_dir:
        if not notebook_dir.endswith(prefix_dir):
            raise InconsistentPath(u"Notebook directory '{}' was expected to end with directory prefix '{}'"
                                   .format(notebook_dir, prefix_dir))
        notebook_dir = notebook_dir[:-len(prefix_dir)]

    if not notebook_dir:
        return notebook_file_name

    # Does notebook_dir ends with a path separator?
    if notebook_dir[-1:] == sep:
        return notebook_dir + notebook_file_name

    return notebook_dir + sep + notebook_file_name