diff --git a/libsaria/sources/playlist/__init__.py b/libsaria/sources/playlist/__init__.py index 2664a171..610a4567 100644 --- a/libsaria/sources/playlist/__init__.py +++ b/libsaria/sources/playlist/__init__.py @@ -2,14 +2,12 @@ import libsaria import libsaria.path.files +import playlist library = None source = None rand = None -song_list = [] -song_set = set(song_list) - filtered = False visible = 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" cur_index = None -def add_id(id): - global song_list - 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() +# Function pointers for convenience +add_ids = playlist.add_ids def rm_id(id): - song_list.remove(id) - song_set.remove(id) + playlist.song_list.remove(id) def rm_ids(ids): for id in ids: @@ -52,20 +40,16 @@ def init(): source.save = save def reset(): - global song_list - global song_set global recent global cur_index recent = [] - song_list = [] - song_set = set() + playlist.song_list = [] cur_index = -1 def default_objects(): return ([], [], -1) def load(): - global song_list global song_set global recent global visible @@ -74,12 +58,11 @@ def load(): if objects == None or len(objects) != 3: reset() else: - song_list = objects[0] + playlist.song_list = objects[0] recent = objects[1] cur_index = objects[2] - song_set = set(song_list) 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(): init() @@ -87,14 +70,13 @@ def init_bg(): def startup(): if cur_index >= 0: - load_id(song_list[cur_index]) + load_id(playlist.song_list[cur_index]) 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(): - global song_list - for id in song_list: + for id in playlist.song_list: yield id def walk_playlist(*attrs): @@ -108,10 +90,10 @@ def filter(text): global song_set global filtered if len(text) > 0: - visible = song_set.intersection(library.test_filter(text)) + visible = playlist.as_set().intersection(library.test_filter(text)) filtered = True else: - visible = song_set + visible = playlist.as_set() filtered = False def is_visible(id): @@ -124,23 +106,22 @@ def is_visible(id): def num_visible(): if filtered == True: return len(visible) - return len(song_list) + return len(playlist.song_list) def get_cur_id(): - return song_list[cur_index] + return playlist.song_list[cur_index] def seq_next(): global cur_index - global song_list cur_index += 1 - max_index = len(song_list) + max_index = len(playlist.song_list) if cur_index == max_index: cur_index = 0 - while is_visible(song_list[cur_index]) == False: + while is_visible(playlist.song_list[cur_index]) == False: cur_index += 1 if cur_index == max_index: cur_index = 0 - return song_list[cur_index] + return playlist.song_list[cur_index] def rand_candidate(list, max): index = rand.randint(0, max-1) @@ -154,7 +135,7 @@ def rand_next(): get_attrs = library.get_attrs if filtered == False: - selection = song_list + selection = playlist.song_list else: selection = list(visible) @@ -169,13 +150,13 @@ def rand_next(): continue if i > 0: 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 def load_id(id): global cur_index - cur_index = song_list.index(id) + cur_index = playlist.song_list.index(id) save() path = library.get_attrs(id, "filepath") return source.load_file(path) @@ -189,7 +170,7 @@ def play_id(id): def next(): id = None - if len(song_list) == 0: + if len(playlist.song_list) == 0: return if libsaria.prefs.get("libsaria.random") == True: id = rand_next() diff --git a/libsaria/sources/playlist/playlist.py b/libsaria/sources/playlist/playlist.py new file mode 100644 index 00000000..2192b48f --- /dev/null +++ b/libsaria/sources/playlist/playlist.py @@ -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