[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/OFS/Content/Image - Image.py:1.1.2.1 ImageFields.py:1.1.2.1 __init__.py:1.1.2.1 image.zcml:1.1.2.1

Stephan Richter srichter@cbu.edu
Wed, 27 Mar 2002 10:54:39 -0500


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

Added Files:
      Tag: Zope-3x-branch
	Image.py ImageFields.py __init__.py image.zcml 
Log Message:
New Content Objects:

- NaiveFile --> all the data is stored in one string
- File --> Uses a BTree to store the data in chunks (more efficient)
- Image --> Can store and display an image a la Z2 (based on File)
- ZPTPage --> A simple version of ZPT for the content space to allow some 
  dynamics data (please do not use this for scripting)

Also:

- Expansion of supported views
- all edit screens are Formulator supported



=== Added File Zope3/lib/python/Zope/App/OFS/Content/Image/Image.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
# 
##############################################################################
"""
Revision Information:
$Id: Image.py,v 1.1.2.1 2002/03/27 15:54:38 srichter Exp $
"""

import struct

from Zope.App.OFS.Content.File.File import IFile, File
from Zope.App.Security.IAttributeRolePermissionManageable \
     import IAttributeRolePermissionManageable


class IImage(IFile):
    """This interface defines an Image that can be displayed.
    """

    def getImageSize():
        """Return a tuple (x, y) that describes the dimensions of
           the object.
        """
    

class Image(File):
    """ """

    __implements__ = (
        IImage,
        IAttributeRolePermissionManageable
        )

    def __init__(self, data=None):
        """ """
        self._contentType, self._width, self._height = getImageInfo(data)
        self.setData(data)


    def edit(self, data, contentType=None):
        """See interface IFile

           Note: It ignores the content type!
        """
        
        contentType, self._width, self._height = getImageInfo(data)
        if contentType:
            self._contentType = contentType
        self.setData(data)
        

    ############################################################
    # Implementation methods for interface
    # Zope.App.OFS.Image.IImage

    def getImageSize(self):
        '''See interface IImage'''

        return (self._width, self._height)

    #
    ############################################################
    


def getImageInfo(data):
    """ """
    
    data = str(data)
    size = len(data)
    height = -1
    width = -1
    content_type = ''

    # handle GIFs   
    if (size >= 10) and data[:6] in ('GIF87a', 'GIF89a'):
        # Check to see if content_type is correct
        content_type = 'image/gif'
        w, h = struct.unpack("<HH", data[6:10])
        width = int(w)
        height = int(h)

    # See PNG v1.2 spec (http://www.cdrom.com/pub/png/spec/)
    # Bytes 0-7 are below, 4-byte chunk length, then 'IHDR'
    # and finally the 4-byte width, height
    elif ((size >= 24) and data.startswith('\211PNG\r\n\032\n')
          and (data[12:16] == 'IHDR')):
        content_type = 'image/png'
        w, h = struct.unpack(">LL", data[16:24])
        width = int(w)
        height = int(h)
            
    # Maybe this is for an older PNG version.
    elif (size >= 16) and data.startswith('\211PNG\r\n\032\n'):
        # Check to see if we have the right content type
        content_type = 'image/png'
        w, h = struct.unpack(">LL", data[8:16])
        width = int(w)
        height = int(h)

    # handle JPEGs
    elif (size >= 2) and data.startswith('\377\330'):
        content_type = 'image/jpeg'
        jpeg = StringIO(data)
        jpeg.read(2)
        b = jpeg.read(1)
        try:
            while (b and ord(b) != 0xDA):
                while (ord(b) != 0xFF): b = jpeg.read(1)
                while (ord(b) == 0xFF): b = jpeg.read(1)
                if (ord(b) >= 0xC0 and ord(b) <= 0xC3):
                    jpeg.read(3)
                    h, w = struct.unpack(">HH", jpeg.read(4))
                    break
                else:
                    jpeg.read(int(struct.unpack(">H", jpeg.read(2))[0])-2)
                b = jpeg.read(1)
            width = int(w)
            height = int(h)
        except: pass

    return content_type, width, height


=== Added File Zope3/lib/python/Zope/App/OFS/Content/Image/ImageFields.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
# 
##############################################################################
"""
Revision information: 
$Id: ImageFields.py,v 1.1.2.1 2002/03/27 15:54:38 srichter Exp $
"""

from Zope.App.Formulator.FieldRegistry import getField
from Zope.App.Formulator.ValidatorRegistry import getValidator


ContentTypeField = getField('StringField')(
    id = 'contentType',
    title = 'Content Type',
    description = 'The content type identifies the type of data.',
    default = 'image/gif',
    )


DataField = getField('FileField')(
    id = 'data',
    title = 'Data',
    description = 'The actual content of the object.'
    )


=== Added File Zope3/lib/python/Zope/App/OFS/Content/Image/__init__.py ===


=== Added File Zope3/lib/python/Zope/App/OFS/Content/Image/image.zcml ===
<zopeConfigure
   xmlns='http://namespaces.zope.org/zope'
   xmlns:security='http://namespaces.zope.org/security'
   xmlns:zmi='http://namespaces.zope.org/zmi'
   xmlns:browser='http://namespaces.zope.org/browser'
>

  <security:permission permission_id="Zope.AddImages" title="Add Images" />

  <zmi:provideClass name=".Image."
                    permission_id="Zope.AddImages"
                    title="Image"
                    description="An Image" />

  <security:protectClass name=".Image."
    permission_id="Zope.View" />


  <!-- tabs for image -->

  <zmi:tabs for=".Image.IImage.">
    <zmi:tab label="Edit" action="edit;view"/>
    <zmi:tab label="Contents" action="data;view"/>
    <zmi:tab label="Role Permissions" action="RolePermissionsManagement;view"/>
  </zmi:tabs>


  <!-- Further Directives -->

  <include package=".Views" file="views.zcml" />

</zopeConfigure>