from typing import Union
import pathlib


def get_anki_backup_folder(
    path: Union[str, pathlib.PurePath], nexist="raise"
) -> pathlib.Path:
    """ Return path to Anki backup folder.

    Args:
        path: Path to Aki database as :class:`pathlib.Path`
        nexist: What to do if backup folder doesn't seem to exist: ``raise`` or
            ``ignore``.

    Returns:
        Path to Anki backup folder as :class:`pathlib.Path`.
    """
    path = pathlib.Path(path)
    if not path.is_file():
        raise FileNotFoundError(
            f"Database path {path} seems to be invalid."
        )
    backup_folder = path.parent / "backups"
    if nexist == "raise" and not backup_folder.is_dir():
        raise ValueError(
            "Anki backup folder corresponding to database at {} doesn't seem"
            " to exist. Perhaps you can specify a custom back "
            "folder?".format(path)
        )
    return backup_folder