[Zope3-checkins] CVS: Zope3/src/zope/app/interfaces/content - i18nimage.py:1.1.2.1 image.py:1.1.2.1
Jim Fulton
jim@zope.com
Wed, 25 Dec 2002 08:27:33 -0500
Update of /cvs-repository/Zope3/src/zope/app/interfaces/content
In directory cvs.zope.org:/tmp/cvs-serv9989/src/zope/app/interfaces/content
Added Files:
Tag: NameGeddon-branch
i18nimage.py image.py
Log Message:
Gor zope running again!
I haven't tested everything.
(Yes, we need functional tests. Maybe this week).
Good enough to merge into trunk.
=== Added File Zope3/src/zope/app/interfaces/content/i18nimage.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: i18nimage.py,v 1.1.2.1 2002/12/25 13:27:33 jim Exp $
"""
from zope.app.content.image import IImage, Image, getImageInfo
from zope.app.interfaces.content.i18nfile import II18nFile
from zope.app.content.i18nfile import I18nFile
from zope.app.interfaces.annotation import IAnnotatable
class II18nImage(II18nFile, IImage):
"""I18n aware image interface."""
class I18nImage(I18nFile):
"""An internationalized Image object. Note that images of all
languages share the same content type.
"""
__implements__ = (
II18nImage,
IAnnotatable,
)
def _create(self, data):
return Image(data)
def setData(self, data, language=None):
'''See interface IFile'''
super(I18nImage, self).setData(data, language)
if language is None or language == self.getDefaultLanguage():
# Uploading for the default language only overrides content
# type. Note: do not use the argument data here, it doesn't
# work.
contentType = getImageInfo(self.getData(language))[0]
if contentType:
self.setContentType(contentType)
def getImageSize(self, language=None):
'''See interface IImage'''
return self._get(language).getImageSize()
=== Added File Zope3/src/zope/app/interfaces/content/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.
#
##############################################################################
"""
$Id: image.py,v 1.1.2.1 2002/12/25 13:27:33 jim Exp $
"""
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):
"""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, 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