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 @@
# # cybertools.media.asset
# 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
#
""" """ Media asset file adapter.
Media asset file adapter.
Authors: Johann Schimpf, Erich Seifert. Authors: Johann Schimpf, Erich Seifert.
$Id$
""" """
from cStringIO import StringIO from io import BytesIO, StringIO
from logging import getLogger from logging import getLogger
import mimetypes import mimetypes
import os, re, sys import os, re, sys
from zope.interface import implements from zope.interface import implementer
from cybertools.media.interfaces import IMediaAsset from cybertools.media.interfaces import IMediaAsset
from cybertools.media.piltransform import PILTransform from cybertools.media.piltransform import PILTransform
@ -55,13 +36,12 @@ def getMimetypeExt(mimetype):
return exts and exts[-1] or "" return exts and exts[-1] or ""
@implementer(IMediaAsset)
class MediaAssetFile(object): class MediaAssetFile(object):
""" Class for extracting metadata from assets and to create transformed """ Class for extracting metadata from assets and to create transformed
variants using file representations in subdirectories. variants using file representations in subdirectories.
""" """
implements(IMediaAsset)
def __init__(self, dataPath, rules, contentType): def __init__(self, dataPath, rules, contentType):
self.dataPath = dataPath self.dataPath = dataPath
self.rules = rules self.rules = rules
@ -72,7 +52,7 @@ class MediaAssetFile(object):
return self.getOriginalData() return self.getOriginalData()
path = self.getPath(variant) path = self.getPath(variant)
if not os.path.exists(path): 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) 'Media asset directory %r not found.' % path)
self.transform() self.transform()
# return self.getOriginalData() # return self.getOriginalData()
@ -85,7 +65,7 @@ class MediaAssetFile(object):
if data is None: if data is None:
data = self.getData(variant) data = self.getData(variant)
pt = PILTransform() pt = PILTransform()
pt.open(StringIO(data)) pt.open(BytesIO(data))
if pt.im is None: if pt.im is None:
return (0, 0) return (0, 0)
return pt.im.size return pt.im.size
@ -148,14 +128,15 @@ class MediaAssetFile(object):
mediaFile.resize(*size) mediaFile.resize(*size)
outputFormat = self.getContentType(variant) outputFormat = self.getContentType(variant)
mediaFile.save(path, outputFormat) mediaFile.save(path, outputFormat)
except Exception, e: except Exception:
excInfo = sys.exc_info() raise
#excInfo = sys.exc_info()
# Handle exceptions that have occured during the transformation # Handle exceptions that have occured during the transformation
# in order to provide information on the affected asset # in order to provide information on the affected asset
if excInfo: #if excInfo:
eType, eValue, eTraceback = excInfo # Extract exception information # eType, eValue, eTraceback = excInfo # Extract exception information
raise eType("Error transforming asset '%s': %s" % # raise eType("Error transforming asset '%s': %s" %
(oldassetdir, eValue)), None, eTraceback # (oldassetdir, eValue)), None, eTraceback)
def getPath(self, variant): def getPath(self, variant):
pathOrig = self.getDataPath() pathOrig = self.getDataPath()

View file

