3
from django.utils.encoding import smart_str, smart_unicode
6
from cStringIO import StringIO
8
from StringIO import StringIO
11
DEFAULT_CHUNK_SIZE = 64 * 2**10
13
def __init__(self, file):
15
self._name = file.name
16
self._mode = file.mode
20
return smart_str(self.name or '')
22
def __unicode__(self):
23
return smart_unicode(self.name or u'')
26
return "<%s: %s>" % (self.__class__.__name__, self or "None")
28
def __nonzero__(self):
29
return not not self.name
36
name = property(_get_name)
40
mode = property(_get_mode)
42
def _get_closed(self):
44
closed = property(_get_closed)
47
if not hasattr(self, '_size'):
48
if hasattr(self.file, 'size'):
49
self._size = self.file.size
50
elif os.path.exists(self.file.name):
51
self._size = os.path.getsize(self.file.name)
53
raise AttributeError("Unable to determine the file's size.")
56
def _set_size(self, size):
59
size = property(_get_size, _set_size)
61
def chunks(self, chunk_size=None):
63
Read the file and yield chucks of ``chunk_size`` bytes (defaults to
64
``UploadedFile.DEFAULT_CHUNK_SIZE``).
67
chunk_size = self.__class__.DEFAULT_CHUNK_SIZE
69
if hasattr(self, 'seek'):
71
# Assume the pointer is at zero...
75
yield self.read(chunk_size)
78
def multiple_chunks(self, chunk_size=None):
80
Returns ``True`` if you can expect multiple chunks.
82
NB: If a particular file representation is in memory, subclasses should
83
always return ``False`` -- there's no good reason to read from memory in
87
chunk_size = self.DEFAULT_CHUNK_SIZE
88
return self.size > chunk_size
94
return list(self.xreadlines())
97
# Iterate over this file-like object by newlines
99
for chunk in self.chunks():
100
chunk_buffer = StringIO(chunk)
102
for line in chunk_buffer:
104
line = buffer_ + line
107
# If this is the end of a line, yield
108
# otherwise, wait for the next round
109
if line[-1] in ('\n', '\r'):
114
if buffer_ is not None:
117
def open(self, mode=None):
120
elif os.path.exists(self.file.name):
121
self.file = open(self.file.name, mode or self.file.mode)
123
raise ValueError("The file cannot be reopened.")
125
def seek(self, position):
126
self.file.seek(position)
129
return self.file.tell()
131
def read(self, num_bytes=None):
132
if num_bytes is None:
133
return self.file.read()
134
return self.file.read(num_bytes)
136
def write(self, content):
137
if not self.mode.startswith('w'):
138
raise IOError("File was not opened with write access.")
139
self.file.write(content)
142
if not self.mode.startswith('w'):
143
raise IOError("File was not opened with write access.")
150
class ContentFile(File):
152
A File-like object that takes just raw content, rather than an actual file.
154
def __init__(self, content):
155
self.file = StringIO(content or '')
156
self.size = len(content or '')
163
def __nonzero__(self):
166
def open(self, mode=None):