Back to index

plone3  3.1.7
py_mimetypes.py
Go to the documentation of this file.
00001 import os.path
00002 from Products.MimetypesRegistry.MimeTypeItem import MimeTypeItem
00003 from Products.MimetypesRegistry.MimeTypeItem import guess_icon_path
00004 from Products.MimetypesRegistry.common import MimeTypeException
00005 
00006 try:
00007     from zope.contenttype import add_files
00008 except ImportError: # BBB: Zope < 2.10
00009     try:
00010         from zope.app.content_types import add_files
00011     except ImportError: # BBB: Zope < 2.9
00012         from OFS.content_types import add_files
00013 
00014 import mimetypes as pymimetypes
00015 
00016 mimes_initialized = False
00017 
00018 def mimes_initialize():
00019     global mimes_initialized
00020     if mimes_initialized:
00021         return
00022     mimes_initialized = True
00023     # Augment known mime-types.
00024     here = os.path.dirname(os.path.abspath(__file__))
00025     add_files([os.path.join(here, 'mime.types')])
00026 
00027 # don't register the mimetype from python mimetypes if matching on of
00028 # this extensions.
00029 skip_extensions = (
00030     )
00031 
00032 def initialize(registry):
00033     # Find things that are not in the specially registered mimetypes
00034     # and add them using some default policy, none of these will impl
00035     # iclassifier
00036 
00037     # Read our included mime.types file, in addition to whatever the
00038     # mimetypes python module might have found.
00039     mimes_initialize()
00040 
00041     # Initialize from registry known mimetypes if we are on Windows
00042     # and pywin32 is available.
00043     try:
00044         from windows_mimetypes import initialize
00045         initialize()
00046     except ImportError:
00047         pass
00048     
00049     for ext, mt in pymimetypes.types_map.items():
00050         if not ext:
00051             continue
00052         if ext.startswith('.'):
00053             ext = ext[1:]
00054         if registry.lookupExtension(ext):
00055             continue
00056         if ext in skip_extensions:
00057             continue
00058 
00059         try:
00060             mto =  registry.lookup(mt)
00061         except MimeTypeException:
00062             # malformed MIME type
00063             continue
00064         if mto:
00065             mto = mto[0]
00066             if not ext in mto.extensions:
00067                 registry.register_extension(ext, mto)
00068                 mto.extensions += (ext, )
00069                 # here we guess icon path again, to find icon match the new ext
00070                 mto.icon_path = guess_icon_path(mto)
00071             continue
00072         isBin = mt.split('/', 1)[0] != "text"
00073         registry.register(MimeTypeItem(mt, (mt,), (ext,), isBin))