Sugar.activity.registry: Difference between revisions

From OLPC
Jump to navigation Jump to search
No edit summary
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{Sugar Almanac TOC}}

= Class: ActivityInfo =
= Class: ActivityInfo =
=== What are the all the things I can find out about an activity using an ActivityInfo object? ===
ActivityInfo objects are containers that contain metadata about different activities installed on an XO. The sample code prints out some of the most useful pieces of information that an ActivityInfo object will contain.


<pre>
from sugar.activity import registry
...
#Print out all the relevant info about this activity info object (ai is an ActivityInfo instance).
print ai.name
print ai.icon #path to the icon for this activity
print ai.bundle_id #like 'org.laptop.WebActivity'
print ai.version
print ai.path #path where this activity's bundle is saved
print ai.command
print ai.show_launcher
print ai.favorite
print ai.installation_time
</pre>


= Class: ActivityRegistry =
= Class: ActivityRegistry =
Line 12: Line 30:
from sugar.activity import registry
from sugar.activity import registry
...
...
#Create a new ActivityRegistry object
#Retrieve and ActivityRegistry Object
ar = registry.ActivityRegistry()
ar = registry.get_registry()


# use get_activities() to get a list of ActivityInfo objects and iterate through each
# use get_activities() to get a list of ActivityInfo objects and iterate through each
Line 27: Line 45:
from sugar.activity import registry
from sugar.activity import registry
...
...
#Create a new ActivityRegistry object
#Retrieve and ActivityRegistry Object
ar = registry.ActivityRegistry()
ar = registry.get_registry()


# search for activities with a name like 'annot'
# search for activities with a name like 'annot'
Line 36: Line 54:
print ai.name
print ai.name


</pre>

=== How do I get a list of all activities that handle a specific mime type? ===
Use the get_activities_for_type() method in ActivityRegistry, passing to it the mime type you are trying to open.

<pre>
from sugar.activity import registry
...
#Retrieve and ActivityRegistry Object
ar = registry.get_registry()

# search for activities that handle gif images - will get a list of ActivityInfo objects
aiList = ar.get_activities_for_type('image/gif')
</pre>
</pre>

Latest revision as of 15:28, 2 July 2008

Sugar Almanac for Developers

Sugar Almanac Main Page

Package: sugar

sugar.env

sugar.profile

sugar.mime

Package: sugar.activity

sugar.activity.activity

sugar.activity.registry

Package: sugar.graphics

sugar.graphics.alert

sugar.graphics.toolbutton

sugar.graphics.toolbox

Package: sugar.datastore

sugar.datastore.datastore

Logging

sugar.logger

Notes on using Python Standard Logging in Sugar

Internationalization

Internationalization in Sugar

Class: ActivityInfo

What are the all the things I can find out about an activity using an ActivityInfo object?

ActivityInfo objects are containers that contain metadata about different activities installed on an XO. The sample code prints out some of the most useful pieces of information that an ActivityInfo object will contain.

from sugar.activity import registry
...
            #Print out all the relevant info about this activity info object (ai is an ActivityInfo instance). 
            print ai.name 
            print ai.icon #path to the icon for this activity
            print ai.bundle_id #like 'org.laptop.WebActivity'
            print ai.version 
            print ai.path #path where this activity's bundle is saved
            print ai.command
            print ai.show_launcher
            print ai.favorite
            print ai.installation_time

Class: ActivityRegistry

The ActivityRegistry class can be used to help you get information about the activities that are installed on the current XO.

How do I get a list of all the activities that are available on an XO?

Use the get_activities() method in ActivityRegistry. This will return a list of ActivityInfo objects - one for each activity installed. You can then iterate through this list.

from sugar.activity import registry
...
        #Retrieve and ActivityRegistry Object
        ar = registry.get_registry()

        # use get_activities() to get a list of ActivityInfo objects and iterate through each
        for ai in ar.get_activities():
            print ai.name


How do I find all the activities whose name matches a certain string?

The find_activity() method in ActivityRegistry takes a string and returns a list of ActivityInfo objects, each of which represents an activity whose name matches the string passed. You can pass the full name of an activity (eg. 'Terminal', or 'Web') or you can pass part of the name (eg. 'term'). Note that this method may find multiple activities that match your search, so make sure you check for how many have been returned if you are looking for a specific activity. The find_activity() method is not case sensitive.

from sugar.activity import registry
...
        #Retrieve and ActivityRegistry Object
        ar = registry.get_registry()

        # search for activities with a name like 'annot'
        aiList = ar.find_activity('terminal')
        # for each ActivityInfo object (representing each activity) found, print out the activity name. 
        for ai in aiList:
            print ai.name

How do I get a list of all activities that handle a specific mime type?

Use the get_activities_for_type() method in ActivityRegistry, passing to it the mime type you are trying to open.

from sugar.activity import registry
...
        #Retrieve and ActivityRegistry Object
        ar = registry.get_registry()

        # search for activities that handle gif images - will get a list of ActivityInfo objects
        aiList = ar.get_activities_for_type('image/gif')