From f063da5b5645fa07d97fe28847c7acc70b735ea9 Mon Sep 17 00:00:00 2001 From: Bryan Schumaker Date: Sun, 5 Dec 2010 17:11:41 -0500 Subject: [PATCH] Began work on virtual sources Virtual sources should make it easier to do things with the current song without having to know anything about it (such as a song id or file path). --- libsaria/audio/__init__.py | 9 ---- libsaria/controls.py | 42 +++++++++++++++++-- libsaria/path/lastfm.py | 2 +- libsaria/sources/__init__.py | 48 +++++++++++----------- libsaria/sources/library.py | 80 +++++++++++++++++++++++++----------- libsaria/sources/playlist.py | 42 ++++++++++++++----- ocarina/components/button.py | 2 +- ocarina/components/label.py | 4 +- ocarina/footer.py | 2 +- ocarina/library.py | 2 +- ocarina/playlist.py | 2 +- plugins/web_server.py | 2 +- plugins/wm_tweaks.py | 2 +- 13 files changed, 161 insertions(+), 78 deletions(-) diff --git a/libsaria/audio/__init__.py b/libsaria/audio/__init__.py index 386512ab..2cced114 100644 --- a/libsaria/audio/__init__.py +++ b/libsaria/audio/__init__.py @@ -27,15 +27,6 @@ def end_of_song(): return call("EOS", audio.reset) -def load(file): - global audio - global expand - file = expand(file) - if not exists(file): - return False - return call("LOAD", audio.load, file) - - def seek(prcnt): global audio return call("SEEK", audio.seek, prcnt) diff --git a/libsaria/controls.py b/libsaria/controls.py index 4d8c8433..24021617 100644 --- a/libsaria/controls.py +++ b/libsaria/controls.py @@ -1,11 +1,16 @@ # Bryan Schumaker (11/23/2010) libsaria = None +sources = None +library = None +playlist = None audio = None call = None invite = None prefs = None +expand = None +exists = None def init(): global libsaria @@ -17,13 +22,38 @@ def init2(): global audio global invite global prefs + global sources + global library + global playlist + global expand + global exists - call = libsaria.event.call - invite = libsaria.event.invite - prefs = libsaria.prefs + call = libsaria.event.call + invite = libsaria.event.invite + prefs = libsaria.prefs + expand = libsaria.path.expand + exists = libsaria.path.exists + sources = libsaria.sources + library = sources.library + playlist = sources.playlist from libsaria.audio import audio invite("PREEOS", pause) + invite("POSTEOS", catch_eos) + +def pick_next(): + file = playlist.next() + load(file) + play() + +def catch_eos(*args): + library.inc_count() + library.inc_score() + return call("NEXT", pick_next) + +def next(): + library.change_score() + return call("NEXT", pick_next) def play(): return call("PLAY", audio.play) @@ -39,3 +69,9 @@ def set_volume(prcnt): def toggle_rand(): prefs.set_pref("libsaria.random", not prefs.get_pref("libsaria.random")) + +def load(file): + file = expand(file) + if not exists(file): + return False + return call("LOAD", audio.load, file) diff --git a/libsaria/path/lastfm.py b/libsaria/path/lastfm.py index 02c5945d..245879fb 100644 --- a/libsaria/path/lastfm.py +++ b/libsaria/path/lastfm.py @@ -86,7 +86,7 @@ def set_artwork_tags(artist, album, path): libsaria.event.start("POSTSETART", file) def get_artwork_id(id): - artist, album = libsaria.sources.get_attrs(id, "artist", "album") + artist, album = libsaria.sources.library.get_attrs(id, "artist", "album") return get_artwork_tags(artist, album) def get_artwork(filepath): diff --git a/libsaria/sources/__init__.py b/libsaria/sources/__init__.py index f6432c59..948ae05a 100644 --- a/libsaria/sources/__init__.py +++ b/libsaria/sources/__init__.py @@ -3,22 +3,42 @@ __all__ = ["collection"] import libsaria -import tagpy call = libsaria.event.call exists = libsaria.path.exists expand = libsaria.path.expand -FileRef = tagpy.FileRef prefs = None import library import playlist -get_attrs = library.get_attrs reset = library.reset inc_score = library.inc_score inc_count = library.inc_count -cur_lib_id = -1 +controls = libsaria.controls + +sources = dict() +cur_source = None +class Source: + def __init__(self, name): + self.name = name + self.get_attrs = None + #self.reset = None + #self.load = None + #self.walk = None + #self.filter = None + #self.is_visible = None + #self.num_visible = None + sources[name] = self + + def load_file(self, path): + ret = controls.load(path) + cur_source = self.name + return ret + +def get_attrs(*attrs): + return sources[cur_source].get_attrs(attrs) + def new_source(path, bg=True): global library @@ -31,27 +51,7 @@ def lib_get_cur_id(): global cur_lib_id return cur_lib_id -def change_score(): - prcnt = libsaria.audio.get_progress() - if prcnt > 0.75: - inc_count(cur_lib_id) - inc_score(cur_lib_id) - if prcnt < 0.33: - inc_score(cur_lib_id, -1) - def play_selected_id(id): change_score() play_id(id) inc_score(id, 1) - -def plist_next(): - change_score() - playlist.next() - -def catch_eos(*args): - inc_count(cur_lib_id) - inc_score(cur_lib_id) - playlist.next() -libsaria.event.invite("POSTEOS", catch_eos) - - diff --git a/libsaria/sources/library.py b/libsaria/sources/library.py index d0dc6b16..52ae1fbc 100644 --- a/libsaria/sources/library.py +++ b/libsaria/sources/library.py @@ -1,14 +1,16 @@ # Bryan Schumaker (11/05/2010) -import os -import tagpy import libsaria +import libsaria.sources import libsaria.path.files -import string from libsaria import threads -from track import Track splitext = libsaria.path.splitext +os = None +tagpy = None +Track = None + +FileRef = None fs_tree = None tag_tree = None index = None @@ -16,19 +18,25 @@ tracks = None locations = None size = None visible = None +source = None -FileRef = tagpy.FileRef - +cur_id = -1 lib_lock = threads.get_mutex("library") loaded = False filtered = False badfiles = set() -ttable = dict() -for s in string.punctuation: - ttable[ord(s)] = u"" -for s in string.lowercase: - ttable[ord(s)] = ord(s) - 32 +def init(): + global source + import string + ttable = dict() + for s in string.punctuation: + ttable[ord(s)] = u"" + for s in string.lowercase: + ttable[ord(s)] = ord(s) - 32 + + source = libsaria.sources.Source("library") + source.get_attrs = get_attrs def reset(): from libsaria.trees import FSTree, DLFSTree, DLValTree @@ -75,12 +83,13 @@ def is_loaded(): lib_lock.release() return val -def load_bg2(callback): +def init_bg2(callback): + init() load() callback() -def load_bg(callback): - thr = threads.BG_Thread(load_bg2, callback) +def init_bg(callback): + thr = threads.BG_Thread(init_bg2, callback) thr.start() def save(): @@ -129,7 +138,12 @@ def song_id(artist, album, title): title = alb[title] return title.keys()[0] -def get_attrs(id, *attrs): +def get_attrs(*attrs): + if attrs[0].__class__ != str: + id = attrs[0] + attrs = attrs[1:] + else: + id = cur_id res = [] rec = tracks.get(id, None) if rec == None: @@ -153,6 +167,8 @@ def get_attrs(id, *attrs): res += [tags[2]] else: res += [get(attr, None)] + if len(res) == 1: + return res[0] return res def set_attr(id, attr, new_value): @@ -165,30 +181,38 @@ def set_attr(id, attr, new_value): else: rec.__dict__[attr] = new_value -def inc_score(id, amount=1): +def change_score(): + prcnt = libsaria.audio.get_progress() + if prcnt > 0.75: + inc_count(cur_id) + inc_score(cur_id) + if prcnt < 0.33: + inc_score(cur_id, -1) + +def inc_score(id = cur_id, amount=1): rec = tracks.get(id, None) if rec: rec.score += amount save() -def inc_count(id): +def inc_count(id = cur_id): rec = tracks.get(id, None) if rec: rec.count += 1 save() def play_id(id): - loaded = load_id(id) - if loaded == True: + if load_id(id) == True: libsaria.controls.play() - return loaded + return True + return False def load_id(id): + global cur_id if tracks.get(id, None) == None: return False - libsaria.sources.cur_lib_id = id - libsaria.audio.load(get_attrs(id, "filepath")[0]) - return True + cur_id = id + return source.load_file(get_attrs(id, "filepath")) def filter(text): global visible @@ -269,6 +293,16 @@ def update_path(path): print e def update(): + global os + global tagpy + global FileRef + global Track + + import os + import tagpy + from track import Track + FileRef = tagpy.FileRef + sep = libsaria.path.sep for path in locations: update_path(path) diff --git a/libsaria/sources/playlist.py b/libsaria/sources/playlist.py index f24943b1..9f1767d0 100644 --- a/libsaria/sources/playlist.py +++ b/libsaria/sources/playlist.py @@ -1,13 +1,16 @@ # Bryan Schumaker (11/07/2010) -import random as rand import libsaria import libsaria.path.files from libsaria import threads -from libsaria.sources import library call = libsaria.event.call + +library = None +source = None +rand = None + song_list = [] song_set = set(song_list) @@ -29,6 +32,16 @@ def rm_id(id): song_list.remove(id) song_set.remove(id) +def init(): + global library + global rand + global source + + import library + import random as rand + source = libsaria.sources.Source("playlist") + source.get_attrs = library.get_attrs + def reset(): global song_list global song_set @@ -56,16 +69,18 @@ def load(): if cur_index >= 0: libsaria.sources.cur_lib_id = song_list[cur_index] -def load_bg2(callback): +def init_bg2(callback): + init() load() while library.is_loaded() == False: pass callback() if cur_index >= 0: - library.load_id(song_list[cur_index]) + load_id(song_list[cur_index]) + #library.load_id(song_list[cur_index]) -def load_bg(callback): - thr = threads.BG_Thread(load_bg2, callback) +def init_bg(callback): + thr = threads.BG_Thread(init_bg2, callback) thr.start() def save(): @@ -147,11 +162,19 @@ def rand_next(): cur_index = index return id -def play_id(id): +def load_id(id): global cur_index cur_index = song_list.index(id) - library.play_id(id) save() + path = library.get_attrs(id, "filepath") + return source.load_file(path) + +def play_id(id): + if load_id(id) == True: + print "playing..." + libsaria.controls.play() + return True + return False def next(): id = None @@ -162,5 +185,4 @@ def next(): else: id = seq_next() if id != None: - save() - return call("NEXT", library.play_id, id) + return library.get_attrs(id, "filepath") diff --git a/ocarina/components/button.py b/ocarina/components/button.py index 6762df03..522edf20 100644 --- a/ocarina/components/button.py +++ b/ocarina/components/button.py @@ -73,7 +73,7 @@ class NextButton(Button): def __init__(self): Button.__init__(self, gtk.STOCK_MEDIA_NEXT) def clicked(self, button): - LS.sources.plist_next() + LS.controls.next() class ForwardButton(Button): diff --git a/ocarina/components/label.py b/ocarina/components/label.py index ad49f157..77161d65 100644 --- a/ocarina/components/label.py +++ b/ocarina/components/label.py @@ -15,7 +15,7 @@ def set_fns(): global get_attrs global file_id get_time = ocarina.libsaria.audio.get_time - get_attrs = libsaria.sources.get_attrs + get_attrs = libsaria.sources.library.get_attrs file_id = libsaria.path.file_id invite("POSTSTART", set_fns) @@ -45,7 +45,7 @@ class AttrLabel(gtk.Alignment): global get_attrs id = file_id(filepath) if id != None: - text = str(get_attrs(id, self.attr)[0]) + text = str(get_attrs(id, self.attr)) if self.other: text = "%s %s" % (self.other, text) self.label.set_text(text) diff --git a/ocarina/footer.py b/ocarina/footer.py index d8548817..1a995a59 100644 --- a/ocarina/footer.py +++ b/ocarina/footer.py @@ -20,7 +20,7 @@ pages = dict() def change_title(filepath): id = libsaria.path.file_id(filepath) - (ttl, artst) = libsaria.sources.get_attrs(id, "title", "artist") + (ttl, artst) = libsaria.sources.library.get_attrs(id, "title", "artist") title.set_text("%s by %s" % (ttl, artst)) def bar_add(widget, expand = False, fill = False): diff --git a/ocarina/library.py b/ocarina/library.py index bfce3fbe..aee75617 100644 --- a/ocarina/library.py +++ b/ocarina/library.py @@ -16,7 +16,7 @@ lib_page = source.Source() def init(): body.add_page("Library", lib_page) - library.load_bg(filler) + library.init_bg(filler) libsaria.event.invite("POSTNEWSOURCE", refresh) def filler(): diff --git a/ocarina/playlist.py b/ocarina/playlist.py index 70168292..3507029a 100644 --- a/ocarina/playlist.py +++ b/ocarina/playlist.py @@ -14,7 +14,7 @@ plist_page = source.Source() def init(): body.add_page("Playlist", plist_page) - playlist.load_bg(filler) + playlist.init_bg(filler) def filler(): plist_page.init(filter, is_visible, right_click, playlist.play_id, reset) diff --git a/plugins/web_server.py b/plugins/web_server.py index 4671099f..9210da65 100644 --- a/plugins/web_server.py +++ b/plugins/web_server.py @@ -94,7 +94,7 @@ class HTTPRequest(BaseHTTPRequestHandler): def audio_file(self): sid, ext = splitext(self.path.strip("/")) - fpath = library.get_attrs(long(sid), "filepath")[0] + fpath = library.get_attrs(long(sid), "filepath") self.other_file(fpath, ext) def other_file(self, path, ext): diff --git a/plugins/wm_tweaks.py b/plugins/wm_tweaks.py index 3ec3f2b1..8f97c380 100644 --- a/plugins/wm_tweaks.py +++ b/plugins/wm_tweaks.py @@ -19,7 +19,7 @@ def tweak_icon(file): def tweak_title(filepath): if filepath != None: id = file_id(filepath) - title = get_attrs(id, "title")[0] + title = get_attrs(id, "title") else: title = ocarina.__vers__ window.set_title(title)