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

class Prefs:

    def __init__(self, prefsfile=None):
        self.prefs = prefsfile
        self.manager=0
        self.team=[]
        self.title=''
        self.sections={}
        self.sectionorder=string.join(kDefFiles)
        for k in range(len(kDefFiles)):
            self.sections[kDefFiles[k]] = kDefSections[k]
        self.submitday = 4
        self.autosubmit = 0
        self.bugstat = 0
        self.codestat = 0
        self.newstat = 0
        self.revstat = 0
        self.rows = 4
        if prefsfile:
            self.load()

    def load(self):
        cfg = ConfigParser()
        cfg.read(self.prefs)
        try: self.submitday = cfg.get('DEFAULT', 'submitday')
        except NoOptionError: pass
        try: self.autosubmit = cfg.get('DEFAULT', 'autosubmit')
        except NoOptionError: pass
        try: self.manager = int(cfg.get('DEFAULT', 'manager'))
        except NoOptionError: pass
        try: self.bugstat = int(cfg.get('DEFAULT', 'bugstat'))
        except NoOptionError: pass
        try: self.codestat = int(cfg.get('DEFAULT', 'codestat'))
        except NoOptionError: pass
        try: self.revstat = int(cfg.get('DEFAULT', 'revstat'))
        except NoOptionError: pass
        try: self.newstat = int(cfg.get('DEFAULT', 'newstat'))
        except NoOptionError: pass
        try: self.rows = int(cfg.get('DEFAULT', 'rows'))
        except NoOptionError: pass
        try: self.team = string.split(cfg.get('DEFAULT', 'team'))
        except NoOptionError: pass
        try: self.title = cfg.get('DEFAULT', 'title')
        except NoOptionError: pass
        try:
            self.sectionorder = cfg.get('DEFAULT', 'sections')
            splitchar = ' '
            if ',' in self.sectionorder: splitchar = ','
            for sect in string.split(self.sectionorder, splitchar):
                self.sections[sect] = cfg.get('DEFAULT', sect)
        except NoOptionError: pass

    def save(self):
        cf = open(self.prefs, 'w')
        cf.write('# Auto generated! DO NOT EDIT!\n[DEFAULT]\n')
        cf.write('submitday:%s\n' % self.submitday)
        cf.write('autosubmit:%s\n' % self.autosubmit)
        cf.write('bugstat:%d\n' % self.bugstat)
        cf.write('codestat:%d\n' % self.codestat)
        cf.write('revstat:%d\n' % self.revstat)
        cf.write('newstat:%d\n' % self.newstat)
        cf.write('manager:%d\n' % self.manager)
        cf.write('rows:%s\n' % self.rows)
        cf.write('team:%s\n' % string.join(self.team))
        cf.write('title:%s\n' % self.title)
        cf.write('sections:%s\n' % self.sectionorder)
        for k in self.sections.keys():
            cf.write('%s:%s\n' % (k, self.sections[k]))
        cf.close()

    def needAutoStats(self):
        return self.bugstat or self.codestat or self.revstat or self.newstat

#quick and dirty functions--
def DefPrefs():
    from konst import kReportsDir
    from cookie import GetCookie
    return Prefs('%s%s/.prefs' % (kReportsDir, GetCookie('user')))

def Manager():
    return DefPrefs().manager

def Sections():
    return DefPrefs().sections

def SubmitDay():
    return DefPrefs().submitday

def Team():
    return DefPrefs().team

if __name__ == '__main__':
    p = Prefs('/home/reports/gagan/.prefs')
    print ('manager = %s' % p.manager)
    print ('team = %s' % p.team)
    print ('title = %s' % p.title)
    print ('sections = %s' % p.sections)
    p.prefs = '/tmp/prefstmp'
    p.save()
