RFC: file-like interface for Zope File and Image
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
Zope 2 |
Invalid
|
Wishlist
|
TinoW |
Bug Description
I'd like to implement an open() method for Zopes Image and File object
such that it is usable much like standard python file objects.
This should solve the following problems:
- efficient access in python scripts w/o the need of loading
the whole data into a string using a non intuitive str(fileobject)
(which even fails with Image objects, where you need to hack
into the data structures)
- clean interface to classes deriving from these objects or
using them (e.g Foto products etc)
I propose the following interface:
handle=
while it should default to "r" for text/* files
and "rb" for any other mime types.
handle.
handle.read([size])
should we make size a sensible default here?
handle.write(), handle.truncate(), handle.writeline, ... etc.
readblock()
File-Objects use a pdata structure which is 128K block, readblock
would just read to the end of the current block and sets cursor
to the next block if any.
handle.next()
now thats interesting - python handles this with readline()
all the time. I'd like to use readline() for text/*
files and for all others rather use readblock() so its
easy to write:
for block in handle:
dosomething
in a most efficient way instead of:
while True:
block=
if not block:
break
dosomething
or even worser with read(128*1024)
I'd implement write() as truncating the file
from that point and write new blocks as
the data grows unless someone objects.
discussion leads to the idea of having a blockiterator
like (readblock,) readblocks, xreadblocks
which return an ilterable. instead of changing the
behavior of next() for binary files.