Sugar.datastore.datastore: Difference between revisions
No edit summary |
|||
Line 19: | Line 19: | ||
finally: |
finally: |
||
f.close() |
f.close() |
||
=== How do I ensure that my activity loads file data and activity metadata from the datastore when it is accessed from the journal? === |
|||
For most activities, you can implement a read_file() method that will be used by the journal to load your activity. This method is called when the user choses to resume an activity that was running previously. Thus, after ensuring that you write all the necessary data using write_file(), you should implement a read_file() method that reads relevant file data and metadata. This information can then be used to set state variables for your activity so that it resumes in the earlier state chosen by the user. |
|||
The following code shows how both file data and activity metadata are read. Notice the call at the end of read_file that actually sets up the activity UI once the state of the activity class is set. |
|||
def write_file(self, file_path): |
|||
logging.debug('WRITING FILE ...') |
|||
self.metadata['current_page'] = '3' |
|||
f = open(file_path, 'w') |
|||
try: |
|||
f.write("Hello World") |
|||
finally: |
|||
f.close() |
|||
"""Read saved data from datastore and resume activity based on a previous state |
|||
""" |
|||
def read_file(self, file_path): |
|||
logging.debug('RELOADING ACTIVITY DATA...') |
|||
#load data from file for this datastore entry in to the data variable |
|||
data = self._get_data_from_file_path(file_path) |
|||
#Access different metadata mappings and change any relevant state |
|||
#variables for this activity instance. |
|||
cp = self.metadata['current_page'] |
|||
self._CURRENT_PAGE = int(cp) |
|||
#call any code that will actually initialize the activity with |
|||
#data that is read from datastore |
|||
self._create_ui() |
|||
"""This method simply returns data that is stored in a given |
|||
file_path""" |
|||
def _get_data_from_file_path(self, file_path): |
|||
fd = open(file_path, 'r') |
|||
try: |
|||
data = fd.read() |
|||
finally: |
|||
fd.close() |
|||
return data |
|||
= Notes = |
= Notes = |
Revision as of 21:14, 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()
How do I ensure that my activity loads file data and activity metadata from the datastore when it is accessed from the journal?
For most activities, you can implement a read_file() method that will be used by the journal to load your activity. This method is called when the user choses to resume an activity that was running previously. Thus, after ensuring that you write all the necessary data using write_file(), you should implement a read_file() method that reads relevant file data and metadata. This information can then be used to set state variables for your activity so that it resumes in the earlier state chosen by the user.
The following code shows how both file data and activity metadata are read. Notice the call at the end of read_file that actually sets up the activity UI once the state of the activity class is set.
def write_file(self, file_path): logging.debug('WRITING FILE ...') self.metadata['current_page'] = '3' f = open(file_path, 'w') try: f.write("Hello World") finally: f.close() """Read saved data from datastore and resume activity based on a previous state """ def read_file(self, file_path): logging.debug('RELOADING ACTIVITY DATA...')
#load data from file for this datastore entry in to the data variable data = self._get_data_from_file_path(file_path) #Access different metadata mappings and change any relevant state #variables for this activity instance. cp = self.metadata['current_page'] self._CURRENT_PAGE = int(cp)
#call any code that will actually initialize the activity with #data that is read from datastore self._create_ui()
"""This method simply returns data that is stored in a given file_path""" def _get_data_from_file_path(self, file_path): fd = open(file_path, 'r') try: data = fd.read() finally: fd.close() return data
Notes
<references />