[Zope3-checkins] CVS: Zope3/src/zope/app/file - configure.zcml:1.9 file.py:1.5 fssync.py:1.3 image.py:1.4 interfaces.py:1.3

Stephan Richter srichter at cosmos.phy.tufts.edu
Thu Mar 18 22:18:09 EST 2004


Update of /cvs-repository/Zope3/src/zope/app/file
In directory cvs.zope.org:/tmp/cvs-serv2488/src/zope/app/file

Modified Files:
	configure.zcml file.py fssync.py image.py interfaces.py 
Log Message:


Modernized the File and Image API to rely more heavily on schemas instead of
accessor and mutator methods. Removed a lot of unnecessary cruft from the
object classes and their views.


=== Zope3/src/zope/app/file/configure.zcml 1.8 => 1.9 ===
--- Zope3/src/zope/app/file/configure.zcml:1.8	Mon Mar 15 08:10:48 2004
+++ Zope3/src/zope/app/file/configure.zcml	Thu Mar 18 22:17:38 2004
@@ -50,20 +50,19 @@
 
   <content class=".file.File">
     <factory
-        id="File"
+        id="zope.app.content.File"
         title="File"
         description="A File"
         />
 
     <require
         permission="zope.View"
-        interface=".interfaces.IReadFile"
+        interface=".interfaces.IFile"
         />
 
     <require
         permission="zope.ManageContent"
-        interface=".interfaces.IWriteFile"
-        set_schema=".interfaces.IReadFile"
+        set_schema=".interfaces.IFile"
         />
 
     <implements
@@ -73,21 +72,19 @@
 
   <content class=".image.Image">
     <factory
-        id="Image"
+        id="zope.app.content.Image"
         title="Image"
         description="An Image"
         />
 
     <require
         permission="zope.View"
-        interface="zope.app.file.interfaces.IReadFile"
-        attributes="getImageSize"
+        interface="zope.app.file.interfaces.IImage"
         />
 
     <require
         permission="zope.ManageContent"
-        interface="zope.app.file.interfaces.IWriteFile"
-        set_schema="zope.app.file.interfaces.IReadFile"
+        set_schema="zope.app.file.interfaces.IFile"
         />
 
     <implements
@@ -119,7 +116,7 @@
       />
 
   <adapter
-      for=".interfaces.IReadFile"
+      for=".interfaces.IFile"
       provides="zope.app.index.interfaces.text.ISearchableText"
       factory=".file.SearchableText"
       />


=== Zope3/src/zope/app/file/file.py 1.4 => 1.5 ===
--- Zope3/src/zope/app/file/file.py:1.4	Fri Mar  5 17:09:04 2004
+++ Zope3/src/zope/app/file/file.py	Thu Mar 18 22:17:38 2004
@@ -20,9 +20,8 @@
 from zope.interface import implements
 
 from zope.publisher.browser import FileUpload
-from interfaces import IFile, IReadFile, IFileContent
-
-__metaclass__ = type
+from zope.app.index.interfaces.text import ISearchableText
+from interfaces import IFile, IFileContent
 
 # set the size of the chunks
 MAXCHUNKSIZE = 1 << 16
@@ -33,48 +32,42 @@
     Let's test the constructor:
 
     >>> file = File()
-    >>> file.getContentType()
+    >>> file.contentType
     ''
-    >>> file.getData()
+    >>> file.data
     ''
 
     >>> file = File('Foobar')
-    >>> file.getContentType()
+    >>> file.contentType
     ''
-    >>> file.getData()
+    >>> file.data
     'Foobar'
 
     >>> file = File('Foobar', 'text/plain')
-    >>> file.getContentType()
+    >>> file.contentType
     'text/plain'
-    >>> file.getData()
+    >>> file.data
     'Foobar'
 
     >>> file = File(data='Foobar', contentType='text/plain')
-    >>> file.getContentType()
+    >>> file.contentType
     'text/plain'
-    >>> file.getData()
+    >>> file.data
     'Foobar'
 
 
     Let's test the mutators:
 
     >>> file = File()
