Library class -> library module
I think that the library will be cleaner if I turn it into a module instead of keeping it a class.
This commit is contained in:
parent
f1292471f0
commit
58ad3c7a9d
|
@ -14,9 +14,16 @@ PLAYLIST = 1
|
||||||
QUEUE = 2
|
QUEUE = 2
|
||||||
|
|
||||||
import lens
|
import lens
|
||||||
library = lens.Library()
|
import library
|
||||||
|
#library = lens.Library()
|
||||||
playlist = lens.Playlist()
|
playlist = lens.Playlist()
|
||||||
|
|
||||||
|
file_to_id = library.file_to_id
|
||||||
|
get_attr = library.get_attr
|
||||||
|
reset = library.reset
|
||||||
|
inc_score = library.inc_score
|
||||||
|
inc_count = library.inc_count
|
||||||
|
|
||||||
def init():
|
def init():
|
||||||
libsaria.init_pref("random", False)
|
libsaria.init_pref("random", False)
|
||||||
libsaria.event.invite("POSTINIT", init)
|
libsaria.event.invite("POSTINIT", init)
|
||||||
|
@ -33,44 +40,13 @@ def new_source(path, bg=True):
|
||||||
return 0
|
return 0
|
||||||
return call("NEWSOURCE", library.scan, path)
|
return call("NEWSOURCE", library.scan, path)
|
||||||
|
|
||||||
def walk_library():
|
|
||||||
global library
|
|
||||||
for track in library.walk_tags():
|
|
||||||
yield track
|
|
||||||
|
|
||||||
def lib_reset():
|
|
||||||
global library
|
|
||||||
library.reset()
|
|
||||||
library.save()
|
|
||||||
|
|
||||||
def lib_get_attr(id, attr):
|
|
||||||
global library
|
|
||||||
if id >= 0:
|
|
||||||
return library.get_attr(id, attr)
|
|
||||||
|
|
||||||
def lib_inc_count(id):
|
|
||||||
global library
|
|
||||||
if id >= 0:
|
|
||||||
library.inc_count(id)
|
|
||||||
library.save()
|
|
||||||
|
|
||||||
def lib_inc_score(id, amount=1):
|
|
||||||
global library
|
|
||||||
if id >= 0:
|
|
||||||
library.inc_score(id, amount)
|
|
||||||
library.save()
|
|
||||||
|
|
||||||
def lib_play_id(id):
|
def lib_play_id(id):
|
||||||
global cur_lib_id
|
global cur_lib_id
|
||||||
cur_lib_id = id
|
cur_lib_id = id
|
||||||
filepath = lib_get_attr(id, "filepath")
|
filepath = library.get_attr(id, "filepath")
|
||||||
libsaria.music.load(filepath)
|
libsaria.music.load(filepath)
|
||||||
libsaria.music.play()
|
libsaria.music.play()
|
||||||
|
|
||||||
def lib_find_id(file):
|
|
||||||
global library
|
|
||||||
return library.find_id(file)
|
|
||||||
|
|
||||||
def lib_get_cur_id():
|
def lib_get_cur_id():
|
||||||
global cur_lib_id
|
global cur_lib_id
|
||||||
return cur_lib_id
|
return cur_lib_id
|
||||||
|
@ -101,9 +77,9 @@ def plist_add_libid(lib_id):
|
||||||
# Hey, Future Bryan. I really want to use a lib_get_attrs() function
|
# Hey, Future Bryan. I really want to use a lib_get_attrs() function
|
||||||
# to return a list of attributes. Can you do that for me, please?
|
# to return a list of attributes. Can you do that for me, please?
|
||||||
# Thank you, Past Bryan
|
# Thank you, Past Bryan
|
||||||
artist = lib_get_attr(lib_id, "artist")
|
artist = library.get_attr(lib_id, "artist")
|
||||||
album = lib_get_attr(lib_id, "album")
|
album = library.get_attr(lib_id, "album")
|
||||||
title = lib_get_attr(lib_id, "title")
|
title = library.get_attr(lib_id, "title")
|
||||||
playlist.insert_tags(artist, album, title, lib_id)
|
playlist.insert_tags(artist, album, title, lib_id)
|
||||||
|
|
||||||
def plist_is_visible(id):
|
def plist_is_visible(id):
|
||||||
|
@ -135,18 +111,18 @@ def choose_next():
|
||||||
def change_score():
|
def change_score():
|
||||||
prcnt = libsaria.music.get_progress()
|
prcnt = libsaria.music.get_progress()
|
||||||
if prcnt > 0.75:
|
if prcnt > 0.75:
|
||||||
lib_inc_count(cur_lib_id)
|
inc_count(cur_lib_id)
|
||||||
lib_inc_score(cur_lib_id)
|
inc_score(cur_lib_id)
|
||||||
if prcnt < 0.33:
|
if prcnt < 0.33:
|
||||||
lib_inc_score(cur_lib_id, -1)
|
inc_score(cur_lib_id, -1)
|
||||||
|
|
||||||
def plist_next():
|
def plist_next():
|
||||||
change_score()
|
change_score()
|
||||||
choose_next()
|
choose_next()
|
||||||
|
|
||||||
def catch_eos(*args):
|
def catch_eos(*args):
|
||||||
lib_inc_count(cur_lib_id)
|
inc_count(cur_lib_id)
|
||||||
lib_inc_score(cur_lib_id)
|
inc_score(cur_lib_id)
|
||||||
choose_next()
|
choose_next()
|
||||||
libsaria.event.invite("POSTEOS", catch_eos)
|
libsaria.event.invite("POSTEOS", catch_eos)
|
||||||
|
|
||||||
|
|
|
@ -96,7 +96,7 @@ class Playlist(collection.Collection):
|
||||||
def random(self):
|
def random(self):
|
||||||
if self.size == 0:
|
if self.size == 0:
|
||||||
return
|
return
|
||||||
getattr = libsaria.collection.lib_get_attr
|
getattr = libsaria.collection.get_attr
|
||||||
last = self.last_tracks
|
last = self.last_tracks
|
||||||
for i in xrange(15):
|
for i in xrange(15):
|
||||||
id = self.get_rand_candidate()
|
id = self.get_rand_candidate()
|
||||||
|
|
|
@ -0,0 +1,97 @@
|
||||||
|
# Bryan Schumaker (11/05/2010)
|
||||||
|
|
||||||
|
import libsaria
|
||||||
|
|
||||||
|
fs_tree = None
|
||||||
|
tag_tree = None
|
||||||
|
index = None
|
||||||
|
tracks = None
|
||||||
|
next_id = None
|
||||||
|
sources = None
|
||||||
|
size = None
|
||||||
|
|
||||||
|
def reset():
|
||||||
|
from tree import DLTree
|
||||||
|
from index import Index
|
||||||
|
fs_tree = DLTree()
|
||||||
|
tag_tree = DLTree()
|
||||||
|
index = Index()
|
||||||
|
tracks = dict()
|
||||||
|
next_id = 0
|
||||||
|
size = 0
|
||||||
|
sources = []
|
||||||
|
save()
|
||||||
|
|
||||||
|
def load():
|
||||||
|
global fs_tree
|
||||||
|
global tag_tree
|
||||||
|
global index
|
||||||
|
global tracks
|
||||||
|
global next_id
|
||||||
|
global sources
|
||||||
|
global size
|
||||||
|
objects = libsaria.data.load("library", ".dl_tree")
|
||||||
|
if objects == None or len(objects) != 6:
|
||||||
|
reset()
|
||||||
|
return
|
||||||
|
(fs_tree, tag_tree, index, tracks, next_id, size) = objects
|
||||||
|
load()
|
||||||
|
|
||||||
|
def save():
|
||||||
|
libsaria.data.save(
|
||||||
|
(fs_tree, tag_tree, index, tracks, next_id, size),
|
||||||
|
"library", ".dl_tree")
|
||||||
|
|
||||||
|
def walk_tags():
|
||||||
|
for tag in tag_tree.walk_forwards():
|
||||||
|
rec = tracks[tag[3]]
|
||||||
|
yield tag + [rec.year] + [rec.lenstr]
|
||||||
|
|
||||||
|
def file_to_id(file):
|
||||||
|
global fs_tree
|
||||||
|
stripped = file.strip(libsaria.path.sep)
|
||||||
|
split = stripped.split(libsaria.path.sep)
|
||||||
|
|
||||||
|
res = fs_tree.walk_path(split)
|
||||||
|
if res == False:
|
||||||
|
return None
|
||||||
|
return res[0]
|
||||||
|
|
||||||
|
def get_attr(id, attr):
|
||||||
|
rec = tracks.get(id, None)
|
||||||
|
if rec == None:
|
||||||
|
return None
|
||||||
|
if attr == "filepath":
|
||||||
|
cmp = rec.fs.walk_backwards()
|
||||||
|
return libsaria.path.sep.join([""] + cmp)
|
||||||
|
if attr == "playcount":
|
||||||
|
return rec.count
|
||||||
|
if attr == "year":
|
||||||
|
return rec.year
|
||||||
|
if attr == "lenstr":
|
||||||
|
return rec.lenstr
|
||||||
|
if attr == "score":
|
||||||
|
return rec.score
|
||||||
|
|
||||||
|
tags = rec.tags.walk_backwards()
|
||||||
|
if attr == "artist":
|
||||||
|
return tags[0]
|
||||||
|
if attr == "album":
|
||||||
|
return tags[1]
|
||||||
|
if attr == "title":
|
||||||
|
return tags[2]
|
||||||
|
|
||||||
|
def inc_score(id, amount=1):
|
||||||
|
rec = tracks.get(id, None)
|
||||||
|
if rec:
|
||||||
|
rec.score += amount
|
||||||
|
save()
|
||||||
|
|
||||||
|
def inc_count(id):
|
||||||
|
rec = tracks.get(id, None)
|
||||||
|
if rec:
|
||||||
|
rec.count += 1
|
||||||
|
save()
|
||||||
|
|
||||||
|
def is_visible(id):
|
||||||
|
return True
|
|
@ -54,7 +54,7 @@ def universal_open(file):
|
||||||
plugin.install(file)
|
plugin.install(file)
|
||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
id = libsaria.collection.lib_find_id(file)
|
id = libsaria.collection.file_to_id(file)
|
||||||
if id:
|
if id:
|
||||||
libsaria.collection.lib_play_id(id)
|
libsaria.collection.lib_play_id(id)
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -4,8 +4,9 @@ import libsaria
|
||||||
import web
|
import web
|
||||||
import xm
|
import xm
|
||||||
|
|
||||||
cache = libsaria.cache
|
file_to_id = libsaria.collection.file_to_id
|
||||||
pref_attr = xm.find_preferred_attribute
|
cache = libsaria.cache
|
||||||
|
pref_attr = xm.find_preferred_attribute
|
||||||
|
|
||||||
pref_sizes = ["extralarge", "large", "medium", "small"]
|
pref_sizes = ["extralarge", "large", "medium", "small"]
|
||||||
|
|
||||||
|
@ -61,11 +62,11 @@ def lfm_cache_album(file, artist, album):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def get_artwork(filepath):
|
def get_artwork(filepath):
|
||||||
id = libsaria.collection.lib_find_id(filepath)
|
id = file_to_id(filepath)
|
||||||
if id == None:
|
if id == None:
|
||||||
return
|
return
|
||||||
artist = libsaria.collection.lib_get_attr(id, "artist")
|
artist = libsaria.collection.get_attr(id, "artist")
|
||||||
album = libsaria.collection.lib_get_attr(id, "album")
|
album = libsaria.collection.get_attr(id, "album")
|
||||||
cached = cache[artist]
|
cached = cache[artist]
|
||||||
file = cached.get("%s.jpg" % album, lfm_cache_album, artist, album)
|
file = cached.get("%s.jpg" % album, lfm_cache_album, artist, album)
|
||||||
libsaria.event.start("POSTGETART", file)
|
libsaria.event.start("POSTGETART", file)
|
||||||
|
|
|
@ -6,6 +6,7 @@ import menu
|
||||||
|
|
||||||
libsaria = ocarina.libsaria
|
libsaria = ocarina.libsaria
|
||||||
from libsaria import collection
|
from libsaria import collection
|
||||||
|
library = collection.library
|
||||||
|
|
||||||
event = ocarina.libsaria.event
|
event = ocarina.libsaria.event
|
||||||
gtk = ocarina.gtk
|
gtk = ocarina.gtk
|
||||||
|
@ -81,7 +82,7 @@ class Library(Collection):
|
||||||
def populate(self):
|
def populate(self):
|
||||||
#import datetime
|
#import datetime
|
||||||
#before = datetime.datetime.now()
|
#before = datetime.datetime.now()
|
||||||
Collection.populate(self, collection.walk_library)
|
Collection.populate(self, library.walk_tags)
|
||||||
#after = datetime.datetime.now()
|
#after = datetime.datetime.now()
|
||||||
#print "Populating took: %s" % (after - before)
|
#print "Populating took: %s" % (after - before)
|
||||||
|
|
||||||
|
@ -90,17 +91,17 @@ class Library(Collection):
|
||||||
self.populate()
|
self.populate()
|
||||||
|
|
||||||
def reset(self):
|
def reset(self):
|
||||||
collection.lib_reset()
|
collection.reset()
|
||||||
self.clear()
|
self.clear()
|
||||||
|
|
||||||
def mouse_motion(self, row):
|
def mouse_motion(self, row):
|
||||||
return (collection.lib_get_attr(row[0], "playcount"),
|
return (collection.get_attr(row[0], "playcount"),
|
||||||
collection.lib_get_attr(row[0], "score"))
|
collection.get_attr(row[0], "score"))
|
||||||
|
|
||||||
def select_row(self, row):
|
def select_row(self, row):
|
||||||
collection.change_score()
|
collection.change_score()
|
||||||
collection.lib_play_id(row[0])
|
collection.lib_play_id(row[0])
|
||||||
collection.lib_inc_score(row[0], 1)
|
collection.inc_score(row[0], 1)
|
||||||
|
|
||||||
def filter(self, text):
|
def filter(self, text):
|
||||||
collection.lib_filter(text)
|
collection.lib_filter(text)
|
||||||
|
@ -138,13 +139,13 @@ class Playlist(Collection):
|
||||||
#print "Populating took: %s" % (after - before)
|
#print "Populating took: %s" % (after - before)
|
||||||
|
|
||||||
def mouse_motion(self, row):
|
def mouse_motion(self, row):
|
||||||
return (collection.lib_get_attr(row[0], "playcount"),
|
return (collection.get_attr(row[0], "playcount"),
|
||||||
collection.lib_get_attr(row[0], "score"))
|
collection.get_attr(row[0], "score"))
|
||||||
|
|
||||||
def select_row(self, row):
|
def select_row(self, row):
|
||||||
collection.change_score()
|
collection.change_score()
|
||||||
collection.lib_play_id(row[0])
|
collection.lib_play_id(row[0])
|
||||||
collection.lib_inc_score(row[0], 1)
|
collection.inc_score(row[0], 1)
|
||||||
|
|
||||||
def filter(self, text):
|
def filter(self, text):
|
||||||
collection.plist_filter(text)
|
collection.plist_filter(text)
|
||||||
|
|
|
@ -11,7 +11,8 @@ label = None
|
||||||
image = None
|
image = None
|
||||||
|
|
||||||
lib_get_cur_id = libsaria.collection.lib_get_cur_id
|
lib_get_cur_id = libsaria.collection.lib_get_cur_id
|
||||||
lib_get_attr = libsaria.collection.lib_get_attr
|
get_attr = libsaria.collection.get_attr
|
||||||
|
file_to_id = libsaria.collection.file_to_id
|
||||||
|
|
||||||
filter = None
|
filter = None
|
||||||
info = None
|
info = None
|
||||||
|
@ -72,9 +73,9 @@ class InfoBar(Bar):
|
||||||
libsaria.event.invite("POSTLOAD", self.change_title)
|
libsaria.event.invite("POSTLOAD", self.change_title)
|
||||||
|
|
||||||
def change_title(self, filepath):
|
def change_title(self, filepath):
|
||||||
id = libsaria.collection.lib_find_id(filepath)
|
id = file_to_id(filepath)
|
||||||
title = lib_get_attr(id, "title")
|
title = get_attr(id, "title")
|
||||||
artist = libsaria.collection.lib_get_attr(id, "artist")
|
artist = get_attr(id, "artist")
|
||||||
self.title.set_text("%s by %s" % (title,artist))
|
self.title.set_text("%s by %s" % (title,artist))
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -7,19 +7,19 @@ invite = ocarina.libsaria.event.invite
|
||||||
libsaria = ocarina.libsaria
|
libsaria = ocarina.libsaria
|
||||||
|
|
||||||
#update = None
|
#update = None
|
||||||
get_time = None
|
get_time = None
|
||||||
lib_get_attr = None
|
get_attr = None
|
||||||
lib_find_id = None
|
file_to_id = None
|
||||||
|
|
||||||
def set_fns():
|
def set_fns():
|
||||||
#global update
|
#global update
|
||||||
global get_time
|
global get_time
|
||||||
global lib_get_attr
|
global get_attr
|
||||||
global lib_find_id
|
global file_to_id
|
||||||
#update = ocarina.libsaria.music.get_progress
|
#update = ocarina.libsaria.music.get_progress
|
||||||
get_time = ocarina.libsaria.music.get_time
|
get_time = ocarina.libsaria.music.get_time
|
||||||
lib_get_attr = libsaria.collection.lib_get_attr
|
get_attr = libsaria.collection.get_attr
|
||||||
lib_find_id = libsaria.collection.lib_find_id
|
file_to_id = libsaria.collection.file_to_id
|
||||||
invite("POSTSTART", set_fns)
|
invite("POSTSTART", set_fns)
|
||||||
|
|
||||||
|
|
||||||
|
@ -45,11 +45,10 @@ class AttrLabel(gtk.Alignment):
|
||||||
invite("POSTLOAD", self.update)
|
invite("POSTLOAD", self.update)
|
||||||
|
|
||||||
def update(self, filepath):
|
def update(self, filepath):
|
||||||
global lib_find_id
|
global get_attr
|
||||||
global lib_get_attr
|
id = file_to_id(filepath)
|
||||||
id = lib_find_id(filepath)
|
|
||||||
if id != None:
|
if id != None:
|
||||||
text = str(lib_get_attr(id, self.attr))
|
text = str(get_attr(id, self.attr))
|
||||||
if self.other:
|
if self.other:
|
||||||
text = "%s %s" % (self.other, text)
|
text = "%s %s" % (self.other, text)
|
||||||
self.label.set_text(text)
|
self.label.set_text(text)
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
# Bryan Schumaker (10/30/2010)
|
# Bryan Schumaker (10/30/2010)
|
||||||
|
|
||||||
import ocarina
|
import ocarina
|
||||||
gdk = ocarina.gdk
|
gdk = ocarina.gdk
|
||||||
libsaria = ocarina.libsaria
|
libsaria = ocarina.libsaria
|
||||||
lib_find_id = libsaria.collection.lib_find_id
|
file_to_id = libsaria.collection.file_to_id
|
||||||
lib_get_attr = libsaria.collection.lib_get_attr
|
get_attr = libsaria.collection.get_attr
|
||||||
invite = libsaria.event.invite
|
invite = libsaria.event.invite
|
||||||
|
|
||||||
|
|
||||||
def tweak_icon(file):
|
def tweak_icon(file):
|
||||||
|
@ -17,8 +17,8 @@ def tweak_icon(file):
|
||||||
|
|
||||||
def tweak_title(filepath):
|
def tweak_title(filepath):
|
||||||
if filepath != None:
|
if filepath != None:
|
||||||
id = lib_find_id(filepath)
|
id = file_to_id(filepath)
|
||||||
title = lib_get_attr(id, "title")
|
title = get_attr(id, "title")
|
||||||
else:
|
else:
|
||||||
title = ocarina.__vers__
|
title = ocarina.__vers__
|
||||||
ocarina.set_window_title(title)
|
ocarina.set_window_title(title)
|
||||||
|
|
Loading…
Reference in New Issue