libsaria: Move rand_next() to playlist's next.py

This keeps code for picking the next song in one place.  I also cleaned
up rand_next() while I was at it.
This commit is contained in:
Bryan Schumaker 2011-05-15 10:30:22 -04:00
parent 898f843ee0
commit 5a781479cd
2 changed files with 34 additions and 36 deletions

View File

@ -17,6 +17,7 @@ cur_index = None
add_ids = playlist.add_ids
rm_ids = playlist.rm_ids
reset = playlist.reset
to_list = list
playlist.load()
@ -50,37 +51,6 @@ def num_visible():
def get_cur_id():
return playlist.song_list[cur_index]
def rand_candidate(list, max):
index = random.randint(0, max-1)
return list[index]
def rand_next():
global cur_index
n = num_visible()
if n == 0:
return
get_attrs = library.get_attrs
if filtered == False:
selection = playlist.song_list
else:
selection = list(visible)
for i in xrange(15):
id = rand_candidate(selection, n)
(artist, title, like) = get_attrs(id, "artist", "title", "like")
if (artist, title) in recent:
print recent_msg % (artist, title)
continue
if like == False:
print skip_msg % (artist, title)
continue
if i > 0:
print "Picking a song took %s iterations" % i
cur_index = playlist.song_list.index(id)
return id
return id
def load_id(id):
global cur_index
cur_index = playlist.song_list.index(id)
@ -97,16 +67,18 @@ def play_id(id):
def get_next(cur_id):
id = None
if len(playlist.song_list) == 0:
return
if libsaria.prefs.get("libsaria.random") == True:
id = rand_next()
if filtered == False:
index = next.rand_next(playlist.song_list)
else:
index = next.rand_next(to_list(visible))
else:
index = next.seq_next(cur_id)
if index != None:
id = playlist.get(index)
if id != None:
artist, title, file = library.get_attrs(id, "artist", "title", "filepath")
recent.append((artist, title))
next.recent.append((artist, title))
if len(recent) > 50:
recent.pop(0)
return id

View File

@ -1,6 +1,10 @@
# Bryan Schumaker (5 / 14 / 2011)
import random
import playlist
from libsaria.sources import library
recent = []
def incr_index(cur_index, max_index):
cur_index += 1
@ -12,9 +16,31 @@ def seq_next(cur_id):
from libsaria import sources
is_visible = sources.playlist.is_visible
index = playlist.index(cur_id)
max = playlist.size()
if max == 0: # List is empty
return
index = playlist.index(cur_id)
index = incr_index(index, max)
while not is_visible(playlist.get(index)):
index = incr_index(index, max)
return index
def check_candidate(id):
(artist, title, like) = library.get_attrs(id, "artist", "title", "like")
if (artist, title) in recent:
return False
if like == False:
return False
return True
def rand_next(list):
max = len(list) - 1
if max == -1: # List is empty
return
for i in xrange(15):
index = random.randint(0, max)
id = list[index]
if check_candidate(id) == True:
break
return playlist.index(id)