"""Helper module, which provides a TemporaryFilePath() context manager"""

import contextlib
import os
import tempfile


class TemporaryFilePath:
    """Context manager class, which generates temporary file name

    Coonroraly to standard tempfile.NamedTemporaryFile(), it does not
    create file. Upon exit from the context manager block, it will
    attempt to delete the file with the generated file name.

    Example:
    -------
        >>> with TemporaryFilePath() as temp_file_name:
        >>>    with open(temp_file_name, "w") as temp_file:
        >>>        temp_file.write("some test data, which goes to the file")
        >>>        # some test code is here which reads data out of temp_file

    Args:
    ----
        suffix: If 'suffix' is not None, the file name will end with that
            suffix, otherwise there will be no suffix.
        prefix: If 'prefix' is not None, the file name will begin with that
            prefix, otherwise a default prefix is used.
        dir: If 'dir' is not None, the file will be created in that directory,
            otherwise a default directory is used.
        delete: whether the file is deleted at the end (default True)

    """

    def __init__(
        self,
        suffix: str | None = None,
        prefix: str | None = None,
        dir: str | None = None,
        delete: bool = True,
    ):
        self.suffix = suffix
        self.prefix = prefix
        self.dir = dir
        self.delete = delete

    def __enter__(self) -> str:
        """Create temporary file path

        `tempfile.NamedTemporaryFile` will create and delete a file, and
        this method only returns the filepath of the non-existing file.
        """
        with tempfile.NamedTemporaryFile(
            suffix=self.suffix,
            prefix=self.prefix,
            dir=self.dir,
        ) as file:
            self.temp_file_name = file.name

        return self.temp_file_name

    def __exit__(self, exc_type, exc_val, exc_tb):
        if self.delete:
            # Exception 'FileNotFoundError' is acceptable as user may have not
            # created the file to start with or has deleted it himself
            with contextlib.suppress(FileNotFoundError):
                os.remove(self.temp_file_name)
