Update of /cvsroot/archetypes/PortalTransforms
In directory sc8-pr-cvs1:/tmp/cvs-serv27357
Modified Files:
MimeTypesRegistry.py
Log Message:
use suffix map has the standard mime types module
hopefully correct behaviour of classify
fiex testaccordingly
Index: MimeTypesRegistry.py
===================================================================
RCS file: /cvsroot/archetypes/PortalTransforms/MimeTypesRegistry.py,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** MimeTypesRegistry.py 6 Aug 2003 15:52:28 -0000 1.3
--- MimeTypesRegistry.py 8 Aug 2003 10:16:29 -0000 1.4
***************
*** 1,4 ****
! from os.path import splitext
! from types import UnicodeType
from Products.PortalTransforms.interfaces import imimetype, isourceAdapter, \
--- 1,4 ----
! from os.path import splitext, basename
! from types import UnicodeType, StringType
from Products.PortalTransforms.interfaces import imimetype, isourceAdapter, \
***************
*** 7,13 ****
from Products.PortalTransforms.mime_types import initialize
from Products.PortalTransforms.utils import log, implements, DictClass, Base
__revision__ = '$Id$'
!
class MimeTypesRegistry(Base):
"""Mimetype registry that deals with
--- 7,27 ----
from Products.PortalTransforms.mime_types import initialize
from Products.PortalTransforms.utils import log, implements, DictClass, Base
+ from encoding import guess_encoding
__revision__ = '$Id$'
!
! STRING_TYPES = (UnicodeType, StringType)
!
! suffix_map = {
! 'tgz': '.tar.gz',
! 'taz': '.tar.gz',
! 'tz': '.tar.gz',
! }
!
! encodings_map = {
! 'gz': 'gzip',
! 'Z': 'compress',
! }
!
class MimeTypesRegistry(Base):
"""Mimetype registry that deals with
***************
*** 20,23 ****
--- 34,39 ----
def __init__(self, fill=1):
+ self.encodings_map = encodings_map.copy()
+ self.suffix_map = suffix_map.copy()
# Major key -> minor imimetype objects
self._mimetypes = DictClass()
***************
*** 121,127 ****
"""
if filename.find('.') != -1:
! ext = splitext(filename)[1][1:]
else:
ext = filename
return self.extensions.get(ext)
--- 137,153 ----
"""
if filename.find('.') != -1:
! base, ext = splitext(filename)
! ext = ext[1:] # remove the dot
! while self.suffix_map.has_key(ext):
! base, ext = splitext(base + self.suffix_map[ext])
! ext = ext[1:] # remove the dot
else:
ext = filename
+ if self.encodings_map.has_key(ext):
+ encoding = self.encodings_map[ext]
+ base, ext = splitext(base)
+ ext = ext[1:] # remove the dot
+ else:
+ encoding = None
return self.extensions.get(ext)
***************
*** 136,144 ****
the mimetype
3) we can optionally introspect the data
! 4) default to application/octet-stream if all previous step failed
! return an imimetype object
"""
- if not data:
- return self.lookup(self.defaultMimetype)[0]
mt = None
if mimetype:
--- 162,171 ----
the mimetype
3) we can optionally introspect the data
! 4) default to self.defaultMimetype if no data was provided
! else to application/octet-stream of no filename was provided,
! else to text/plain
!
! return an imimetype object
"""
mt = None
if mimetype:
***************
*** 148,153 ****
elif filename:
mt = self.lookupExtension(filename)
!
! if not mt:
for c in self._classifiers():
if c.classify(data):
--- 175,179 ----
elif filename:
mt = self.lookupExtension(filename)
! if data and not mt:
for c in self._classifiers():
if c.classify(data):
***************
*** 155,159 ****
break
if not mt:
! mt = self.lookup('application/octet-stream')[0]
return mt
--- 181,190 ----
break
if not mt:
! if not data:
! mt = self.lookup(self.defaultMimetype)[0]
! elif filename:
! mt = self.lookup('application/octet-stream')[0]
! else:
! mt = self.lookup('text/plain')[0]
return mt
***************
*** 165,175 ****
"""
filename = mt = None
- if hasattr(data, 'read'):
- data = data.read()
if hasattr(data, 'filename'):
! filename = data.filename
elif hasattr(data, 'name'):
! filename = data.name()
!
# We need to figure out if data is binary and skip encoding if
# it is
--- 196,210 ----
"""
filename = mt = None
if hasattr(data, 'filename'):
! filename = basename(data.filename)
elif hasattr(data, 'name'):
! filename = basename(data.name)
!
! if hasattr(data, 'read'):
! _data = data.read()
! if hasattr(data, 'seek'):
! data.seek(0)
! data = _data
!
# We need to figure out if data is binary and skip encoding if
# it is
***************
*** 177,188 ****
if not mt.binary and type(data) != UnicodeType:
if encoding is None:
! site_props = self.portal_properties.site_properties
! encoding = site_props.getProperty('default_charset', 'UTF-8')
data = unicode(data, encoding)
return (data, filename, mt)
!
def split(name):
""" split a mime type in a (major / minor) 2-uple """
--- 212,240 ----
if not mt.binary and type(data) != UnicodeType:
+ # if no encoding specified, try to guess it from data
if encoding is None:
! encoding = self.guess_encoding(data)
data = unicode(data, encoding)
return (data, filename, mt)
! def guess_encoding(self, data):
! """try to guess encoding from a text value
! if no encoding guessed, used the default charset from site properties
(Zope)
! with a fallback to UTF-8 (should never happen with correct
site_properties,
! but always raise Attribute error without Zope)
! """
! if type(data) is type(u''):
! # data maybe unicode but with another encoding specified
! data = data.encode('UTF-8')
! encoding = guess_encoding(data)
! if encoding is None:
! try:
! site_props = self.portal_properties.site_properties
! encoding = site_props.getProperty('default_charset', 'UTF-8')
! except:
! encoding = 'UTF-8'
! return encoding
!
def split(name):
""" split a mime type in a (major / minor) 2-uple """
-------------------------------------------------------
This SF.Net email sponsored by: Free pre-built ASP.NET sites including
Data Reports, E-commerce, Portals, and Forums are available now.
Download today and enter to win an XBOX or Visual Studio .NET.
http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01/01
|