# -*- coding: utf-8 -*-
try:
    # Ideally we subclass `_TemporaryFileWrapper` to present a clear __repr__
    # for downloaded files.
    from tempfile import _TemporaryFileWrapper

    class DownloadedFile(_TemporaryFileWrapper):
        basename = None

        def __repr__(self):
            state = "closed" if self.closed else "open"
            mode = "" if self.closed else " '%s'" % self.file.mode
            return "<DownloadedFile '%s', %s%s>" % (self.name, state, mode)

        def __str__(self):
            return self.__repr__()

except ImportError:
    # On some platforms (eg GAE) the private _TemporaryFileWrapper may not be
    # available, just use the standard `NamedTemporaryFile` function
    # in this case.
    import tempfile

    DownloadedFile = tempfile.NamedTemporaryFile
