Changes

no edit summary
{{Sugar Almanac}}
{{Sugar Almanac TOC}}
= Helper Functions =

=== How are mime types represented in sugar and how do I get a list of the basic generic mime types? ===

The mime package in sugar has an ObjectType class that represents a specific type of data (image, text, video, etc.). If you use the mime.get_all_generic_types() method, you will be able to see the different data types standard in sugar and the mime types that these data types can have.

A specific data type may be saved in several mime type formats. For example, text can be of type 'text/plain', 'application/pdf' and several other standards. The code below gets all the generic types available and then prints out the information contained in the ObjectType instance for each type.
<pre>
from sugar import mime
...
# Get a list of ObjectType objects representing all the generic types
types = mime.get_all_generic_types()

# Cycle through each type and print out relevant information by accessing
# fields in ObjectType
for ot in types:
print '------- BEGIN -------'
print ot.type_id
print ot.name
print ot.icon
print ot.mime_types
print '--------- END --------'
</pre>


You can also use the mime.get_generic_type() method to get a single ObjectType instance for a given type (eg. 'Text'). The code below does just this - it retrieves all the generic mime types for Text files that are known:

<pre>
from sugar import mime
...
# Look up the different mime types available for text files.
text_type = mime.get_generic_type('Text')

print 'Here are the mime types known for text files: '
for mime_type in text_type.mime_types:
print mime_type
</pre>
=== 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.

<pre>
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')

</pre>


=== 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.

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

=== 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().

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

= Class: ObjectType =
Anonymous user