sugar3.activity.activity module

Base class for activities written in Python

This is currently the only definitive reference for what an activity must do to participate in the Sugar desktop.

A Basic Activity

All activities must implement a class derived from ‘Activity’ in this class. The convention is to call it ActivitynameActivity, but this is not required as the activity.info file associated with your activity will tell the sugar-shell which class to start.

For example the most minimal Activity:

from sugar3.activity import activity

class ReadActivity(activity.Activity):
     pass

To get a real, working activity, you will at least have to implement:

__init__(), sugar3.activity.activity.Activity.read_file() and sugar3.activity.activity.Activity.write_file()

Aditionally, you will probably need a at least a Toolbar so you can have some interesting buttons for the user, like for example ‘exit activity’

See the methods of the Activity class below for more information on what you will need for a real activity.

Note

This API is STABLE.

class sugar3.activity.activity.Activity(handle, create_jobject=True)

Bases: sugar3.graphics.window.Window, gi.overrides.Gtk.Container

This is the base Activity class that all other Activities derive from. This is where your activity starts.

To get a working Activity:
  1. Derive your Activity from this class:
class MyActivity(activity.Activity):
    ...
  1. implement an __init__() method for your Activity class.

Use your init method to create your own ToolbarBox. This is the code to make a basic toolbar with the activity toolbar and a stop button.

from sugar3.graphics.toolbarbox import ToolbarBox
from sugar3.activity.widgets import ActivityToolbarButton
from sugar3.activity.widgets import StopButton

def __init__(self, handle):
    activity.Activity.__init__(self, handle)

    toolbar_box = ToolbarBox()
    activity_button = ActivityToolbarButton(self)
    toolbar_box.toolbar.insert(activity_button, 0)
    activity_button.show()

    ... Your toolbars ...

    separator = Gtk.SeparatorToolItem(draw=False)
    separator.set_expand(True)
    toolbar_box.toolbar.insert(separator, -1)
    separator.show()

    stop_button = StopButton(self)
    toolbar_box.toolbar.insert(stop_button, -1)
    stop_button.show()

    self.set_toolbar_box(toolbar_box)
    toolbar_box.show()

Add extra Toolbars to your toolbox.

You should setup Activity sharing here too.

Finaly, your Activity may need some resources which you can claim here too.

The __init__() method is also used to make the distinction between being resumed from the Journal, or starting with a blank document.

2. Implement sugar3.activity.activity.Activity.read_file() and sugar3.activity.activity.Activity.write_file() Most activities revolve around creating and storing Journal entries. For example, Write: You create a document, it is saved to the Journal and then later you resume working on the document.

sugar3.activity.activity.Activity.read_file() and sugar3.activity.activity.Activity.write_file() will be called by sugar to tell your Activity that it should load or save the document the user is working on.

  1. Implement our Activity Toolbars.

The Toolbars are added to your Activity in step 1 (the toolbox), but you need to implement them somewhere. Now is a good time.

There are a number of standard Toolbars. The most basic one, the one your almost absolutely MUST have is the ActivityToolbar. Without this, you’re not really making a proper Sugar Activity (which may be okay, but you should really stop and think about why not!) You do this with the ActivityToolbox(self) call in step 1.

Usually, you will also need the standard EditToolbar. This is the one which has the standard copy and paste buttons. You need to derive your own EditToolbar class from sugar3.activity.widgets.EditToolbar:

from sugar3.activity.widgets import EditToolbar

class MyEditToolbar(EditToolbar):
    ...

See EditToolbar for the methods you should implement in your class.

Finaly, your Activity will very likely need some activity specific buttons and options you can create your own toolbars by deriving a class from Gtk.Toolbar:

class MySpecialToolbar(Gtk.Toolbar):
...
  1. Use your creativity. Make your Activity something special and share it with your friends!

Read through the methods of the Activity class below, to learn more about how to make an Activity work.

Hint: A good and simple Activity to learn from is the Read activity. To create your own activity, you may want to copy it and use it as a template.

active
can_close()

Activities should override this function if they want to perform extra checks before actually closing.

canvas
Returns:Gtk.Widget: the widget used as canvas
Return type::class
close(skip_save=False)

Request that the activity be stopped and saved to the Journal

Activities should not override this method, but should implement write_file() to do any state saving instead. If the application wants to control wether it can close, it should override can_close().

Parameters:(bool) (skip_save) –
copy()

Request that the activity ‘Keep in Journal’ the current state of the activity.

Activities should not override this method. Instead, like save() do any copy work that needs to be done in write_file()

do_get_property(pspec)
do_set_property(pspec, value)
get_active()
get_activity_root()

Deprecated. This part of the API has been moved out of this class to the module itself

Returns:a path for saving Activity specific preferences, etc.
Return type:str

Returns a path to the location in the filesystem where the activity can store activity related data that doesn’t pertain to the current execution of the activity and thus cannot go into the DataStore.

Currently, this will return something like ~/.sugar/default/MyActivityName/

