Sugar-api-doc: Difference between revisions

From OLPC
Jump to navigation Jump to search
No edit summary
No edit summary
Line 23: Line 23:
"Every activity in the Sugar environment is packaged into a self-contained 'bundle'. The bundle contains all the resources and executable code (other than system-provided base libraries) which the activity needs to execute. Any resources or executable code that is not provided by the base system just be packaged within the bundle." <ref>[http://www.olpcaustria.org/mediawiki/index.php/Activity_handbook OLPC Austria - Activity Handbook for Sugar]</ref>
"Every activity in the Sugar environment is packaged into a self-contained 'bundle'. The bundle contains all the resources and executable code (other than system-provided base libraries) which the activity needs to execute. Any resources or executable code that is not provided by the base system just be packaged within the bundle." <ref>[http://www.olpcaustria.org/mediawiki/index.php/Activity_handbook OLPC Austria - Activity Handbook for Sugar]</ref>


At present, the most direct way to get a handle on the directory where your activity is stored is to use the environment variable 'SUGAR_BUNDLE_PATH'. The following code retrieves the bundle path and prints it to screen - you can reuse it and do whatever you like to save files or manipulate data in your activity bundle directory.
At present, the most direct way to get a handle on the directory where your activity code is saved is to use the activity.get_bundle_path() method. The following code retrieves the bundle path and prints it to screen - you can reuse it and do whatever you like to save files or manipulate data in your activity bundle directory.


import os
from sugar.activity import activity
...
...
environ = os.environ.copy()
print activity.get_bundle_path()
print environ['SUGAR_BUNDLE_PATH']
...
...


=== How do I get the file path where I can write files programmatically? ===
=== How do I get the file path where I can write files programmatically? ===


The activity package also has a activity.get_activity_root() helper function that gets the root directory where your activity may write files. There are three specific subdirectories within your activity root: instance, data and tmp.
This is the same process as getting your activity bundle path.


import os
from sugar.activity import activity
...
import os
environ = os.environ.copy()
...
#print out the root directory where this activity will be writing
print environ['SUGAR_ACTIVITY_ROOT']
...
print activity.get_activity_root()

#print out the names of the different root subdirectories to which
#I can write.
print os.path.join(activity.get_activity_root(), "instance")
print os.path.join(activity.get_activity_root(), "data")
print os.path.join(activity.get_activity_root(), "tmp")


= Notes =
= Notes =

Revision as of 14:21, 13 June 2008

How do I get additional help beyond this almanac?

  • Looking to get started with the basics of sugar development? Check out Christoph Derndorfer's Activity Handbook.

Now, on to the actual almanac ...

Package: sugar.activity

Package: sugar.graphics

Package: sugar.datastore

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 get the file path for my activity bundle?

In the sugar model, all files needed to run an activity (the python code, icons, etc.) should be located under one directory:

"Every activity in the Sugar environment is packaged into a self-contained 'bundle'. The bundle contains all the resources and executable code (other than system-provided base libraries) which the activity needs to execute. Any resources or executable code that is not provided by the base system just be packaged within the bundle." <ref>OLPC Austria - Activity Handbook for Sugar</ref>

At present, the most direct way to get a handle on the directory where your activity code is saved is to use the activity.get_bundle_path() method. The following code retrieves the bundle path and prints it to screen - you can reuse it and do whatever you like to save files or manipulate data in your activity bundle directory.

   from sugar.activity import activity
   ...
       print activity.get_bundle_path()
       ...

How do I get the file path where I can write files programmatically?

The activity package also has a activity.get_activity_root() helper function that gets the root directory where your activity may write files. There are three specific subdirectories within your activity root: instance, data and tmp.

    from sugar.activity import activity
    import os
    ...   
       #print out the root directory where this activity will be writing
       print activity.get_activity_root()
       #print out the names of the different root subdirectories to which 
       #I can write. 
       print os.path.join(activity.get_activity_root(), "instance")
       print os.path.join(activity.get_activity_root(), "data")
       print os.path.join(activity.get_activity_root(), "tmp")

Notes

<references />