import os, ConfigParser
cp = ConfigParser.ConfigParser
class preferences(cp):
types = {
bool: cp.getboolean,
float: cp.getfloat,
int: cp.getint,
str: cp.get,
repr: lambda self,section,option: eval(cp.get(self,section,option)),
}
def __init__(self):
cp.__init__(self)
self.fn = os.path.expanduser("~/.touchy_preferences")
self.read(self.fn)
def getpref(self, option, default=False, type=bool):
m = self.types.get(type)
try:
o = m(self, "DEFAULT", option)
except Exception, detail:
print detail
self.set("DEFAULT", option, default)
self.write(open(self.fn, "w"))
o = default
return o
def putpref(self, option, value, type=bool):
self.set("DEFAULT", option, type(value))
self.write(open(self.fn, "w"))