[Checkins] SVN: z3c.extfile/trunk/src/z3c/extfile/ hopefully fixed
all stuff
Bernd Dorn
bernd.dorn at fhv.at
Thu Sep 14 04:59:27 EDT 2006
Log message for revision 70166:
hopefully fixed all stuff
Changed:
U z3c.extfile/trunk/src/z3c/extfile/adapter.py
U z3c.extfile/trunk/src/z3c/extfile/hashdir.py
U z3c.extfile/trunk/src/z3c/extfile/processor.py
U z3c.extfile/trunk/src/z3c/extfile/property.py
-=-
Modified: z3c.extfile/trunk/src/z3c/extfile/adapter.py
===================================================================
--- z3c.extfile/trunk/src/z3c/extfile/adapter.py 2006-09-14 08:38:50 UTC (rev 70165)
+++ z3c.extfile/trunk/src/z3c/extfile/adapter.py 2006-09-14 08:59:26 UTC (rev 70166)
@@ -11,6 +11,7 @@
@interface.implementer(IResult)
def ReadFileResult(f, request):
f = removeSecurityProxy(f)
+ f.seek(0)
headers = ()
if request.response.getHeader('content-length') is None:
size=len(f)
Modified: z3c.extfile/trunk/src/z3c/extfile/hashdir.py
===================================================================
--- z3c.extfile/trunk/src/z3c/extfile/hashdir.py 2006-09-14 08:38:50 UTC (rev 70165)
+++ z3c.extfile/trunk/src/z3c/extfile/hashdir.py 2006-09-14 08:59:26 UTC (rev 70166)
@@ -73,9 +73,10 @@
return ReadFile(self.getPath(digest))
-
class ReadFile(object):
+ """A lazy read file implementation"""
+
interface.implements(interfaces.IReadFile)
def __init__(self, name, bufsize=-1):
@@ -87,9 +88,8 @@
@property
def _file(self):
- if self._v_file is not None:
- if not self._v_file.closed:
- return self._v_file
+ if not self.closed:
+ return self._v_file
self._v_file = file(self.name, 'rb', self.bufsize)
return self._v_file
@@ -101,40 +101,42 @@
def __repr__(self):
return "<ReadFile named %s>" % repr(self.digest)
+ @property
+ def closed(self):
+ """like file closed, but lazy"""
+ return self._v_file is None or self._v_file.closed
+
def seek(self, offset, whence=0):
"""see file.seek"""
+ # we optimize when we have 0, 0 then we do not need to open
+ # the file if it is closed, because on the next read we are at
+ # 0
+ if offset==0 and whence==0 and self.closed:
+ return
return self._file.seek(offset, whence)
def tell(self):
"""see file.tell"""
+ if self.closed:
+ return 0
return self._file.tell()
def read(self, size=-1):
"""see file.read"""
- chunk = self._file.read(size)
- if chunk == '':
- self.close()
- return chunk
+ return self._file.read(size)
def close(self):
"""see file.close"""
- if self._v_file is not None:
- if not self._v_file.closed:
- return self._v_file.close()
+ if not self.closed:
+ self._v_file.close()
self._v_file = None
def fileno(self):
return self._file.fileno()
def __iter__(self):
- return self
+ return self._file.__iter__()
- def next(self):
- line = self._file.readline()
- if line == '':
- self.close()
- raise StopIteration
- return line
class WriteFile(object):
Modified: z3c.extfile/trunk/src/z3c/extfile/processor.py
===================================================================
--- z3c.extfile/trunk/src/z3c/extfile/processor.py 2006-09-14 08:38:50 UTC (rev 70165)
+++ z3c.extfile/trunk/src/z3c/extfile/processor.py 2006-09-14 08:59:26 UTC (rev 70166)
@@ -51,7 +51,6 @@
self.handle(line, out)
def handle_first_boundary(self, line, out):
- #print "handle_first_boundary", line
self._boundary = line
self._last_boundary = self._boundary.rstrip() + '--\r\n'
self.init_headers()
@@ -65,8 +64,6 @@
self._content_type_options = {}
def handle_headers(self, line, out):
- #print "handle_headers_boundary", line
-
if line in ['\n', '\r\n']:
out.write(line)
self.init_data(out)
@@ -105,7 +102,6 @@
self.handle = None # shouldn't be called again
def handle_file_data(self, line, out):
- #print "handle_file_data", len(line)
def _end():
# write last line, but without \r\n
self._f.write(self._previous_line[:-2])
@@ -115,22 +111,15 @@
out.write(line)
self._f = None
self._dt_start = time.time()
- #print "done file: %s in %s seconds" % (
- # self._disposition_options.get('filename'),
- # (time.time()-self._dt_start))
if line == self._boundary:
- #print "end of handle_file_boundary", line
- # write last line, but without \r\n
_end()
self.handle = self.handle_headers
elif line == self._last_boundary:
- #print "last boundary handle_file_data", len(line)
_end()
self._f = None
self.handle = None # shouldn't be called again
else:
- #print "normal handle_file_data", len(line)
if self._previous_line is not None:
self._f.write(self._previous_line)
self._previous_line = line
Modified: z3c.extfile/trunk/src/z3c/extfile/property.py
===================================================================
--- z3c.extfile/trunk/src/z3c/extfile/property.py 2006-09-14 08:38:50 UTC (rev 70165)
+++ z3c.extfile/trunk/src/z3c/extfile/property.py 2006-09-14 08:59:26 UTC (rev 70166)
@@ -29,8 +29,6 @@
digest = inst.__dict__.get(self.__name, _marker)
if digest is _marker:
return None
-
-
if not hasattr(_storage, 'dataManager'):
_storage.dataManager = ReadFileDataManager()
txn = transaction.manager.get()
@@ -52,8 +50,18 @@
while True:
chunk = value.read(BLOCK_SIZE)
if not chunk:
- digest = f.commit()
- inst.__dict__[self.__name] = digest
+ newDigest = f.commit()
+ oldDigest = inst.__dict__.get(self.__name, _marker)
+ if newDigest == oldDigest:
+ # we have no change, so we have to seek to zero
+ # because this is normal behaviour when setting a
+ # new value
+ if hasattr(_storage, 'dataManager'):
+ if newDigest in _storage.dataManager.files:
+ f = _storage.dataManager.files[newDigest]
+ f.seek(0)
+ else:
+ inst.__dict__[self.__name] = newDigest
break
f.write(chunk)
@@ -78,8 +86,6 @@
return self.files[digest]
def _close(self):
- import logging
- logging.info('RFD.colse %r' % self.files.keys())
for f in self.files.values():
f.close()
More information about the Checkins
mailing list