Back to index

plone3  3.1.7
smi_mimetypes.py
Go to the documentation of this file.
00001 import os
00002 from xml.sax import parse
00003 from xml.sax.handler import ContentHandler
00004 
00005 DIR = os.path.dirname(__file__)
00006 SMI_NAME = "freedesktop.org.xml"
00007 SMI_FILE = os.path.join(DIR, SMI_NAME)
00008 
00009 class SharedMimeInfoHandler(ContentHandler):
00010 
00011     current = None
00012     collect_comment = None
00013 
00014     def __init__(self):
00015         ContentHandler.__init__(self)
00016         self.mimes = []
00017 
00018     def startElement(self, name, attrs):
00019         if name in ('mime-type',):
00020             current = {'type': attrs['type'],
00021                        'comments': {},
00022                        'globs': [],
00023                        'aliases': []}
00024             self.mimes.append(current)
00025             self.current = current
00026             return
00027         if name in ('comment',):
00028             # If no lang, assume 'en'
00029             lang = attrs.get('xml:lang', 'en')
00030             if lang not in ('en',):
00031                 # Ignore for now.
00032                 return
00033             self.__comment_buffer = []
00034             self.__comment_lang = lang
00035             self.collect_comment = True
00036             return
00037         if name in ('glob',):
00038             globs = self.current['globs']
00039             globs.append(attrs['pattern'])
00040             return
00041         if name in ('alias',):
00042             aliases = self.current['aliases']
00043             aliases.append(attrs['type'])
00044 
00045     def endElement(self, name):
00046         if self.collect_comment and name in ('comment',):
00047             self.collect_comment = False
00048             lang = self.__comment_lang
00049             comment = u''.join(self.__comment_buffer)
00050             if not comment:
00051                 comment = self.current['type']
00052             self.current['comments'][lang] = comment
00053 
00054     def characters(self, contents):
00055         if self.collect_comment:
00056             self.__comment_buffer.append(contents)
00057 
00058 def readSMIFile(infofile):
00059     """Reads a shared mime info XML file
00060     """
00061     handler = SharedMimeInfoHandler()
00062     parse(infofile, handler)
00063     return handler.mimes
00064 
00065 mimetypes = readSMIFile(SMI_FILE)
00066 
00067 def initialize(registry):
00068     global mimetypes
00069     from Products.MimetypesRegistry.MimeTypeItem import MimeTypeItem
00070     from Products.MimetypesRegistry.common import MimeTypeException
00071     # Find things that are not in the specially registered mimetypes
00072     # and add them using some default policy, none of these will impl
00073     # iclassifier
00074     for res in mimetypes:
00075         mt = str(res['type'])
00076         mts = (mt,) + tuple(res['aliases'])
00077 
00078         # check the mime type
00079         try:
00080             mto =  registry.lookup(mt)
00081         except MimeTypeException:
00082             # malformed MIME type
00083             continue
00084 
00085         name = str(res['comments'].get(u'en', mt))
00086 
00087         # build a list of globs
00088         globs = []
00089         for glob in res['globs']:
00090             if registry.lookupGlob(glob):
00091                 continue
00092             else:
00093                 globs.append(glob)
00094 
00095         if mto:
00096             mto = mto[0]
00097             for glob in globs:
00098                 if not glob in mto.globs:
00099                     mto.globs = list(mto.globs) + [glob]
00100                     registry.register_glob(glob, mto)
00101             for mt in mts:
00102                 if not mt in mto.mimetypes:
00103                     mto.mimetypes = list(mto.mimetypes) + [mt]
00104                     registry.register_mimetype(mt, mto)
00105         else:
00106             isBin = mt.split('/', 1)[0] != "text"
00107             mti = MimeTypeItem(name, mimetypes=mts,
00108                                binary=isBin,
00109                                globs=globs)
00110             registry.register(mti)