Debugging File Type (MIME) Associations

I’m having less and less time to blog and write stuff lately, so it’s a good oppertunity to catch up with old thing I did. Back in the happy days I used Gentoo, one of irritating issues I faced was messed up file type associations. MIME type for some files was recognized incorrectly, and as a result, KDE offered to open files with unsuitable applications. In order to debug it I wrote a small python script which would help me debug the way KDE applications are associated with MIME types and what MIME type is inferred form each file.

The script does so by querying the KMimeType and KMimeTypeTrader. The script does 3 things:

  • Given a MIME type, show it’s hierarchy and a list of applications associated with it.
  • Given an applications, list all MIME types it’s associated with
  • Given a file, show its MIME type (and also the accuracy, which allows one to know why that MIME type was selected, although I admit that in the two years since I wrote it, I forgot how it works :))

The script is pasted below. I hope someone that still fiddles with less than standard installations, will find it helpful.

import sys
import os.path
from optparse import OptionParser
from PyKDE4.kdecore import *
from PyQt4 import Qt

def mime_type_hierarchy(mime_type):
    ret = []
    while mime_type!='':
        ret.append(str(mime_type))
        mime_type = KMimeType.mimeType(mime_type).parentMimeType()
    return ret

def main(options, expr):
    if options.mime:
        print mime_type_hierarchy(expr)
        for app in KMimeTypeTrader.self().query(expr):
            print app.desktopEntryPath()
    elif options.application:
        for x in KService.serviceByStorageId(expr).serviceTypes():
            print x
    elif options.file:
        mime,accuracy = KMimeType.findByPath(expr)
        print "Mime-type: %s, Accuracy: %d" % (mime.name(), accuracy)

if __name__=="__main__":
    usage = "Usage: %prog [options] EXPR"
    version = "%prog-0.1\n2010 \xc2\xa9 Guy Rutenberg "
    parser = OptionParser(usage,version=version)
    parser.add_option("-m","--mime-type", dest="mime", default=False,
          action="store_true",
          help="Show the MIME type hierarchy and associated applications for a given MIME type")
    parser.add_option("-a","--application", dest="application", default=False,
          action="store_true",
          help="Print associated MIME types for a given application")
    parser.add_option("-f","--file", dest="file", default=False,
          action="store_true",
          help="See the MIME type associated with a given file")
    (options, args) = parser.parse_args(args=sys.argv)

    if len(args) < 2:
        sys.stderr.write(usage+"\n")
        sys.exit(1)
    # this is required to prevent a warning message when using KDE functions
    a = Qt.QApplication(sys.argv)
    main(options, *args[1:])

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.