[Zope3-checkins] CVS: Zope3/src/zope/app/interfaces/content - zpt.py:1.1 image.py:1.3

Steve Alexander steve@cat-box.net
Fri, 27 Dec 2002 15:33:51 -0500


Update of /cvs-repository/Zope3/src/zope/app/interfaces/content
In directory cvs.zope.org:/tmp/cvs-serv24831/src/zope/app/interfaces/content

Modified Files:
	image.py 
Added Files:
	zpt.py 
Log Message:
Split zpt interfaces out into the zope/app/interfaces hierarchy.
Added an ISized adapter for zpt pages.


=== Added File Zope3/src/zope/app/interfaces/content/zpt.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: zpt.py,v 1.1 2002/12/27 20:33:50 stevea Exp $
"""

import zope.schema

from zope.interface import Interface, Attribute

class IZPTPage(Interface):
    """ZPT Pages are a persistent implementation of Page Templates.

       Note: I introduced some new methods whose functionality is
             actually already covered by some other methods but I
             want to start enforcing a common coding standard.
    """

    def setSource(text, content_type='text/html'):
        """Save the source of the page template.

        'text' must be Unicode.
        """

    def getSource():
        """Get the source of the page template."""

    source = zope.schema.Text(
        title=u"Source",
        description=u"""The source of the page template.""",
        required=True)


class IRenderZPTPage(Interface):

    content_type = Attribute('Content type of generated output')

    def render(request, *args, **kw):
        """Render the page template.

        The first argument is bound to the top-level 'request'
        variable. The positional arguments are bound to the 'args'
        variable and the keyword arguments are bound to the 'options'
        variable.
        """



=== Zope3/src/zope/app/interfaces/content/image.py 1.2 => 1.3 ===
--- Zope3/src/zope/app/interfaces/content/image.py:1.2	Wed Dec 25 09:12:59 2002
+++ Zope3/src/zope/app/interfaces/content/image.py	Fri Dec 27 15:33:50 2002
@@ -14,12 +14,8 @@
 """
 $Id$
 """
-import struct
 
-from zope.app.content.file import File
 from zope.app.interfaces.content.file import IFile
-from zope.app.interfaces.annotation import IAnnotatable
-from cStringIO import StringIO
 
 
 class IImage(IFile):
@@ -29,89 +25,3 @@
         """Return a tuple (x, y) that describes the dimensions of
         the object."""
 
-class Image(File):
-    __implements__ = IImage, IAnnotatable
-
-    def __init__(self, data=None):
-        '''See interface IFile'''
-        self.contentType, self._width, self._height = getImageInfo(data)
-        self.data = data
-
-
-    def setData(self, data):
-
-        super(Image, self).setData(data)
-
-        if data is not None:
-            contentType = None
-            contentType, self._width, self._height = getImageInfo(self.data)
-            if contentType:
-                self.contentType = contentType
-
-
-    def getImageSize(self):
-        '''See interface IImage'''
-        return (self._width, self._height)
-
-    data = property(File.getData, setData, None,
-                    """Contains the data of the file.""")
-
-
-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 struct.error:
-            pass
-        except ValueError:
-            pass
-
-    return content_type, width, height