import os import cPickle as pickle from library import Library class Data: def __init__(self,options): path = os.path.join(options.user,".ocarina") #self.updateQ = False #self.updateList = False self.path = path self.song = None self.quit = None self.scrobbler = None if os.path.exists(self.path) == False: os.mkdir(self.path) self.load(path,options) #gobject.timeout_add(600000,self.dump) # Dump user data to a file def dump(self): libview = self.library.libview self.library.libview = None self.save(self.library,"library") self.library.libview = libview self.save([self.curList,self.curQ,self.curSong],"playlist") self.save([self.size,self.divider,self.colSizes,self.sortedCol,self.random],"preferences") self.save([self.lfm,self.lfmuser,self.lfmpass],"last.fm") def save(self,obj,file): out = open(os.path.join(self.path,file),'w') p = pickle.Pickler(out,1) p.dump(obj) out.close() # Read user data from the file # Create values if files do not exist def load(self,path,options): self.library = self.load2("library") if self.library == None: self.library = Library() plist = self.load2("playlist") if plist == None: self.curList = [] self.curQ = [] self.curSong = 0 #self.playingQ = False else: self.curList = plist[0] self.curQ = plist[1] self.curSong = plist[2] #self.playingQ = plist[3] prefs = self.load2("preferences") if prefs == None: self.size = (800,600) self.divider = 150 self.colSizes = [0,0,0,0,0] self.sortedCol = 3 self.random = False else: self.size = prefs [0] self.divider = prefs[1] self.colSizes = prefs[2] self.sortedCol = prefs[3] self.random = prefs[4] lfm = self.load2("last.fm") if lfm == None: self.lfm = "" self.lfmuser = "" self.lfmpass = "" else: self.lfm = lfm[0] self.lfmuser = lfm[1] self.lfmpass = lfm[2] def load2(self,file): path = os.path.join(self.path,file) if os.path.exists(path): p = pickle.Unpickler(open(path)) data = p.load() return data return None