[sugar] storing Activity parameters

Gary C Martin gary
Fri Mar 14 20:44:47 EDT 2008


On 3 Mar 2008, at 10:44, Tomeu Vizoso wrote:

> On Sun, Mar 2, 2008 at 1:51 PM, Gary C Martin <gary at garycmartin.com>  
> wrote:
>
>> What if both journal and file-system were used to store the
>> activity state, with the journal settings overriding the file-system
>> settings. This way a new activity start-up could inherit the last  
>> used
>> activity settings, and a specific journal start-up would inherit the
>> settings used for that specific entry. So in the case of Speak I  
>> could
>> have separate favourite journal entries for '3 eyed alien' and  
>> another
>> for 'Mr square eyes', then if I also just started the activity from
>> fresh I'd pick-up whatever the last used setting were (picked up from
>> the FS).
>
> I like this idea a lot. Thanks for sharing.

I just wanted to follow up on this activity preference storage idea  
from a few weeks ago, with some working code, as I've finally got  
around to writing/testing and all appears to work well. Feedback  
appreciated if I'm doing something dubious here. Activity defaults are  
hard coded, then overwritten by a persistent file (if it exists), then  
overwritten by a call to read_file (if the activity was started from  
the journal):

class MyActivity(activity.Activity):
	"""MyActivity main class."""
	def __init__(self, handle):
		activity.Activity.__init__(self, handle)
		self._name = handle
		self.set_title(_("MyActivity"))
		
		# Defaults (Journal resume priority, default persistent file  
secondary, fall-back hardcoded values)
		self.preferenceOne = 'foo'
		self.preferenceTwo = 'bar'
		self.activityState = {}
		self.activityState['preferenceOne'] = self.preferenceOne
		self.activityState['preferenceTwo'] = self.preferenceTwo
		self.parse_preferences(os.environ['SUGAR_ACTIVITY_ROOT'] + '/data/ 
defaults')

		#
		# Rest of MyActivity init code
		#

	def parse_preferences(self, file_path):
		"""Parse and set preference data from a given file."""
		try:
			f = file(file_path, 'r')
			self.activityState = cPickle.load(f)
			f.close()
			if 'preferenceOne' in self.activityState.keys():
				self. preferenceOne = self.activityState['preferenceOne']
			if 'preferenceTwo' in self.activityState.keys():
				self. preferenceTwo = self.activityState['preferenceTwo']
		except:
			print "preference data %s not available" % (file_path)

	def read_file(self, file_path):
		"""Read state from datastore."""
		self.parse_preferences(file_path)

		#
		# Update state of MyActivity interface if necessary
		#
		
	def write_file(self, file_path):
		"""Write state to journal datastore and to persistent file system."""
		self.activityState['preferenceOne'] = self. preferenceOne
		self.activityState['preferenceTwo'] = self. preferenceTwo
		toJournal = file(file_path, 'w')
		cPickle.dump(self.activityState, toJournal)
		toJournal.close()
		toPersistentFs = file(os.environ['SUGAR_ACTIVITY_ROOT'] + '/data/ 
defaults', 'w')
		cPickle.dump(self.activityState, toPersistentFs)
		toPersistentFs.close()

So... just to recap, when you first launch an activity, the hard coded  
settings are used. If you make changes and exit, they become the new  
defaults if you launch another. If you resume from a previous Journal  
entry its settings are honoured (and will become the new defaults when  
you exit). If your activity has lots of complicate preferences you  
might want to provide an option for resetting preferences back to hard  
coded defaults, but having lot's of complicated settings needing reset  
suggests you might need to simplify things a little for the target  
audience ;-)

If this looks like is might be useful/stable approach for some others  
to use in activities, let me know and I'll find somewhere on the wiki  
to document it more permanently.

G




More information about the Sugar-devel mailing list