ocarina/trunk/src/saveddata.py

82 lines
1.8 KiB
Python

import gobject
import os
import cPickle as pickle
from library import Library
from song import Song
class SavedData:
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
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):
self.save(self.library,"library")
self.save([self.curList,self.curQ,self.curSong,self.playingQ],"playlist")
self.save([self.size,self.divider,self.colSizes,self.sortedCol,self.random],"preferences")
self.save(self.lfm,"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 = [110,110,110,110,110]
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]
self.lfm = self.load2("last.fm")
if self.lfm == None:
self.lfm = ""
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