Changes
Jump to navigation
Jump to search
→MISCELLANEOUS
{{Sugar Almanac TOC}}
= How do I get additional help beyond this almanac? =
* Looking to get started with the basics of sugar development? Check out Christoph Derndorfer's [http://www.olpcaustria.org/mediawiki/index.php/Activity_handbook Activity Handbook].
Now, on to the actual almanac ...
= Package: sugar =
* [[sugar.env]]
* [[sugar.profile]]
* [[sugar.mime]]
* [[sugar.logger]]
= Package: sugar.activity =
* [[sugar.activity.activity]]
* [[sugar.activity.registry]]
= Package: sugar.datastore =
* [[sugar.datastore.datastore]]
= Package: sugar.graphics =
* [[sugar.graphics.alert]]
* [[sugar.graphics.toolbutton]]
* [[sugar.graphics.toolbox]]
= Logging =
* [[sugar.logger]]
* Notes on using [[Python Standard Logging in Sugar]]
= Internationalization =
[[Internationalization in Sugar]]
= 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 [http://www.pygtk.org/docs/pygtk/gdk-constants.html#gdk-visibility-state-constants GDK Visibility State Constants] section of gtk.gdk.Constants for more information.
<pre>
#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"
</pre>
=== How do I get the amount of free space available on disk under the /home directory tree? ===
The following function uses the [http://docs.python.org/lib/module-statvfs.html statvfs] module. The following code demonstrates how to get the total amount of free space under /home.
<pre>
#### 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
</pre>
=== 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>[http://lists.laptop.org/pipermail/devel/2008-June/015923.html reliably detecting if running on an XO]</ref> <ref>[http://wiki.laptop.org/go/Power_Management_Interface OLPC Power Management Interface]</ref>
= Notes =
* See also [[Sugar Code Snippets]]
<references />
= How do I get additional help beyond this almanac? =
* Looking to get started with the basics of sugar development? Check out Christoph Derndorfer's [http://www.olpcaustria.org/mediawiki/index.php/Activity_handbook Activity Handbook].
Now, on to the actual almanac ...
= Package: sugar =
* [[sugar.env]]
* [[sugar.profile]]
* [[sugar.mime]]
* [[sugar.logger]]
= Package: sugar.activity =
* [[sugar.activity.activity]]
* [[sugar.activity.registry]]
= Package: sugar.datastore =
* [[sugar.datastore.datastore]]
= Package: sugar.graphics =
* [[sugar.graphics.alert]]
* [[sugar.graphics.toolbutton]]
* [[sugar.graphics.toolbox]]
= Logging =
* [[sugar.logger]]
* Notes on using [[Python Standard Logging in Sugar]]
= Internationalization =
[[Internationalization in Sugar]]
= 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 [http://www.pygtk.org/docs/pygtk/gdk-constants.html#gdk-visibility-state-constants GDK Visibility State Constants] section of gtk.gdk.Constants for more information.
<pre>
#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"
</pre>
=== How do I get the amount of free space available on disk under the /home directory tree? ===
The following function uses the [http://docs.python.org/lib/module-statvfs.html statvfs] module. The following code demonstrates how to get the total amount of free space under /home.
<pre>
#### 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
</pre>
=== 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>[http://lists.laptop.org/pipermail/devel/2008-June/015923.html reliably detecting if running on an XO]</ref> <ref>[http://wiki.laptop.org/go/Power_Management_Interface OLPC Power Management Interface]</ref>
= Notes =
* See also [[Sugar Code Snippets]]
<references />