Permalink
Browse files

Fixed #7712, #9404, #10249, #10300: a light refactor and cleanup of f…

…ile storage and the `File` object. Thanks to Armin Ronacher and Alex Gaynor.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10717 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
1 parent 2af75b4 commit 68a890e79f660484d05482902663b6168f0bd71e @jacobian jacobian committed May 8, 2009
@@ -1,20 +1,23 @@
import os
-from django.utils.encoding import smart_str, smart_unicode
-
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
-class File(object):
+from django.utils.encoding import smart_str, smart_unicode
+from django.core.files.utils import FileProxyMixin
+
+class File(FileProxyMixin):
DEFAULT_CHUNK_SIZE = 64 * 2**10
- def __init__(self, file):
+ def __init__(self, file, name=None):
self.file = file
- self._name = file.name
- self._mode = file.mode
- self._closed = False
+ if name is None:
+ name = getattr(file, 'name', None)
+ self.name = name
+ self.mode = getattr(file, 'mode', None)
+ self.closed = False
def __str__(self):
return smart_str(self.name or '')
@@ -26,25 +29,11 @@ def __repr__(self):
return "<%s: %s>" % (self.__class__.__name__, self or "None")
def __nonzero__(self):
- return not not self.name
+ return bool(self.name)
def __len__(self):
return self.size
- def _get_name(self):
- if not hasattr(self, '_name'):
- raise ValueError("This operation requires the file to have a name.")
- return self._name
- name = property(_get_name)
-
- def _get_mode(self):
- return self._mode
- mode = property(_get_mode)
-
- def _get_closed(self):
- return self._closed
- closed = property(_get_closed)
-
def _get_size(self):
if not hasattr(self, '_size'):
if hasattr(self.file, 'size'):
@@ -66,7 +55,7 @@ def chunks(self, chunk_size=None):
``UploadedFile.DEFAULT_CHUNK_SIZE``).
"""
if not chunk_size:
- chunk_size = self.__class__.DEFAULT_CHUNK_SIZE
+ chunk_size = self.DEFAULT_CHUNK_SIZE
if hasattr(self, 'seek'):
self.seek(0)
@@ -89,12 +78,6 @@ def multiple_chunks(self, chunk_size=None):
chunk_size = self.DEFAULT_CHUNK_SIZE
return self.size > chunk_size
- def xreadlines(self):
- return iter(self)
-
- def readlines(self):
- return list(self.xreadlines())
-
def __iter__(self):
# Iterate over this file-like object by newlines
buffer_ = None
@@ -121,43 +104,22 @@ def open(self, mode=None):
self.seek(0)
elif os.path.exists(self.file.name):
self.file = open(self.file.name, mode or self.file.mode)
+ self.closed = False
else:
raise ValueError("The file cannot be reopened.")
- def seek(self, position):
- self.file.seek(position)
-
- def tell(self):
- return self.file.tell()
-
- def read(self, num_bytes=None):
- if num_bytes is None:
- return self.file.read()
- return self.file.read(num_bytes)
-
- def write(self, content):
- if not self.mode.startswith('w'):
- raise IOError("File was not opened with write access.")
- self.file.write(content)
-
- def flush(self):
- if not self.mode.startswith('w'):
- raise IOError("File was not opened with write access.")
- self.file.flush()
-
def close(self):
self.file.close()
- self._closed = True
+ self.closed = True
class ContentFile(File):
"""
A File-like object that takes just raw content, rather than an actual file.
"""
def __init__(self, content):
- self.file = StringIO(content or '')
- self.size = len(content or '')
- self.file.seek(0)
- self._closed = False
+ content = content or ''
+ super(ContentFile, self).__init__(StringIO(content))
+ self.size = len(content)
def __str__(self):
return 'Raw content'
@@ -166,6 +128,6 @@ def __nonzero__(self):
return True
def open(self, mode=None):
- if self._closed:
- self._closed = False
+ if self.closed:
+ self.closed = False
self.seek(0)
@@ -11,11 +11,12 @@
import os
import tempfile
+from django.core.files.utils import FileProxyMixin
__all__ = ('NamedTemporaryFile', 'gettempdir',)
if os.name == 'nt':
- class TemporaryFile(object):
+ class TemporaryFile(FileProxyMixin):
"""
Temporary file object constructor that works in Windows and supports
reopening of the temporary file in windows.
@@ -48,12 +49,6 @@ def close(self):
def __del__(self):
self.close()
- # Proxy to the file object.
- def __getattr__(self, name):
- return getattr(self.file, name)
- def __iter__(self):
- return iter(self.file)
-
NamedTemporaryFile = TemporaryFile
else:
NamedTemporaryFile = tempfile.NamedTemporaryFile
@@ -26,14 +26,15 @@ class UploadedFile(File):
"""
DEFAULT_CHUNK_SIZE = 64 * 2**10
- def __init__(self, name=None, content_type=None, size=None, charset=None):
- self.name = name
+ def __init__(self, file=None, name=None, content_type=None, size=None, charset=None):
+ super(UploadedFile, self).__init__(file, name)
self.size = size
self.content_type = content_type
self.charset = charset
def __repr__(self):
- return "<%s: %s (%s)>" % (self.__class__.__name__, smart_str(self.name), self.content_type)
+ return "<%s: %s (%s)>" % (
+ self.__class__.__name__, smart_str(self.name), self.content_type)
def _get_name(self):
return self._name
@@ -53,95 +54,66 @@ def _set_name(self, name):
name = property(_get_name, _set_name)
- # Abstract methods; subclasses *must* define read() and probably should
- # define open/close.
- def read(self, num_bytes=None):
- raise NotImplementedError()
-
- def open(self):
- pass
-
- def close(self):
- pass
-
class TemporaryUploadedFile(UploadedFile):
"""
A file uploaded to a temporary location (i.e. stream-to-disk).
"""
def __init__(self, name, content_type, size, charset):
- super(TemporaryUploadedFile, self).__init__(name, content_type, size, charset)
if settings.FILE_UPLOAD_TEMP_DIR:
- self._file = tempfile.NamedTemporaryFile(suffix='.upload', dir=settings.FILE_UPLOAD_TEMP_DIR)
+ file = tempfile.NamedTemporaryFile(suffix='.upload',
+ dir=settings.FILE_UPLOAD_TEMP_DIR)
else:
- self._file = tempfile.NamedTemporaryFile(suffix='.upload')
+ file = tempfile.NamedTemporaryFile(suffix='.upload')
+ super(TemporaryUploadedFile, self).__init__(file, name, content_type, size, charset)
def temporary_file_path(self):
"""
Returns the full path of this file.
"""
- return self._file.name
-
- # Most methods on this object get proxied to NamedTemporaryFile.
- # We can't directly subclass because NamedTemporaryFile is actually a
- # factory function
- def read(self, *args): return self._file.read(*args)
- def seek(self, *args): return self._file.seek(*args)
- def write(self, s): return self._file.write(s)
- def tell(self, *args): return self._file.tell(*args)
- def __iter__(self): return iter(self._file)
- def readlines(self, size=None): return self._file.readlines(size)
- def xreadlines(self): return self._file.xreadlines()
+ return self.file.name
+
def close(self):
try:
- return self._file.close()
- except OSError, e:
- if e.errno == 2:
- # Means the file was moved or deleted before the tempfile could unlink it.
- # Still sets self._file.close_called and calls self._file.file.close()
- # before the exception
- return
- else:
- raise e
+ try:
+ return self.file.close()
+ except OSError, e:
+ if e.errno != 2:
+ # Means the file was moved or deleted before the tempfile
+ # could unlink it. Still sets self.file.close_called and
+ # calls self.file.file.close() before the exception
+ raise
+ finally:
+ self.closed = True
class InMemoryUploadedFile(UploadedFile):
"""
A file uploaded into memory (i.e. stream-to-memory).
"""
def __init__(self, file, field_name, name, content_type, size, charset):
- super(InMemoryUploadedFile, self).__init__(name, content_type, size, charset)
- self._file = file
+ super(InMemoryUploadedFile, self).__init__(file, name, content_type, size, charset)
self.field_name = field_name
- self._file.seek(0)
def open(self):
- self._file.seek(0)
+ self.closed = False
+ self.file.seek(0)
def chunks(self, chunk_size=None):
- self._file.seek(0)
+ self.file.seek(0)
yield self.read()
def multiple_chunks(self, chunk_size=None):
# Since it's in memory, we'll never have multiple chunks.
return False
- # proxy methods to StringIO
- def read(self, *args): return self._file.read(*args)
- def seek(self, *args): return self._file.seek(*args)
- def tell(self, *args): return self._file.tell(*args)
- def close(self): return self._file.close()
class SimpleUploadedFile(InMemoryUploadedFile):
"""
A simple representation of a file, which just has content, size, and a name.
"""
def __init__(self, name, content, content_type='text/plain'):
- self._file = StringIO(content or '')
- self.name = name
- self.field_name = None
- self.size = len(content or '')
- self.content_type = content_type
- self.charset = None
- self._file.seek(0)
+ content = content or ''
+ super(SimpleUploadedFile, self).__init__(StringIO(content), None, name,
+ content_type, len(content), None)
def from_dict(cls, file_dict):
"""
@@ -154,5 +126,4 @@ def from_dict(cls, file_dict):
return cls(file_dict['filename'],
file_dict['content'],
file_dict.get('content-type', 'text/plain'))
-
from_dict = classmethod(from_dict)
@@ -0,0 +1,29 @@
+class FileProxyMixin(object):
+ """
+ A mixin class used to forward file methods to an underlaying file
+ object. The internal file object has to be called "file"::
+
+ class FileProxy(FileProxyMixin):
+ def __init__(self, file):
+ self.file = file
+ """
+
+ encoding = property(lambda self: self.file.encoding)
+ fileno = property(lambda self: self.file.fileno)
+ flush = property(lambda self: self.file.flush)
+ isatty = property(lambda self: self.file.isatty)
+ newlines = property(lambda self: self.file.newlines)
+ read = property(lambda self: self.file.read)
+ readinto = property(lambda self: self.file.readinto)
+ readline = property(lambda self: self.file.readline)
+ readlines = property(lambda self: self.file.readlines)
+ seek = property(lambda self: self.file.seek)
+ softspace = property(lambda self: self.file.softspace)
+ tell = property(lambda self: self.file.tell)
+ truncate = property(lambda self: self.file.truncate)
+ write = property(lambda self: self.file.write)
+ writelines = property(lambda self: self.file.writelines)
+ xreadlines = property(lambda self: self.file.xreadlines)
+
+ def __iter__(self):
+ return iter(self.file)
Oops, something went wrong. Retry.

0 comments on commit 68a890e

Please sign in to comment.