media: Python3 fixes

This commit is contained in:
Helmut Merz 2024-09-22 15:24:24 +02:00
parent c9220d834d
commit e98dd2ed34
3 changed files with 30 additions and 88 deletions

View file

@ -1,35 +1,16 @@
#
# Copyright (c) 2009 Helmut Merz helmutm@cy55.de
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# cybertools.media.asset
"""
Media asset file adapter.
""" Media asset file adapter.
Authors: Johann Schimpf, Erich Seifert.
$Id$
"""
from cStringIO import StringIO
from io import BytesIO, StringIO
from logging import getLogger
import mimetypes
import os, re, sys
from zope.interface import implements
from zope.interface import implementer
from cybertools.media.interfaces import IMediaAsset
from cybertools.media.piltransform import PILTransform
@ -55,13 +36,12 @@ def getMimetypeExt(mimetype):
return exts and exts[-1] or ""
@implementer(IMediaAsset)
class MediaAssetFile(object):
""" Class for extracting metadata from assets and to create transformed
variants using file representations in subdirectories.
"""
implements(IMediaAsset)
def __init__(self, dataPath, rules, contentType):
self.dataPath = dataPath
self.rules = rules
@ -72,7 +52,7 @@ class MediaAssetFile(object):
return self.getOriginalData()
path = self.getPath(variant)
if not os.path.exists(path):
getLogger('cybertools.media.asset.MediaAssetFile').warn(
getLogger('cybertools.media.asset.MediaAssetFile').warning(
'Media asset directory %r not found.' % path)
self.transform()
# return self.getOriginalData()
@ -85,7 +65,7 @@ class MediaAssetFile(object):
if data is None:
data = self.getData(variant)
pt = PILTransform()
pt.open(StringIO(data))
pt.open(BytesIO(data))
if pt.im is None:
return (0, 0)
return pt.im.size
@ -148,14 +128,15 @@ class MediaAssetFile(object):
mediaFile.resize(*size)
outputFormat = self.getContentType(variant)
mediaFile.save(path, outputFormat)
except Exception, e:
excInfo = sys.exc_info()
except Exception:
raise
#excInfo = sys.exc_info()
# Handle exceptions that have occured during the transformation
# in order to provide information on the affected asset
if excInfo:
eType, eValue, eTraceback = excInfo # Extract exception information
raise eType("Error transforming asset '%s': %s" %
(oldassetdir, eValue)), None, eTraceback
#if excInfo:
# eType, eValue, eTraceback = excInfo # Extract exception information
# raise eType("Error transforming asset '%s': %s" %
# (oldassetdir, eValue)), None, eTraceback)
def getPath(self, variant):
pathOrig = self.getDataPath()

View file

@ -1,23 +1,6 @@
#
# Copyright (c) 2015 Helmut Merz helmutm@cy55.de
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# cybertools.media.piltransform
"""
Views for displaying media assets.
""" Views for displaying media assets.
Authors: Johann Schimpf, Erich Seifert.
"""
@ -32,7 +15,7 @@ except:
getLogger('Asset Manager').warn('Python Imaging Library '
'could not be found.')
from zope.interface import implements
from zope.interface import implementer
from cybertools.media.interfaces import IMediaAsset, IFileTransform
from cybertools.storage.filesystem import FileSystemStorage
@ -44,17 +27,16 @@ def mimetypeToPIL(mimetype):
return mimetype.split("/",1)[-1]
@implementer(IFileTransform)
class PILTransform(object):
""" Class for image transformation methods.
Based on the Python Imaging Library.
"""
implements(IFileTransform)
def open(self, path):
try:
self.im = Image.open(path)
except IOError, e:
except (IOError, e):
logger.warn(e)
self.im = None
@ -106,13 +88,13 @@ class PILTransform(object):
dims = (width, height)
if fill:
image = self.im
image.thumbnail(dims, Image.ANTIALIAS)
image.thumbnail(dims) #, Image.ANTIALIAS)
new = Image.new('RGBA', dims, (255, 255, 255, 0)) #with alpha
new.paste(image,((dims[0] - image.size[0]) / 2,
(dims[1] - image.size[1]) / 2))
self.im = new
return new
return self.im.thumbnail(dims, Image.ANTIALIAS)
return self.im.thumbnail(dims) #, Image.ANTIALIAS)
def save(self, path, mimetype):
if self.im is None:
@ -120,5 +102,5 @@ class PILTransform(object):
format = mimetypeToPIL(mimetype)
try:
self.im.save(path)
except IOError, e:
except(IOError, e):
logger.warn(e)

View file

@ -1,32 +1,13 @@
#
# Copyright (c) 2006 Helmut Merz helmutm@cy55.de
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# cybertools.storage.filesystem
"""
Storing data in files in the file system.
$Id$
""" Storing data in files in the file system.
"""
from datetime import datetime
from logging import getLogger
import os
import shutil
from zope.interface import implements
from zope.interface import implementer
import transaction
from transaction.interfaces import IDataManager
@ -39,10 +20,9 @@ DEFAULT_DIRECTORY = 'extfiles'
logger = getLogger('cybertools.storage.filesystem')
@implementer(IExternalStorage)
class FileSystemStorage(object):
implements(IExternalStorage)
def __init__(self, rootDir=None, subDir=None):
self.rootDir = rootDir
self.subDir = subDir
@ -80,7 +60,7 @@ class FileSystemStorage(object):
data = f.read()
f.close()
return data
except IOError, e:
except(IOError, e):
logger.warn(e)
#'File %r cannot be read.' % fn)
return ''
@ -90,7 +70,7 @@ class FileSystemStorage(object):
fn = self.getDir(address, subDir)
try:
return os.path.getsize(fn)
except OSError, e:
except(OSError, e):
logger.warn(e)
return 0
@ -101,7 +81,7 @@ class FileSystemStorage(object):
ts = os.path.getmtime(fn)
if ts:
return datetime.fromtimestamp(ts)
except OSError, e:
except(OSError, e):
logger.warn(e)
return None
@ -119,10 +99,9 @@ class FileSystemStorage(object):
@implementer(IDataManager)
class FSSDataManager(object):
implements(IDataManager)
transaction_manager = None
def __init__(self, address, temp):