[Zope3-checkins] CVS: Zope3/lib/python/Zope/App/OFS/Content/File - SFile.py:1.1 File.py:1.6 FileFields.py:1.3 NaiveFile.py:1.3 configure.zcml:1.7

Stephan Richter srichter@cbu.edu
Fri, 19 Jul 2002 09:13:02 -0400


Update of /cvs-repository/Zope3/lib/python/Zope/App/OFS/Content/File
In directory cvs.zope.org:/tmp/cvs-serv24805/lib/python/Zope/App/OFS/Content/File

Modified Files:
	File.py FileFields.py NaiveFile.py configure.zcml 
Added Files:
	SFile.py 
Log Message:
Okay, I finished the Forms work. Schema and Forms completely replace the
old Formulator code now. I have switched all the Content objects to using
Schema + Forms; especially the SQL Script has an interesting demo on how
to write your custom fields.

However, I am not satisfied with all my design decisions. There is still
a lot of work to be done in Converters and Widgets. Please contact Martijn
and/or me if you would like to help.


=== Added File Zope3/lib/python/Zope/App/OFS/Content/File/SFile.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
# 
##############################################################################
"""

$Id: SFile.py,v 1.1 2002/07/19 13:12:31 srichter Exp $
"""
import Schema


class SFile(Schema.Schema):

    contentType = Schema.Str(
        id = 'contentType',
        title = 'Content Type',
        description = 'The content type identifies the type of data.',
        default = 'text/plain',
        )


    data = Schema.Str(
        id = 'data',
        title = 'Data',
        description = 'The actual content of the object.',
        )


=== Zope3/lib/python/Zope/App/OFS/Content/File/File.py 1.5 => 1.6 ===
 
 $Id$
 """
-
 from types import StringType, UnicodeType, NoneType
 
-import Persistence
+from Persistence import Persistent
 from Transaction import get_transaction
 
 from Zope.App.OFS.Content.File.FileChunk import FileChunk
 from Zope.App.OFS.Content.File.IFile import IFile
 from Zope.Publisher.Browser.BrowserRequest import FileUpload
 
+from Zope.App.OFS.Annotation.IAnnotatable import IAnnotatable
+from Zope.App.OFS.Content.File.SFile import SFile
+from Zope.App.OFS.Content.File.IFile import IFile
+
 # set the size of the chunks
 MAXCHUNKSIZE = 1 << 16
 
-class File(Persistence.Persistent):
-    """ """
+class File(Persistent):
+    __implements__ = SFile, IFile, IAnnotatable
 
-    __implements__ = IFile
+    def __init__(self, data='', contentType=''):
+        self.data = data
+        self.contentType = contentType
 
-    def __init__(self, data='', contentType=None):
-        """ """
-
-        self.setData(data)
-
-        if contentType is None:
-            self._contentType = ''
-        else:
-            self._contentType = contentType
-        
 
     def __len__(self):
-        return self.getSize()
-
+        return self.size
 
-    ############################################################
-    # Implementation methods for interface
-    # Zope.App.OFS.IFile.IFile
 
     def setContentType(self, contentType):
-        '''See interface IFile'''
+        '''See interface Zope.App.OFS.Content.File.IFile.IFile'''
         self._contentType = contentType
 
         
     def getContentType(self):
-        '''See interface IFile'''
+        '''See interface Zope.App.OFS.Content.File.IFile.IFile'''
         return self._contentType
 
         
     def edit(self, data, contentType=None):
-        '''See interface IFile'''
-
+        '''See interface Zope.App.OFS.Content.File.IFile.IFile'''
         # XXX This seems broken to me, as setData can override the
         # content type explicitly passed in.
         
@@ -74,21 +64,20 @@
            and not data.filename:
            data = None          # Ignore empty files
         if data is not None:
-            self.setData(data)
+            self.data = data
 
 
     def getData(self):
-        '''See interface IFile'''
-        if ( hasattr(self._data, '__class__')
-         and self._data.__class__ is FileChunk ):
+        '''See interface Zope.App.OFS.Content.File.IFile.IFile'''
+        if hasattr(self._data, '__class__') and \
+           self._data.__class__ is FileChunk:
             return str(self._data)
         else:
             return self._data
 
 
     def setData(self, data):
-        '''See interface IFile'''
-
+        '''See interface Zope.App.OFS.Content.File.IFile.IFile'''
         # Handle case when data is a string
         if isinstance(data, UnicodeType):
             data = data.encode('UTF-8')
@@ -112,7 +101,7 @@
             self._data, self._size = data, size
             return None
 
-        # Handle case when File is a file object
+        # Handle case when data is a file object
         seek = data.seek
         read = data.read
         
@@ -175,10 +164,16 @@
 
 
     def getSize(self):
-        '''See interface IFile'''
+        '''See interface Zope.App.OFS.Content.File.IFile.IFile'''
         return self._size
 
-    #
-    ############################################################
 
+    # See schema Zope.App.OFS.File.SFile.SFile
+    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.""")


