[Zope-dev] zope.filerepresentation

Martin Aspeli optilude+lists at gmail.com
Wed Sep 30 11:00:37 EDT 2009


Hi,

I'm doing some work with WebDAV representations in Zope 2, among other 
things, and I'm trying to see if we should use zope.filerepresentation 
as the central abstraction for file read/write operations.

However, I find myself lacking a couple of things:

  1) A way for an IReadFile to return the file's MIME (content) type 
(for the Content-Type, for example) and an optional filename (for the 
Conent-Disposition header, for example)

  2) A way for an IWriteFile to be told about the MIME type and encoding 
of the data it is consuming. I know that IFileFactory() contains this, 
but data is not always going to be written to a new file, so setting 
this from the factory won't work.

  3) I see this comment:

# TODO: We will add ILargeReadFile and ILargeWriteFile to efficiently
# handle large data.

But it seems this was never implemented. I need a way for the 
IWriteFile.write() to be passed a file-like object (in fact, there's not 
really any clear notion of what the 'data' parameter is, but I assume 
it's meant to be a str object), and for IReadFile.read() to return a 
file stream iterator of some kind instead of the full raw data.

I see two options here:

  - add a new plone.filerepresentation that extends these interfaces
  - commit some changes to zope.filerepresentation and make a new 
release of this

Since zope.filerepresentation is really *just* interfaces, #2 seems 
best. Any objections?

Here's an initial take on the interfaces:

class IReadFile(Interface):
     """Provide read access to file data
     """

     def read():
         """Return the file data as a str
         """

     def size():
         """Return the data length
         """

class ILargeReadFile(IReadFile):
     """Provide efficient read access to file data
     """

     def getContentType():
         """Get the content/MIME type of the file as a string in the form
         'major/minor'. Return None if this is not known.
         """

     def getFilename():
         """Get the intended file name for this file. Return None if this
         is not known.
         """


     def getIterator():
         """Return an iterable of the file data
         """

class IWriteFile(Interface):

     def write(data):
         """Update the file data
         """

class ILargeWriteFile(IWriteFile):

     def writeFile(data, contentType=None, encoding=None):
         """Update the file data.

         data is a file-like object containing input data

         contentType is the content/MIME type of the data as a string in
         the form 'major/minor'. It may be None if unknown.

         encoding is the encoding of the string. It may be None
         if unknown.
         """

This should be 100% backwards compatible. The downside is that people 
will need to implement both write() and writeFile() / read() and 
getIterator() to support both str based and stream based reading and 
writing, but that's easily addressed with a StringIO.

And of course, nothing in the ZTK will actually use these new 
interfaces, but that could come later. ;)

Martin


-- 
Author of `Professional Plone Development`, a book for developers who
want to work with Plone. See http://martinaspeli.net/plone-book



More information about the Zope-Dev mailing list