#!/usr/bin/python
"""Reads/writes the prefs"""
from ConfigParser import *
import string

class Prefs:

    def __init__(self, prefsfile='.prefs'):
        self.prefs = prefsfile
        self.teamtitle=''
        self.team=[]
        if prefsfile:
            self.load()

    def load(self):
        cfg = ConfigParser()
        cfg.read(self.prefs)
        try: self.teamtitle = cfg.get('DEFAULT', 'teamtitle')
        except NoOptionError: pass
        try: self.team= string.split(cfg.get('DEFAULT', 'team'))
        except NoOptionError: pass

    def save(self):
        # I have to write this since ConfigParser write isn't good enuf (yet)
        self.team.sort() # sort team before saving
        cf = open(self.prefs, 'w')
        cf.write('# Auto generated! DO NOT EDIT!\n[DEFAULT]\n')
        cf.write('teamtitle:%s\n' % self.teamtitle)
        cf.write('team:%s\n' % string.join(self.team))
        cf.close()

if __name__ == '__main__':
    p = Prefs(None);
    p.teamtitle= 'test title'
    p.prefs = '/tmp/prefs'
    p.save()
    p = None
    p = Prefs('/tmp/prefs')
    print ('teamtitle = %s' % p.teamtitle)
    print ('remember to cleanup /tmp/prefs')
