[Checkins] SVN: zope.file/branches/ajung-blobs/src/zope/file/
savepoint
Andreas Jung
andreas at andreas-jung.com
Mon Feb 26 16:09:34 EST 2007
Log message for revision 72838:
savepoint
Changed:
U zope.file/branches/ajung-blobs/src/zope/file/README.txt
U zope.file/branches/ajung-blobs/src/zope/file/adapters.py
U zope.file/branches/ajung-blobs/src/zope/file/configure.zcml
U zope.file/branches/ajung-blobs/src/zope/file/file.py
U zope.file/branches/ajung-blobs/src/zope/file/interfaces.py
U zope.file/branches/ajung-blobs/src/zope/file/upload.py
-=-
Modified: zope.file/branches/ajung-blobs/src/zope/file/README.txt
===================================================================
--- zope.file/branches/ajung-blobs/src/zope/file/README.txt 2007-02-26 20:06:02 UTC (rev 72837)
+++ zope.file/branches/ajung-blobs/src/zope/file/README.txt 2007-02-26 21:09:33 UTC (rev 72838)
@@ -88,6 +88,10 @@
the file object::
>>> w.flush()
+
+We need to close the file first before determining its file size
+
+ >> w.close()
>>> f.size
19
Modified: zope.file/branches/ajung-blobs/src/zope/file/adapters.py
===================================================================
--- zope.file/branches/ajung-blobs/src/zope/file/adapters.py 2007-02-26 20:06:02 UTC (rev 72837)
+++ zope.file/branches/ajung-blobs/src/zope/file/adapters.py 2007-02-26 21:09:33 UTC (rev 72838)
@@ -38,7 +38,6 @@
self.context = context
def write(self, data):
- import pdb; pdb.set_trace()
f = self.context.open('w')
f.write(data)
f.close()
Modified: zope.file/branches/ajung-blobs/src/zope/file/configure.zcml
===================================================================
--- zope.file/branches/ajung-blobs/src/zope/file/configure.zcml 2007-02-26 20:06:02 UTC (rev 72837)
+++ zope.file/branches/ajung-blobs/src/zope/file/configure.zcml 2007-02-26 21:09:33 UTC (rev 72838)
@@ -26,6 +26,8 @@
</class>
<adapter factory=".browser.Sized"/>
+ <adapter factory=".adapters.ReadFileAdapter" />
+ <adapter factory=".adapters.WriteFileAdapter" />
<class class="ZODB.Blobs.Blob.BlobFile">
<require
@@ -38,6 +40,14 @@
/>
</class>
+
+ <class class=".file.Writer">
+ <require
+ permission="zope.ManageContent"
+ attributes="write close"
+ />
+ </class>
+
<!-- Subscriber to update the mimeType field on content-type
changes. -->
<subscriber
Modified: zope.file/branches/ajung-blobs/src/zope/file/file.py
===================================================================
--- zope.file/branches/ajung-blobs/src/zope/file/file.py 2007-02-26 20:06:02 UTC (rev 72837)
+++ zope.file/branches/ajung-blobs/src/zope/file/file.py 2007-02-26 21:09:33 UTC (rev 72838)
@@ -45,6 +45,7 @@
else:
parameters = dict(parameters)
self.parameters = parameters
+ self._data = Blob()
def open(self, mode="r"):
if mode.startswith("r"):
@@ -58,6 +59,8 @@
@property
def size(self):
+ if self._data == "":
+ return 0
fp = self._data.open('r')
fp.seek(0, 2)
size = int(fp.tell())
@@ -70,6 +73,7 @@
_closed = False
_sio = None
+ _write = False
# XXX Accessor objects need to have an __parent__ to support the
# security machinery, but they aren't ILocation instances since
@@ -81,14 +85,12 @@
def __init__(self, file, mode):
self.__parent__ = file
- self.mode = mode
+ self._stream = self.__parent__._data.open(mode)
def close(self):
if not self._closed:
- self._close()
+ self._stream.close()
self._closed = True
- if "_sio" in self.__dict__:
- del self._sio
def __getstate__(self):
"""Make sure the accessors can't be stored in ZODB."""
@@ -96,37 +98,12 @@
raise TypeError("%s.%s instance is not picklable"
% (cls.__module__, cls.__name__))
- _write = False
+ @property
+ def stream(self):
+ return self._stream
- def _get_stream(self):
- # get the right string io
- if self._sio is None:
- self._data = self.__parent__._data
- # create if we don't have one yet
- self._sio = Blob() # cStringIO creates immutable
- fp = self._sio.open('w')
- fp.write('')
- # instance if you pass a string, unlike StringIO :-/
- if not self._write:
- fp.write(self._data)
- fp.seek(0)
- elif self._data is not self.__parent__._data:
- # if the data for the underlying object has changed,
- # update our view of the data:
- pos = self._sio.tell()
- self._data = self.__parent__._data
- self._sio = Blob()
- fp = self._sio.open('w')
- fp.write(self._data)
- fp.seek(pos) # this may seek beyond EOF, but that appears to
- # be how it is supposed to work, based on experiments. Writing
- # will insert NULLs in the previous positions.
- return fp
- def _close(self):
- pass
-
class Reader(Accessor):
zope.interface.implements(
@@ -137,14 +114,14 @@
def read(self, size=-1):
if self._closed:
raise ValueError("I/O operation on closed file")
- return self._get_stream().read(size)
+ return self.stream.read(size)
def seek(self, offset, whence=0):
if self._closed:
raise ValueError("I/O operation on closed file")
if whence not in (0, 1, 2):
raise ValueError("illegal value for `whence`")
- self._get_stream().seek(offset, whence)
+ self.stream.seek(offset, whence)
def tell(self):
if self._closed:
@@ -166,13 +143,13 @@
if self._closed:
raise ValueError("I/O operation on closed file")
if self._sio is not None:
- self.__parent__._data = self._sio
+ self.__parent__._data = self._sio.getvalue()
self._data = self.__parent__._data
def write(self, data):
if self._closed:
raise ValueError("I/O operation on closed file")
- self._get_stream().write(data)
+ self.stream.write(data)
def _close(self):
self.flush()
Modified: zope.file/branches/ajung-blobs/src/zope/file/interfaces.py
===================================================================
--- zope.file/branches/ajung-blobs/src/zope/file/interfaces.py 2007-02-26 20:06:02 UTC (rev 72837)
+++ zope.file/branches/ajung-blobs/src/zope/file/interfaces.py 2007-02-26 21:09:33 UTC (rev 72838)
@@ -42,7 +42,23 @@
All readers and writers operate in 'binary' mode.
"""
+ def open():
+ """Return an object providing access to the file data.
+ Allowed values for `mode` are 'r' and 'rb' (read); 'w' and
+ 'wb' (write); and 'w+', 'w+b', 'wb+', 'r+', 'r+b', and 'rb+' (both).
+ Other values cause `ValueError` to be raised.
+
+ If the file is opened in read mode, an `IFileReader` is
+ returned; if opened in write mode, an `IFileWriter` is
+ returned; if in read/write, an object that implements both is
+ returned.
+
+ All readers and writers operate in 'binary' mode.
+
+ """
+
+
size = zope.schema.Int(
title=_("Size"),
description=_("Size in bytes"),
Modified: zope.file/branches/ajung-blobs/src/zope/file/upload.py
===================================================================
--- zope.file/branches/ajung-blobs/src/zope/file/upload.py 2007-02-26 20:06:02 UTC (rev 72837)
+++ zope.file/branches/ajung-blobs/src/zope/file/upload.py 2007-02-26 21:09:33 UTC (rev 72838)
@@ -158,5 +158,6 @@
ob.mimeType = mimeType
ob.parameters = {}
w = ob.open("wb")
+ import pdb; pdb.set_trace()
w.write(data)
w.close()
More information about the Checkins
mailing list