@ -1,23 +1,6 @@
# # cybertools.media.piltransform
# 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
#
""" """ Views for displaying media assets.
Views for displaying media assets.
Authors: Johann Schimpf, Erich Seifert. Authors: Johann Schimpf, Erich Seifert.
""" """
@ -32,7 +15,7 @@ except:
getLogger('Asset Manager').warn('Python Imaging Library ' getLogger('Asset Manager').warn('Python Imaging Library '
'could not be found.') 'could not be found.')
from zope.interface import implements from zope.interface import implementer
from cybertools.media.interfaces import IMediaAsset, IFileTransform from cybertools.media.interfaces import IMediaAsset, IFileTransform
from cybertools.storage.filesystem import FileSystemStorage from cybertools.storage.filesystem import FileSystemStorage
@ -44,17 +27,16 @@ def mimetypeToPIL(mimetype):
return mimetype.split("/",1)[-1] return mimetype.split("/",1)[-1]
@implementer(IFileTransform)
class PILTransform(object): class PILTransform(object):
""" Class for image transformation methods. """ Class for image transformation methods.
Based on the Python Imaging Library. Based on the Python Imaging Library.
""" """
implements(IFileTransform)
def open(self, path): def open(self, path):
try: try:
self.im = Image.open(path) self.im = Image.open(path)
except IOError, e: except (IOError, e):
logger.warn(e) logger.warn(e)
self.im = None self.im = None
@ -106,13 +88,13 @@ class PILTransform(object):
dims = (width, height) dims = (width, height)
if fill: if fill:
image = self.im 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 = Image.new('RGBA', dims, (255, 255, 255, 0)) #with alpha
new.paste(image,((dims[0] - image.size[0]) / 2, new.paste(image,((dims[0] - image.size[0]) / 2,
(dims[1] - image.size[1]) / 2)) (dims[1] - image.size[1]) / 2))
self.im = new self.im = new
return new return new
return self.im.thumbnail(dims, Image.ANTIALIAS) return self.im.thumbnail(dims) #, Image.ANTIALIAS)
def save(self, path, mimetype): def save(self, path, mimetype):
if self.im is None: if self.im is None:
@ -120,5 +102,5 @@ class PILTransform(object):
format = mimetypeToPIL(mimetype) format = mimetypeToPIL(mimetype)
try: try:
self.im.save(path) self.im.save(path)
except IOError, e: except(IOError, e):
logger.warn(e) logger.warn(e)

View file

@ -1,32 +1,13 @@
# # cybertools.storage.filesystem
# 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
#
""" """ Storing data in files in the file system.
Storing data in files in the file system.
$Id$
""" """
from datetime import datetime from datetime import datetime
from logging import getLogger from logging import getLogger
import os import os
import shutil import shutil
from zope.interface import implements from zope.interface import implementer
import transaction import transaction
from transaction.interfaces import IDataManager from transaction.interfaces import IDataManager
@ -39,10 +20,9 @@ DEFAULT_DIRECTORY = 'extfiles'
logger = getLogger('cybertools.storage.filesystem') logger = getLogger('cybertools.storage.filesystem')
@implementer(IExternalStorage)
class FileSystemStorage(object): class FileSystemStorage(object):
implements(IExternalStorage)
def __init__(self, rootDir=None, subDir=None): def __init__(self, rootDir=None, subDir=None):
self.rootDir = rootDir self.rootDir = rootDir
self.subDir = subDir self.subDir = subDir
@ -80,7 +60,7 @@ class FileSystemStorage(object):
data = f.read() data = f.read()
f.close() f.close()
return data return data
except IOError, e: except(IOError, e):
logger.warn(e) logger.warn(e)
#'File %r cannot be read.' % fn) #'File %r cannot be read.' % fn)
return '' return ''
@ -90,7 +70,7 @@ class FileSystemStorage(object):
fn = self.getDir(address, subDir) fn = self.getDir(address, subDir)
try: try:
return os.path.getsize(fn) return os.path.getsize(fn)
except OSError, e: except(OSError, e):
logger.warn(e) logger.warn(e)
return 0 return 0
@ -101,7 +81,7 @@ class FileSystemStorage(object):
ts = os.path.getmtime(fn) ts = os.path.getmtime(fn)
if ts: if ts:
return datetime.fromtimestamp(ts) return datetime.fromtimestamp(ts)
except OSError, e: except(OSError, e):
logger.warn(e) logger.warn(e)
return None return None
@ -119,10 +99,9 @@ class FileSystemStorage(object):
@implementer(IDataManager)
class FSSDataManager(object): class FSSDataManager(object):
implements(IDataManager)
transaction_manager = None transaction_manager = None
def __init__(self, address, temp): def __init__(self, address, temp):