libsaria: Move playlist load, save, and reset functions

I moved them to playlist.py so they can grab the lock and safely do
stuff.  I save after adding or removing song ids.  I also save after
resetting the playlist.
This commit is contained in:
Bryan Schumaker 2011-05-12 22:34:36 -04:00
parent 64c6677263
commit 132a66271c
2 changed files with 23 additions and 29 deletions

View File

@ -18,6 +18,8 @@ cur_index = None
# Function pointers for convenience
add_ids = playlist.add_ids
rm_ids = playlist.rm_ids
load = playlist.load
reset = playlist.reset
def init():
global library
@ -30,32 +32,6 @@ def init():
source.get_attrs = library.get_attrs
source.set_attr = library.set_attr
source.get_cur_id = get_cur_id
source.save = save
def reset():
global recent
global cur_index
recent = []
playlist.song_list = []
cur_index = -1
def default_objects():
return ([], [], -1)
def load():
global song_set
global recent
global visible
global cur_index
objects = libsaria.storage.load_obj("playlist", default_objects)
if objects == None or len(objects) != 3:
reset()
else:
playlist.song_list = objects[0]
recent = objects[1]
cur_index = objects[2]
if cur_index >= 0:
libsaria.sources.cur_lib_id = playlist.song_list[cur_index]
def init_bg():
init()
@ -65,9 +41,6 @@ def startup():
if cur_index >= 0:
load_id(playlist.song_list[cur_index])
def save():
libsaria.storage.save_obj("playlist", (playlist.song_list, recent, cur_index))
def list():
for id in playlist.song_list:
yield id

View File

@ -1,6 +1,7 @@
# Bryan Schumaker (5 / 11 / 2011)
import threading
from libsaria import storage
plist_lock = threading.Lock()
lock_plist = plist_lock.acquire
unlock_plist = plist_lock.release
@ -9,10 +10,29 @@ unlock_plist = plist_lock.release
song_list = []
song_set = set()
def load():
global song_list
plist_list = storage.load_obj("playlist", list)
if plist_list.__class__ != list:
plist_list = []
lock_plist()
song_list = plist_list
unlock_plist()
def save():
storage.save_obj("playlist", song_list)
def reset():
global song_list
lock_plist()
song_list = []
save()
unlock_plist()
def add_ids(id_list):
lock_plist()
song_list.extend(id_list)
save()
unlock_plist()
def rm_ids(id_list):
@ -20,6 +40,7 @@ def rm_ids(id_list):
for id in id_list:
if id in song_list:
song_list.remove(id)
save()
unlock_plist()
def as_set():