1. David Larlet
  2. django-storages


django-storages / storages / tempfile.py

from tempfile import TemporaryFile, template

    from cStringIO import StringIO
except ImportError:
    from StringIO import StringIO

class SpooledTemporaryFile:
    Temporary file wrapper, specialized to switch from
    StringIO to a real file when it exceeds a certain size or
    when a fileno is needed.

    Copied from Python 2.7.1
    _rolled = False

    def __init__(self, max_size=0, mode='w+b', bufsize=-1,
                 suffix="", prefix=template, dir=None):
        self._file = StringIO()
        self._max_size = max_size
        self._rolled = False
        self._TemporaryFileArgs = (mode, bufsize, suffix, prefix, dir)

    def _check(self, file):
        if self._rolled: return
        max_size = self._max_size
        if max_size and file.tell() > max_size:

    def rollover(self):
        if self._rolled: return
        file = self._file
        newfile = self._file = TemporaryFile(*self._TemporaryFileArgs)
        del self._TemporaryFileArgs

        newfile.seek(file.tell(), 0)

        self._rolled = True

    # The method caching trick from NamedTemporaryFile
    # won't work here, because _file may change from a
    # _StringIO instance to a real file. So we list
    # all the methods directly.

    # Context management protocol
    def __enter__(self):
        if self._file.closed:
            raise ValueError("Cannot enter context with closed file")
        return self

    def __exit__(self, exc, value, tb):

    # file protocol
    def __iter__(self):
        return self._file.__iter__()

    def close(self):

    def closed(self):
        return self._file.closed

    def encoding(self):
        return self._file.encoding

    def fileno(self):
        return self._file.fileno()

    def flush(self):

    def isatty(self):
        return self._file.isatty()

    def mode(self):
        return self._file.mode

    def name(self):
        return self._file.name

    def newlines(self):
        return self._file.newlines

    def next(self):
        return self._file.next

    def read(self, *args):
        return self._file.read(*args)

    def readline(self, *args):
        return self._file.readline(*args)

    def readlines(self, *args):
        return self._file.readlines(*args)

    def seek(self, *args):

    def softspace(self):
        return self._file.softspace

    def tell(self):
        return self._file.tell()

    def truncate(self):

    def write(self, s):
        file = self._file
        rv = file.write(s)
        return rv

    def writelines(self, iterable):
        file = self._file
        rv = file.writelines(iterable)
        return rv

    def xreadlines(self, *args):
        return self._file.xreadlines(*args)