Sugar almanac: Difference between revisions

From OLPC
Jump to navigation Jump to search
No edit summary
Line 26: Line 26:
* [[sugar.graphics.toolbutton]]
* [[sugar.graphics.toolbutton]]
* [[sugar.graphics.toolbox]]
* [[sugar.graphics.toolbox]]

= Clipboard =
* Notes on using [[GTK's Clipboard Module in Sugar]]


= Logging =
= Logging =

Revision as of 15:37, 25 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

How do I get additional help beyond this almanac?


Now, on to the actual almanac ...

Package: sugar

Package: sugar.activity

Package: sugar.datastore

Package: sugar.graphics

Clipboard

Logging

Internationalization

Internationalization in Sugar

Text and Graphics for Sugar Activities

MISCELLANEOUS

The tasks below are random useful techniques that have come up as I write code and documentation for this reference. They have yet to be categorized, but will be as a sufficient set of related entries are written.


How do I know when my activity is "active" or not?

You can set an event using the VISIBILITY_NOTIFY_MASK constant in order to know when your activity changes visibility. Then in the callback for this event, you simply compare the event's state to gtk-defined variables for activity visibility. See the GDK Visibility State Constants section of gtk.gdk.Constants for more information.

        #Notify when the visibility state changes by calling self._visibleNotifyCb
        #(PUT THIS IN YOUR ACTIVITY CODE - EG. THE __init__() METHOD)
        self.add_events(gtk.gdk.VISIBILITY_NOTIFY_MASK)
        self.connect("visibility-notify-event", self._visibleNotifyCb)
    ...
    #Callback method for when the activity's visibility changes
    def _visibleNotifyCb(self, widget, event):
        if (event.state == gtk.gdk.VISIBILITY_FULLY_OBSCURED):
            print "I am not visible"
        elif (event.state == gtk.gdk.VISIBILITY_UNOBSCURED):
            print "I am visible"

How do I get the amount of free space available on disk under the /home directory tree?

The following function uses the statvfs module. The following code demonstrates how to get the total amount of free space under /home.

    #### Method: getFreespaceKb, returns the available freespace in kilobytes. 
    def getFreespaceKb(self):
        stat = os.statvfs("/home")
        freebytes  = stat[statvfs.F_BSIZE] * stat[statvfs.F_BAVAIL]
        freekb = freebytes / 1024
        return freekb


How do I know whether my activity is running on a physical XO?

While your activity is typically going to be run on a real XO, there might be circumstances (such as for development through sugar-jhbuild) that you will not be running on an XO machine. The easiest way to tell if you are on a physical XO is to check whether /sys/power/olpc-pm, an essential power management file for the XO, exists. <ref>reliably detecting if running on an XO</ref> <ref>OLPC Power Management Interface</ref>

import os
...
      #Print out a boolean value that tells us whether we are on an XO or not. 
      print os.path.exists('/sys/power/olpc-pm')

How do I know the current language setting on my XO?

The system variable 'LANG' tells you which language is currently active on the XO. The following code shows how to look at the value of this variable.

import os
...
       _logger.debug(os.environ['LANG'])

How do I repeatedly call a specific method after N number of seconds?

The gobject.timeout_add() function allows you to invoke a callback method after a certain amount of time. If you want to repeatedly call a method, simply keep invoking the gobject.timeout_add function in your callback itself. The code below is a simple example, where the callback function is named repeatedly_call. Note that the timing of the callbacks are approximate. To get the process going, you should make an initial call to repeatedly_call() somewhere in your code.

You can see a more substantive example of this pattern in use when we regularly update the time displayed on a pango layout object.


	#This method calls itself ROUGHLY every 1 second 
	def repeatedly_call(self):
		now = datetime.datetime.now()
		gobject.timeout_add(self.repeat_period_msec, self.repeatedly_update_time)

Notes

<references />