import pathlib
import click


def validate_keep_media_path(ctx, param, value):
    """
    Validate the path used to set the 'keep_media' option
    """
    if value is None or pathlib.Path(value).exists():
        return value
    else:
        path = pathlib.Path(value)
        pathlib.Path(path).mkdir(parents=True, exist_ok=True)
        if path.exists():
            return path
        else:
            raise click.BadParameter("provide a valid path for media")