-    >>> file.setContentType('text/plain')
-    >>> file.getContentType()
+    >>> file.contentType = 'text/plain'
+    >>> file.contentType
     'text/plain'
 
-    >>> file.setData('Foobar')
-    >>> file.getData()
+    >>> file.data = 'Foobar'
+    >>> file.data
     'Foobar'
 
-    >>> file.edit('Blah', 'text/html')
-    >>> file.getContentType()
-    'text/html'
-    >>> file.getData()
-    'Blah'
-
-    >>> file.setData(None)
+    >>> file.data = None
     Traceback (most recent call last):
     ...
     TypeError: Cannot set None data on a file.
@@ -86,19 +79,19 @@
 
     Insert as string:
 
-    >>> file.setData('Foobar'*60000)
+    >>> file.data = 'Foobar'*60000
     >>> file.getSize()
     360000
-    >>> file.getData() == 'Foobar'*60000
+    >>> file.data == 'Foobar'*60000
     True
 
     Insert data as FileChunk:
 
     >>> fc = FileChunk('Foobar'*4000)
-    >>> file.setData(fc)
+    >>> file.data = fc
     >>> file.getSize()
     24000
-    >>> file.getData() == 'Foobar'*4000
+    >>> file.data == 'Foobar'*4000
     True
 
     Insert data from file object:
@@ -107,10 +100,10 @@
     >>> sio = cStringIO.StringIO()
     >>> sio.write('Foobar'*100000)
     >>> sio.seek(0)
-    >>> file.setData(sio)
+    >>> file.data = sio
     >>> file.getSize()
     600000
-    >>> file.getData() == 'Foobar'*100000
+    >>> file.data == 'Foobar'*100000
     True
 
 
@@ -129,38 +122,13 @@
         self.data = data
         self.contentType = contentType
 
-    def __len__(self):
-        return self.size
-
-    def setContentType(self, contentType):
-        '''See interface IFile'''
-        self._contentType = contentType
-
-    def getContentType(self):
-        '''See interface IFile'''
-        return self._contentType
-
-    def edit(self, data, contentType=None):
-        '''See interface IFile'''
-        # XXX This seems broken to me, as setData can override the
-        # content type explicitly passed in.
-
-        if contentType is not None:
-            self._contentType = contentType
-        if isinstance(data, FileUpload) and not data.filename:
-            data = None          # Ignore empty files
-        if data is not None:
-            self.data = data
-
-    def getData(self):
-        '''See interface IFile'''
+    def _getData(self):
         if isinstance(self._data, FileChunk):
             return str(self._data)
         else:
             return self._data
 
-    def setData(self, data):
-        '''See interface IFile'''
+    def _setData(self, data):
         # Handle case when data is a string
         if isinstance(data, unicode):
             data = data.encode('UTF-8')
@@ -241,27 +209,18 @@
         return
 
     def getSize(self):
-        '''See interface IFile'''
+        '''See IFile'''
         return self._size
 
-    data = property(getData, setData, None,
-                    """Contains the data of the file.""")
-
-    contentType = property(getContentType, setContentType, None,
-                           """Specifies the content type of the data.""")
-
-    size = property(getSize, None, None,
-                    """Specifies the size of the file in bytes. Read only.""")
+    # See IFile.
+    data = property(_getData, _setData)
 
 
-# Adapter for ISearchableText
-
-from zope.app.index.interfaces.text import ISearchableText
-
-class SearchableText:
+class SearchableText(object):
+    """Make File objects searchable."""
 
     implements(ISearchableText)
-    __used_for__ = IReadFile
+    __used_for__ = IFile
 
     def __init__(self, file):
         self.file = file
@@ -301,39 +260,40 @@
 
         return ''.join(result)
 
