Sugar.mime

From OLPC
Revision as of 21:19, 1 July 2008 by Fanwar (talk | contribs)
Jump to navigation Jump to search

Helper Functions

Given a specific file, how do I figure out what its mime type is?

The mime.get_from_file_name() method lets you get the mime type based on the name of a given file. The mime.get_for_file() is a little more robust and can get mime types often without an extension.

from sugar import mime
...
       # Get the mime type for a specific file
        print mime.get_from_file_name('~/file-test/sample-music.mp3')

        # Get mime type for a file (does some more sophisticated checking on the file - not
        # dependent upon the file extension). 
        print mime.get_for_file('/home/fanwar/file-test/sample-acrobat')
        


How do I access the description of a specific mime type?

The following code will print out a description ('Text') for the 'text/plain' mime type. You can pass it any other mime types to get a basic description of those types to present to users.

from sugar import mime
...
        print mime.get_mime_description('text/plain')

How do I get the primary extension that is standard for a given mime type?

If you are saving or manipulating a new file and want to know the extension typically used for the mime type you are dealing with, then use mime.get_primary_extension().

from sugar import mime
...
        # Get the primary extension used for jpeg images
        print mime.get_primary_extension('image/jpeg')

Class: ObjectType