=== Zope3/lib/python/Zope/App/OFS/Content/File/FileFields.py 1.2 => 1.3 ===
 
 $Id$
 """
+import Schema
 
-from Zope.App.Formulator.FieldRegistry import getField
-from Zope.App.Formulator.ValidatorRegistry import getValidator
 
+class SFile(Schema):
 
-ContentTypeField = getField('StringField')(
-    id = 'contentType',
-    title = 'Content Type',
-    description = 'The content type identifies the type of data.',
-    default = 'text/plain',
-    )
+    contentType = Schema.Str(
+        id = 'contentType',
+        title = 'Content Type',
+        description = 'The content type identifies the type of data.',
+        default = 'text/plain',
+        )
 
 
-DataField = getField('FileField')(
-    id = 'data',
-    title = 'Data',
-    description = 'The actual content of the object.',
-    )
+    data = Schema.Str(
+        id = 'data',
+        title = 'Data',
+        description = 'The actual content of the object.',
+        )


=== Zope3/lib/python/Zope/App/OFS/Content/File/NaiveFile.py 1.2 => 1.3 ===
 # 
 ##############################################################################
 """
-
 $Id$
 """
-
-import Persistence
-from IFile import IFile
+from Persistence import Persistent
+from Zope.App.OFS.Content.File.IFile import IFile
+from Zope.App.OFS.Content.File.SFile import SFile
 from Zope.App.OFS.Annotation.IAnnotatable import IAnnotatable
 
 
-_RAISE_KEYERROR = []
-
-
-class NaiveFile:
+class NaiveFile(Persistent):
     """This is a very simple implementation of a file.
 
     WARNING: This implementation should not be used to save large amounts
              of Data.
     """
+    __implements__ = IFile, SFile, IAnnotatable
 
-    __implements__ = (
-        IFile,
-        IAnnotatable)
-
-
-    def __init__(self, data='', contentType=None):
-        """ """
-
+    def __init__(self, data='', contentType=''):
         self.setData(data)
-
-        if contentType is None:
-            self._contentType = ''
-        else:
-            self._contentType = contentType
-
+        self._contentType = contentType
 
     def __str__(self):
         return self.getData()
 
-
     def __len__(self):
         return 1
         
-
-    ############################################################
-    # Implementation methods for interface
-    # Zope.App.OFS.File.IFile
-
     def setContentType(self, contentType):
-        '''See interface IFile'''
+        '''See interface Zope.App.OFS.File.IFile.IFile'''
+        SFile.getDescriptionFor('contentType').validate(contentType)
         self._contentType = contentType
-
-        
+    
     def getContentType(self):
-        '''See interface IFile'''
+        '''See interface Zope.App.OFS.File.IFile.IFile'''
         return self._contentType
 
-        
     def edit(self, data, contentType=None):
-        '''See interface IFile'''
+        '''See interface Zope.App.OFS.File.IFile.IFile'''
         self._data = data
         if contentType is not None:
             self._contentType = contentType
 
-
     def getData(self):
-        '''See interface IFile'''
+        '''See interface Zope.App.OFS.File.IFile.IFile'''
         return self._data
 
-
     def setData(self, data):
-        '''See interface IFile'''
+        '''See interface Zope.App.OFS.File.IFile.IFile'''
+        SFile.getDescriptionFor('data').validate(data)
         if data is not None:
             self._size = len(data)
-        else:
-            self._size = 0
-        self._data = data
-
+            self._data = data
 
     def getSize(self):
-        '''See interface IFile'''
+        '''See interface Zope.App.OFS.File.IFile.IFile'''
         return self._size
-        
-    #
-    ############################################################
 
+    # See schema Zope.App.OFS.File.SFile.SFile
+    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.""")


