libsaria: Move playlist's list to new file

It will be easier to work with from a new file, especially after I put
in functions for doing basic operations on it.
This commit is contained in:
Bryan Schumaker 2011-05-11 23:05:47 -04:00
parent e2be97165e
commit 8bd914802e
2 changed files with 43 additions and 40 deletions

View File

@ -2,14 +2,12 @@
import libsaria import libsaria
import libsaria.path.files import libsaria.path.files
import playlist
library = None library = None
source = None source = None
rand = None rand = None
song_list = []
song_set = set(song_list)
filtered = False filtered = False
visible = None visible = None
recent = None recent = None
@ -17,21 +15,11 @@ recent_msg = "Skipping %s by %s because it has played recently"
skip_msg = "Skipping %s by %s because I don't think you like it" skip_msg = "Skipping %s by %s because I don't think you like it"
cur_index = None cur_index = None
def add_id(id): # Function pointers for convenience
global song_list add_ids = playlist.add_ids
global song_set
if id not in song_set:
song_list.append(id)
song_set.add(id)
def add_ids(ids):
for id in ids:
add_id(id)
save()
def rm_id(id): def rm_id(id):
song_list.remove(id) playlist.song_list.remove(id)
song_set.remove(id)
def rm_ids(ids): def rm_ids(ids):
for id in ids: for id in ids:
@ -52,20 +40,16 @@ def init():
source.save = save source.save = save
def reset(): def reset():
global song_list
global song_set
global recent global recent
global cur_index global cur_index
recent = [] recent = []
song_list = [] playlist.song_list = []
song_set = set()
cur_index = -1 cur_index = -1
def default_objects(): def default_objects():
return ([], [], -1) return ([], [], -1)
def load(): def load():
global song_list
global song_set global song_set
global recent global recent
global visible global visible
@ -74,12 +58,11 @@ def load():
if objects == None or len(objects) != 3: if objects == None or len(objects) != 3:
reset() reset()
else: else:
song_list = objects[0] playlist.song_list = objects[0]
recent = objects[1] recent = objects[1]
cur_index = objects[2] cur_index = objects[2]
song_set = set(song_list)
if cur_index >= 0: if cur_index >= 0:
libsaria.sources.cur_lib_id = song_list[cur_index] libsaria.sources.cur_lib_id = playlist.song_list[cur_index]
def init_bg(): def init_bg():
init() init()
@ -87,14 +70,13 @@ def init_bg():
def startup(): def startup():
if cur_index >= 0: if cur_index >= 0:
load_id(song_list[cur_index]) load_id(playlist.song_list[cur_index])
def save(): def save():
libsaria.storage.save_obj("playlist", (song_list, recent, cur_index)) libsaria.storage.save_obj("playlist", (playlist.song_list, recent, cur_index))
def walk(): def walk():
global song_list for id in playlist.song_list:
for id in song_list:
yield id yield id
def walk_playlist(*attrs): def walk_playlist(*attrs):
@ -108,10 +90,10 @@ def filter(text):
global song_set global song_set
global filtered global filtered
if len(text) > 0: if len(text) > 0:
visible = song_set.intersection(library.test_filter(text)) visible = playlist.as_set().intersection(library.test_filter(text))
filtered = True filtered = True
else: else:
visible = song_set visible = playlist.as_set()
filtered = False filtered = False
def is_visible(id): def is_visible(id):
@ -124,23 +106,22 @@ def is_visible(id):
def num_visible(): def num_visible():
if filtered == True: if filtered == True:
return len(visible) return len(visible)
return len(song_list) return len(playlist.song_list)
def get_cur_id(): def get_cur_id():
return song_list[cur_index] return playlist.song_list[cur_index]
def seq_next(): def seq_next():
global cur_index global cur_index
global song_list
cur_index += 1 cur_index += 1
max_index = len(song_list) max_index = len(playlist.song_list)
if cur_index == max_index: if cur_index == max_index:
cur_index = 0 cur_index = 0
while is_visible(song_list[cur_index]) == False: while is_visible(playlist.song_list[cur_index]) == False:
cur_index += 1 cur_index += 1
if cur_index == max_index: if cur_index == max_index:
cur_index = 0 cur_index = 0
return song_list[cur_index] return playlist.song_list[cur_index]
def rand_candidate(list, max): def rand_candidate(list, max):
index = rand.randint(0, max-1) index = rand.randint(0, max-1)
@ -154,7 +135,7 @@ def rand_next():
get_attrs = library.get_attrs get_attrs = library.get_attrs
if filtered == False: if filtered == False:
selection = song_list selection = playlist.song_list
else: else:
selection = list(visible) selection = list(visible)
@ -169,13 +150,13 @@ def rand_next():
continue continue
if i > 0: if i > 0:
print "Picking a song took %s iterations" % i print "Picking a song took %s iterations" % i
cur_index = song_list.index(id) cur_index = playlist.song_list.index(id)
return id return id
return id return id
def load_id(id): def load_id(id):
global cur_index global cur_index
cur_index = song_list.index(id) cur_index = playlist.song_list.index(id)
save() save()
path = library.get_attrs(id, "filepath") path = library.get_attrs(id, "filepath")
return source.load_file(path) return source.load_file(path)
@ -189,7 +170,7 @@ def play_id(id):
def next(): def next():
id = None id = None
if len(song_list) == 0: if len(playlist.song_list) == 0:
return return
if libsaria.prefs.get("libsaria.random") == True: if libsaria.prefs.get("libsaria.random") == True:
id = rand_next() id = rand_next()

View File

@ -0,0 +1,22 @@
# Bryan Schumaker (5 / 11 / 2011)
import threading
plist_lock = threading.Lock()
lock_plist = plist_lock.acquire
unlock_plist = plist_lock.release
# Playlist
song_list = []
song_set = set()
def add_ids(id_list):
lock_plist()
song_list.extend(id_list)
unlock_plist()
def as_set():
lock_plist()
song_set = set(song_list)
unlock_plist()
return song_set