-class FileReadFile:
-    """Adapter for file-system style read access.
+
+class FileReadFile(object):
+    '''Adapter for file-system style read access.
 
     >>> file = File()
     >>> content = "This is some file\\ncontent."
-    >>> file.edit(content, 'text/plain')
+    >>> file.data = content
+    >>> file.contentType = "text/plain"
     >>> FileReadFile(file).read() == content
     True
     >>> FileReadFile(file).size() == len(content)
     True
-    """
-
+    '''
     def __init__(self, context):
         self.context = context
 
     def read(self):
-        return self.context.getData()
+        return self.context.data
 
     def size(self):
-        return len(self.context.getData())
+        return len(self.context.data)
 
-class FileWriteFile:
+
+class FileWriteFile(object):
     """Adapter for file-system style write access.
 
     >>> file = File()
     >>> content = "This is some file\\ncontent."
     >>> FileWriteFile(file).write(content)
-    >>> file.getData() == content
+    >>> file.data == content
     True
     """
-
     def __init__(self, context):
         self.context = context
 
     def write(self, data):
-        self.context.setData(data)
+        self.context.data = data


=== Zope3/src/zope/app/file/fssync.py 1.2 => 1.3 ===
--- Zope3/src/zope/app/file/fssync.py:1.2	Tue Feb 24 11:49:48 2004
+++ Zope3/src/zope/app/file/fssync.py	Thu Mar 18 22:17:38 2004
@@ -25,10 +25,10 @@
     implements(IObjectFile)
 
     def getBody(self):
-        return self.context.getData()
+        return self.context.data
 
     def setBody(self, data):
-        self.context.setData(data)
+        self.context.data = data
 
     def extra(self):
         return AttrMapping(self.context, ('contentType',))


=== Zope3/src/zope/app/file/image.py 1.3 => 1.4 ===
--- Zope3/src/zope/app/file/image.py:1.3	Wed Mar  3 06:03:59 2004
+++ Zope3/src/zope/app/file/image.py	Thu Mar 18 22:17:38 2004
@@ -37,10 +37,10 @@
         self.contentType, self._width, self._height = getImageInfo(data)
         self.data = data
 
-    def setData(self, data):
-        super(Image, self).setData(data)
+    def _setData(self, data):
+        super(Image, self)._setData(data)
 
-        contentType, self._width, self._height = getImageInfo(self.data)
+        contentType, self._width, self._height = getImageInfo(self._data)
         if contentType:
             self.contentType = contentType
 
@@ -48,8 +48,7 @@
         '''See interface IImage'''
         return (self._width, self._height)
 
-    data = property(File.getData, setData, None,
-                    """Contains the data of the file.""")
+    data = property(File._getData, _setData)
 
 class ImageSized:
     implements(ISized)


=== Zope3/src/zope/app/file/interfaces.py 1.2 => 1.3 ===
--- Zope3/src/zope/app/file/interfaces.py:1.2	Tue Feb 24 11:49:48 2004
+++ Zope3/src/zope/app/file/interfaces.py	Thu Mar 18 22:17:38 2004
@@ -15,57 +15,29 @@
 
 $Id$
 """
-import zope.schema
+from zope.schema import BytesLine, Bytes
 from zope.interface import Interface
 from zope.app.i18n import ZopeMessageIDFactory as _
 
-class IReadFile(Interface):
+class IFile(Interface):
 
-    contentType = zope.schema.BytesLine(
+    contentType = BytesLine(
         title = _(u'Content Type'),
         description=_(u'The content type identifies the type of data.'),
         default = 'text/plain',
         required=False
         )
 
-    data = zope.schema.Bytes(
+    data = Bytes(
         title = _(u'Data'),
         description = _(u'The actual content of the object.'),
         default='',
         required=False,
         )
 
-    def getData():
-        """Return the contained data of the object."""
-
-    def getContentType():
-        """Returns the content type of the file using mime-types syntax."""
-
     def getSize():
         """Return the byte-size of the data of the object."""
 
-
-class IWriteFile(Interface):
-
-    def edit(data, contentType=None):
-        """Sets the data and the content type for the object.
-
-           Since some implementations will provide their content type
-           through the data, it is good to leave the argument optional.
-        """
-
-    def setData(data):
-        """Rewrite the 'file'."""
-
-    def setContentType(contentType):
-        """Sets the content type of the file."""
-
-
-class IFile(IReadFile, IWriteFile):
-    """The basic methods that are required to implement
-       a file as a Zope Content object.
-
-    """
 
 class IFileContent(Interface):
     """Marker interface for content that can be managed as files.




More information about the Zope3-Checkins mailing list