import os, ConfigParser
cp = ConfigParser.RawConfigParser
cp.optionxform=str
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,path=None):
cp.__init__(self)
if not path:
path="~/.gscreen_preferences"
self.fn = os.path.expanduser(path)
self.read(self.fn)
def getpref(self, option, default=False, type=bool, section="DEFAULT"):
m = self.types.get(type)
try:
o = m(self, section, option)
except Exception, detail:
print detail
try:
self.set(section, option, default)
except ConfigParser.NoSectionError:
print 'Adding section %s'%section
self.add_section(section)
self.set(section, option, default)
self.write(open(self.fn, "w"))
if type in(bool,float,int):
o = type(default)
else:
o = default
return o
def putpref(self, option, value, type=bool, section="DEFAULT"):
try:
self.set(section, option, type(value))
except ConfigParser.NoSectionError:
print 'Adding section %s'%section
self.add_section(section)
self.set(section, option, type(value))
self.write(open(self.fn, "w"))