Back to index

plone3  3.1.7
windows_mimetypes.py
Go to the documentation of this file.
00001 # Utilities for mime-types and the Windows registry.
00002 import _winreg
00003 import win32api
00004 import win32con
00005 import mimetypes
00006 
00007 import logging
00008 logger = logging.getLogger('mimetypes.win32')
00009 
00010 # "safely" query a value, returning a default when it doesn't exist.
00011 def _RegQueryValue(key, value, default=None):
00012     try:
00013         data, typ = win32api.RegQueryValueEx(key, value)
00014     except win32api.error:
00015         return default
00016     if typ == win32con.REG_EXPAND_SZ:
00017         data = win32api.ExpandEnvironmentStrings(data)
00018     if type in (win32con.REG_EXPAND_SZ, win32con.REG_SZ):
00019         # Occasionally see trailing \0 chars.
00020         data = data.rstrip('\0')
00021     return data
00022 
00023 def get_desc_for_mimetype(mime_type):
00024     try:
00025         hk = win32api.RegOpenKey(win32con.HKEY_CLASSES_ROOT,
00026                                  r"MIME\Database\Content Type\\" + mime_type)
00027         desc = _RegQueryValue(hk, "")
00028     except win32api.error, details:
00029         logger.info("win32api error fetching description for mime-type %r: %s",
00030                      mime_type, details)
00031         desc = None
00032     logger.debug("mime-type %s has description %s", mime_type, desc)
00033     return desc
00034 
00035 def get_ext_for_mimetype(mime_type):
00036     try:
00037         hk = win32api.RegOpenKey(win32con.HKEY_CLASSES_ROOT,
00038                                  r"MIME\Database\Content Type\\" + mime_type)
00039         ext = _RegQueryValue(hk, "Extension")
00040     except win32api.error, details:
00041         logger.info("win32api error fetching extension for mime-type %r: %s",
00042                      mime_type, details)
00043         ext = None
00044     logger.debug("mime-type %s has extension %s", mime_type, ext)
00045     return ext
00046 
00047 def get_mime_types():
00048     try:
00049         hk = win32api.RegOpenKey(win32con.HKEY_CLASSES_ROOT,
00050                                  r"MIME\Database\Content Type")
00051         items = win32api.RegEnumKeyEx(hk)
00052     except win32api.error, details:
00053         logger.info("win32api error fetching mimetypes: %s",
00054                     details)
00055         items = []
00056     return [i[0] for i in items if i[0]]
00057 
00058 def normalize(mt):
00059     # Some mimetypes might have extra ';q=value' params.
00060     return mt.lower().split(';')[0]
00061 
00062 def initialize():
00063     if not mimetypes.inited:
00064         mimetypes.init()
00065 
00066     for mt in get_mime_types():
00067         ext = get_ext_for_mimetype(mt)
00068         if not ext:
00069             continue
00070         if not mimetypes.types_map.has_key(ext):
00071             mimetypes.add_type(normalize(mt), ext)
00072 
00073 if __name__=='__main__':
00074     for mt in get_mime_types():
00075         ext = get_ext_for_mimetype(mt)
00076         desc = get_desc_for_mimetype(mt)
00077         print "%s (%s) - %s" % (mt.lower(), desc, ext)
00078     import code; code.interact(local=locals())