Sugar.datastore.datastore: Difference between revisions

From OLPC
Jump to navigation Jump to search
No edit summary
Line 7: Line 7:
The following simple write_file() method shows how both metadata and files are written. Currently, write_file() will throw an error unless somewhere you actually write an actual file to the file_path that is passed to write_file. The code below writes a dummy file within the body of write_file itself (you can do this elsewhere as long as you have a handle on the file_path variable used by write_file).
The following simple write_file() method shows how both metadata and files are written. Currently, write_file() will throw an error unless somewhere you actually write an actual file to the file_path that is passed to write_file. The code below writes a dummy file within the body of write_file itself (you can do this elsewhere as long as you have a handle on the file_path variable used by write_file).


class AnnotateActivity(activity.Activity):
def write_file(self, file_path):
...
logging.debug('WRITING FILE ...')
def write_file(self, file_path):
#save some metadata
self.metadata['current_page'] = '3'
logging.debug('WRITING FILE ...')
#save the file itself
#save some metadata
f = open(file_path, 'w')
self.metadata['current_page'] = '3'
try:
#save the file itself
f.write("Hello World")
f = open(file_path, 'w')
finally:
try:
f.close()
f.write("Hello World")
finally:

f.close()




= Notes =
= Notes =

Revision as of 20:46, 13 June 2008

Briefly, what is the sugar datastore?

How do I save my activity data to the datastore?

Most basic activities can be frozen in the datastore by implementing the write_file() method within the main activity class. This method is then called by sugar to save the activity when it is closed or when user or program commands request that the activity be saved to the datastore. Once saved, the activity can be accessed and reloaded from the journal.

The following simple write_file() method shows how both metadata and files are written. Currently, write_file() will throw an error unless somewhere you actually write an actual file to the file_path that is passed to write_file. The code below writes a dummy file within the body of write_file itself (you can do this elsewhere as long as you have a handle on the file_path variable used by write_file).

 class AnnotateActivity(activity.Activity):
 ...
     def write_file(self, file_path):
         logging.debug('WRITING FILE ...')
         #save some metadata
         self.metadata['current_page'] = '3'
         #save the file itself
         f = open(file_path, 'w')
         try:
             f.write("Hello World")
         finally:
             f.close()

Notes

<references />