=== Zope3/lib/python/Zope/App/OFS/Content/File/configure.zcml 1.6 => 1.7 ===
 <zopeConfigure xmlns='http://namespaces.zope.org/zope'>
 
-  <!-- NaiveFile Directives -->
+<!-- File Directives -->
 
-  <permission id="Zope.AddNaiveFiles" 
-                       title="Add Naive Files" />
+<content class=".File.">
+  <factory
+      id="File"
+      permission="Zope.ManageContent"
+      title="File"
+      description="A File" />
+  <require
+      permission="Zope.View"
+      interface=".SFile.SFile" />
+  <require
+      permission="Zope.View"
+      interface=".IFile.IReadFile" />
+  <require
+      permission="Zope.ManageContent"
+      interface=".IFile.IWriteFile" />
+</content>
+
+<adapter
+    factory="Zope.App.OFS.Annotation.AttributeAnnotations."
+    provides="Zope.App.OFS.Annotation.IAnnotations."
+    for=".IFile." />
+
+<!-- NaiveFile Directives -->
+
+<content class=".NaiveFile.">
+  <factory
+      id="NaiveFile"
+      permission="Zope.ManageContent"
+      title="Naive File"
+      description="This is a simple file" />
+  <require like_class=".File." />
+</content>
+
+<!-- I18n File Directives -->
+
+<content class=".I18nFile.">
+  <factory
+      id="I18nFile"
+      permission="Zope.ManageContent"
+      title="I18n File"
+      description="An Internationalized File" />
+  <require
+      permission="Zope.View"
+      interface=".IFile.IReadFile" />
+  <require
+      permission="Zope.ManageContent"
+      interface=".IFile.IWriteFile" />
+  <require
+      permission="Zope.View"
+      attributes="getDefaultLanguage getAvailableLanguages" />
+  <require
+      permission="Zope.ManageContent"
+      attributes="setDefaultLanguage removeLanguage" />
+</content>
 
-  <content class=".NaiveFile.">
-    <factory
-        id="NaiveFile"
-        permission="Zope.AddNaiveFiles"
-        title="Naive File"
-        description="This is a simple file" />
-  </content>
-
-
-  <!-- File Directives -->
-
-  <permission id="Zope.AddFiles" title="Add Files" />
-
-  <content class=".File.">
-    <factory
-        id="File"
-        permission="Zope.ManageContent"
-        title="File"
-        description="A File" />
-    <require
-        permission="Zope.View"
-        interface=".IFile.IReadFile" />
-    <require
-        permission="Zope.ManageContent"
-        interface=".IFile.IWriteFile" />
-  </content>
-
-  <adapter
-      factory="Zope.App.OFS.Annotation.AttributeAnnotations."
-      provides="Zope.App.OFS.Annotation.IAnnotations."
-      for=".IFile." />
-
-
-  <!-- I18n File Directives -->
-
-  <content class=".I18nFile.">
-    <factory
-        id="I18nFile"
-        permission="Zope.ManageContent"
-        title="I18n File"
-        description="An Internationalized File" />
-    <require
-        permission="Zope.View"
-        interface=".IFile.IReadFile" />
-    <require
-        permission="Zope.ManageContent"
-        interface=".IFile.IWriteFile" />
-    <require
-        permission="Zope.View"
-        attributes="getDefaultLanguage getAvailableLanguages" />
-    <require
-        permission="Zope.ManageContent"
-        attributes="setDefaultLanguage removeLanguage" />
-  </content>
 
+<!-- Further Directives -->
 
-  <!-- Further Directives -->
-
-  <include package=".Views" />
+<include package=".Views" />
 
 </zopeConfigure>