Activities should ONLY save settings, user preferences and other data which isn’t specific to a journal item here. If (meta-)data is in anyway specific to a journal entry, it MUST be stored in the DataStore.

get_bundle_id()
Returns:the bundle_id from the activity.info file
Return type:int
get_canvas()
Returns:Gtk.Widget: the widget used as canvas
Return type::class
get_document_path(async_cb, async_err_cb)
get_id()
Returns:the activity id of the current instance of your activity.

The activity id is sort-of-like the unix process id (PID). However, unlike PIDs it is only different for each new instance and stays the same everytime a user resumes an activity. This is also the identity of your Activity to other XOs for use when sharing.

Return type:int
get_max_participants()
Returns:the max number of users than can share a instance of the activity. Should be configured in the activity.info file.
Return type:int
get_metadata()
Returns:the jobject metadata or None if there is no jobject.
Return type:dict

Activities can set metadata in write_file() using:

self.metadata['MyKey'] = 'Something'

and retrieve metadata in read_file() using:

self.metadata.get('MyKey', 'aDefaultValue')

Note: Make sure your activity works properly if one or more of the metadata items is missing. Never assume they will all be present.

get_preview()
Returns:with data ready to save with an image representing the state of the activity. Generally this is what the user is seeing in this moment.
Return type:str

Activities can override this method, which should return a str with the binary content of a png image with a width of PREVIEW_SIZE pixels.

The method does create a cairo surface similar to that of the canvas’ window and draws on that. Then we create a cairo image surface with the desired preview size and scale the canvas surface on that.

get_shared()
Returns:True if the activity is shared on the mesh.
Return type:bool
get_shared_activity()
Returns:an instance of the shared Activity or None

The shared activity is of type sugar3.presence.activity.Activity

handle_view_source()

A developer can impleement this method to show aditional information in the View Source window. Example implementations are available on activities Browse or TurtleArt.

invite(account_path, contact_id)

Invite a buddy to join this Activity.

Parameters:
  • account_path
  • contact_id
Side Effects:
Calls self.share(True) to privately share the activity if it wasn’t shared before.
max_participants
Returns:the max number of users than can share a instance of the activity. Should be configured in the activity.info file.
Return type:int
metadata
Returns:the jobject metadata or None if there is no jobject.
Return type:dict

Activities can set metadata in write_file() using:

self.metadata['MyKey'] = 'Something'

and retrieve metadata in read_file() using:

self.metadata.get('MyKey', 'aDefaultValue')

Note: Make sure your activity works properly if one or more of the metadata items is missing. Never assume they will all be present.

notify_user(summary, body)

Display a notification with the given summary and body. The notification will go under the activities icon in the frame.

read_file(file_path)

Subclasses implement this method if they support resuming objects from the journal. ‘file_path’ is the file to read from.

You should immediately open the file from the file_path, because the file_name will be deleted immediately after returning from read_file(). Once the file has been opened, you do not have to read it immediately: After you have opened it, the file will only be really gone when you close it.

Although not required, this is also a good time to read all meta-data: the file itself cannot be changed externally, but the title, description and other metadata[‘tags’] may change. So if it is important for you to notice changes, this is the time to record the originals.

Parameters:str – the file path to read
run_main_loop()
save()

Request that the activity is saved to the Journal.

This method is called by the close() method below. In general, activities should not override this method. This method is part of the public API of an Acivity, and should behave in standard ways. Use your own implementation of write_file() to save your Activity specific data.

set_active(active)
set_canvas(canvas)

Sets the ‘work area’ of your activity with the canvas of your choice.

One commonly used canvas is Gtk.ScrolledWindow

Parameters:canvas (Gtk.Widget) – the widget used as canvas
set_max_participants(participants)
share(private=False)

Request that the activity be shared on the network.

Parameters:
  • private (bool) – True to share by invitation only,
  • to advertise as shared to everyone. (False) –

Once the activity is shared, its privacy can be changed by setting its ‘private’ property.

write_file(file_path)

Subclasses implement this method if they support saving data to objects in the journal. ‘file_path’ is the file to write to.

If the user did make changes, you should create the file_path and save all document data to it.

Additionally, you should also write any metadata needed to resume your activity. For example, the Read activity saves the current page and zoom level, so it can display the page.

Note: Currently, the file_path WILL be different from the one you received in file_read(). Even if you kept the file_path from file_read() open until now, you must still write the entire file to this file_path.

Parameters:file_path (str) – complete path of the file to write
sugar3.activity.activity.get_activity_root()
Returns:a path for saving Activity specific preferences, etc.
Return type:str
sugar3.activity.activity.get_bundle(bundle_id='', object_id='')
sugar3.activity.activity.get_bundle_name()
Returns:the bundle name for the current process’ bundle
Return type:str
sugar3.activity.activity.get_bundle_path()
Returns:the bundle path for the current process’ bundle
Return type:str
sugar3.activity.activity.launch_bundle(bundle_id='', object_id='')
sugar3.activity.activity.show_object_in_journal(object_id)