Back to index

plone3  3.1.7
MimeTypeItem.py
Go to the documentation of this file.
00001 import os
00002 import urllib
00003 
00004 from Acquisition import Explicit
00005 from OFS.SimpleItem import Item
00006 from AccessControl import ClassSecurityInfo
00007 from Globals import Persistent, InitializeClass
00008 
00009 from Products.CMFCore.permissions import ManagePortal
00010 from Products.MimetypesRegistry.interfaces import IMimetype
00011 from Products.MimetypesRegistry.common import MimeTypeException
00012 
00013 
00014 class MimeTypeItem(Persistent, Explicit, Item):
00015     security = ClassSecurityInfo()
00016     __implements__ = (IMimetype,)
00017 
00018     extensions = ()
00019     globs = ()
00020 
00021     def __init__(self, name='', mimetypes=None, extensions=None,
00022                  binary=None, icon_path='', globs=None):
00023         if name:
00024             self.__name__ = self.id = name
00025         if mimetypes is not None:
00026             self.mimetypes = mimetypes
00027         if extensions is not None:
00028             self.extensions = extensions
00029         if binary is not None:
00030             self.binary = binary
00031         if globs is not None:
00032             self.globs = globs
00033         self.icon_path = icon_path or guess_icon_path(self)
00034 
00035     def __str__(self):
00036         return self.normalized()
00037 
00038     def __repr__(self):
00039         return "<mimetype %s>" % self.mimetypes[0]
00040 
00041     def __cmp__(self, other):
00042         if IMimetype.isImplementedBy(other):
00043             other = other.normalized()
00044         return not (other in self.mimetypes)
00045 
00046     def __hash__(self):
00047         return hash(self.name())
00048 
00049     security.declarePublic('name')
00050     def name(self):
00051         """ The name of this object """
00052         return self.__name__
00053 
00054     security.declarePublic('major')
00055     def major(self):
00056         """ return the major part of the RFC-2046 name for this mime type """
00057         return self.normalized().split('/', 1)[0]
00058 
00059     security.declarePublic('minor')
00060     def minor(self):
00061         """ return the minor part of the RFC-2046 name for this mime type """
00062         return self.normalized().split('/', 1)[1]
00063 
00064     security.declarePublic('normalized')
00065     def normalized(self):
00066         """ return the main RFC-2046 name for this mime type
00067 
00068         e.g. if this object has names ('text/restructured', 'text-x-rst')
00069         then self.normalized() will always return the first form.
00070         """
00071         return self.mimetypes[0]
00072     
00073     security.declarePublic('urlsafe')
00074     def urlsafe(self):
00075         """Return a url safe version of the normalized version of this
00076         mime type.
00077         """
00078         return urllib.quote(self.normalized())
00079 
00080     security.declareProtected(ManagePortal, 'edit')
00081     def edit(self, name, mimetypes, extensions, icon_path,
00082              binary=0, globs=None, REQUEST=None):
00083         """edit this mime type"""
00084         # if mimetypes and extensions are string instead of lists,
00085         # split them on new lines
00086         if isinstance(mimetypes, basestring):
00087             mimetypes = [mts.strip() for mts in mimetypes.split('\n')
00088                          if mts.strip()]
00089         if isinstance(extensions, basestring):
00090             extensions = [mts.strip() for mts in extensions.split('\n')
00091                           if mts.strip()]
00092         if isinstance(globs, basestring):
00093             globs = [glob.strip() for glob in globs.split('\n')
00094                      if glob.strip()]
00095         self.__name__ = self.id = name
00096         self.mimetypes = mimetypes
00097         self.globs = globs
00098         self.extensions = extensions
00099         self.binary = binary
00100         self.icon_path = icon_path
00101         if REQUEST is not None:
00102             REQUEST['RESPONSE'].redirect(self.absolute_url()+'/manage_main')
00103 
00104 InitializeClass(MimeTypeItem)
00105 
00106 
00107 ICONS_DIR = os.path.join(os.path.dirname(__file__), 'skins', 'mimetypes_icons')
00108 
00109 def guess_icon_path(mimetype, icons_dir=ICONS_DIR, icon_ext='png'):
00110     if mimetype.extensions:
00111         for ext in mimetype.extensions:
00112             icon_path = '%s.%s' % (ext, icon_ext)
00113             if os.path.exists(os.path.join(icons_dir, icon_path)):
00114                 return icon_path
00115     icon_path = '%s.png' % mimetype.major()
00116     if os.path.exists(os.path.join(icons_dir, icon_path)):
00117         return icon_path
00118     return 'unknown.png'