from pathlib import Path
import tempfile


def make(fromlist: list, pathlike=None) -> Path:
    """Creates a file with the fromlist elements one per line.

    If *pathlike* is given, that will be the path used, otherwise
    create a temporary file and return its path.

    You're responsible for deleting it after you're done.

    You can use it like this::

        fromlist_path = fromlist.make(['a.cub', 'b.cub', 'c.cub'])
        isis.cubeit(fromlist=fromlist_path, to='stacked.cub')
        fromlist_path.unlink()

    However, using the :class:`.fromlist.temp()` context manager might
    be even more handy.
    """
    mode = "wt"
    if pathlike is None:
        filelike = tempfile.NamedTemporaryFile(mode=mode, delete=False)
    else:
        filelike = open(pathlike, mode=mode)

    print(fromlist, file=filelike)
    filelike.close()

    return Path(filelike.name)