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:
Bryan Schumaker 2010-11-05 21:52:10 -04:00
parent f1292471f0
commit 58ad3c7a9d
9 changed files with 154 additions and 79 deletions

View File

@ -14,9 +14,16 @@ PLAYLIST = 1
QUEUE = 2
import lens
library = lens.Library()
import library
#library = lens.Library()
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():
libsaria.init_pref("random", False)
libsaria.event.invite("POSTINIT", init)
@ -33,44 +40,13 @@ def new_source(path, bg=True):
return 0
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):
global cur_lib_id
cur_lib_id = id
filepath = lib_get_attr(id, "filepath")
filepath = library.get_attr(id, "filepath")
libsaria.music.load(filepath)
libsaria.music.play()
def lib_find_id(file):
global library
return library.find_id(file)
def lib_get_cur_id():
global 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
# to return a list of attributes. Can you do that for me, please?
# Thank you, Past Bryan
artist = lib_get_attr(lib_id, "artist")
album = lib_get_attr(lib_id, "album")
title = lib_get_attr(lib_id, "title")
artist = library.get_attr(lib_id, "artist")
album = library.get_attr(lib_id, "album")
title = library.get_attr(lib_id, "title")
playlist.insert_tags(artist, album, title, lib_id)
def plist_is_visible(id):
@ -135,18 +111,18 @@ def choose_next():
def change_score():
prcnt = libsaria.music.get_progress()
if prcnt > 0.75:
lib_inc_count(cur_lib_id)
lib_inc_score(cur_lib_id)
inc_count(cur_lib_id)
inc_score(cur_lib_id)
if prcnt < 0.33:
lib_inc_score(cur_lib_id, -1)
inc_score(cur_lib_id, -1)
def plist_next():
change_score()
choose_next()
def catch_eos(*args):
lib_inc_count(cur_lib_id)
lib_inc_score(cur_lib_id)
inc_count(cur_lib_id)
inc_score(cur_lib_id)
choose_next()
libsaria.event.invite("POSTEOS", catch_eos)

View File

@ -96,7 +96,7 @@ class Playlist(collection.Collection):
def random(self):
if self.size == 0:
return
getattr = libsaria.collection.lib_get_attr
getattr = libsaria.collection.get_attr
last = self.last_tracks
for i in xrange(15):
id = self.get_rand_candidate()

View File

@ -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

View File

@ -54,7 +54,7 @@ def universal_open(file):
plugin.install(file)
return
try:
id = libsaria.collection.lib_find_id(file)
id = libsaria.collection.file_to_id(file)
if id:
libsaria.collection.lib_play_id(id)
else:

View File

@ -4,8 +4,9 @@ import libsaria
import web
import xm
cache = libsaria.cache
pref_attr = xm.find_preferred_attribute
file_to_id = libsaria.collection.file_to_id
cache = libsaria.cache
pref_attr = xm.find_preferred_attribute
pref_sizes = ["extralarge", "large", "medium", "small"]
@ -61,11 +62,11 @@ def lfm_cache_album(file, artist, album):
return False
def get_artwork(filepath):
id = libsaria.collection.lib_find_id(filepath)
id = file_to_id(filepath)
if id == None:
return
artist = libsaria.collection.lib_get_attr(id, "artist")
album = libsaria.collection.lib_get_attr(id, "album")
artist = libsaria.collection.get_attr(id, "artist")
album = libsaria.collection.get_attr(id, "album")
cached = cache[artist]
file = cached.get("%s.jpg" % album, lfm_cache_album, artist, album)
libsaria.event.start("POSTGETART", file)

View File

@ -6,6 +6,7 @@ import menu
libsaria = ocarina.libsaria
from libsaria import collection
library = collection.library
event = ocarina.libsaria.event
gtk = ocarina.gtk
@ -81,7 +82,7 @@ class Library(Collection):
def populate(self):
#import datetime
#before = datetime.datetime.now()
Collection.populate(self, collection.walk_library)
Collection.populate(self, library.walk_tags)
#after = datetime.datetime.now()
#print "Populating took: %s" % (after - before)
@ -90,17 +91,17 @@ class Library(Collection):
self.populate()
def reset(self):
collection.lib_reset()
collection.reset()
self.clear()
def mouse_motion(self, row):
return (collection.lib_get_attr(row[0], "playcount"),
collection.lib_get_attr(row[0], "score"))
return (collection.get_attr(row[0], "playcount"),
collection.get_attr(row[0], "score"))
def select_row(self, row):
collection.change_score()
collection.lib_play_id(row[0])
collection.lib_inc_score(row[0], 1)
collection.inc_score(row[0], 1)
def filter(self, text):
collection.lib_filter(text)
@ -138,13 +139,13 @@ class Playlist(Collection):
#print "Populating took: %s" % (after - before)
def mouse_motion(self, row):
return (collection.lib_get_attr(row[0], "playcount"),
collection.lib_get_attr(row[0], "score"))
return (collection.get_attr(row[0], "playcount"),
collection.get_attr(row[0], "score"))
def select_row(self, row):
collection.change_score()
collection.lib_play_id(row[0])
collection.lib_inc_score(row[0], 1)
collection.inc_score(row[0], 1)
def filter(self, text):
collection.plist_filter(text)

View File

@ -11,7 +11,8 @@ label = None
image = None
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
info = None
@ -72,9 +73,9 @@ class InfoBar(Bar):
libsaria.event.invite("POSTLOAD", self.change_title)
def change_title(self, filepath):
id = libsaria.collection.lib_find_id(filepath)
title = lib_get_attr(id, "title")
artist = libsaria.collection.lib_get_attr(id, "artist")
id = file_to_id(filepath)
title = get_attr(id, "title")
artist = get_attr(id, "artist")
self.title.set_text("%s by %s" % (title,artist))

View File

@ -7,19 +7,19 @@ invite = ocarina.libsaria.event.invite
libsaria = ocarina.libsaria
#update = None
get_time = None
lib_get_attr = None
lib_find_id = None
get_time = None
get_attr = None
file_to_id = None
def set_fns():
#global update
global get_time
global lib_get_attr
global lib_find_id
global get_attr
global file_to_id
#update = ocarina.libsaria.music.get_progress
get_time = ocarina.libsaria.music.get_time
lib_get_attr = libsaria.collection.lib_get_attr
lib_find_id = libsaria.collection.lib_find_id
get_time = ocarina.libsaria.music.get_time
get_attr = libsaria.collection.get_attr
file_to_id = libsaria.collection.file_to_id
invite("POSTSTART", set_fns)
@ -45,11 +45,10 @@ class AttrLabel(gtk.Alignment):
invite("POSTLOAD", self.update)
def update(self, filepath):
global lib_find_id
global lib_get_attr
id = lib_find_id(filepath)
global get_attr
id = file_to_id(filepath)
if id != None:
text = str(lib_get_attr(id, self.attr))
text = str(get_attr(id, self.attr))
if self.other:
text = "%s %s" % (self.other, text)
self.label.set_text(text)

View File

@ -1,11 +1,11 @@
# Bryan Schumaker (10/30/2010)
import ocarina
gdk = ocarina.gdk
libsaria = ocarina.libsaria
lib_find_id = libsaria.collection.lib_find_id
lib_get_attr = libsaria.collection.lib_get_attr
invite = libsaria.event.invite
gdk = ocarina.gdk
libsaria = ocarina.libsaria
file_to_id = libsaria.collection.file_to_id
get_attr = libsaria.collection.get_attr
invite = libsaria.event.invite
def tweak_icon(file):
@ -17,8 +17,8 @@ def tweak_icon(file):
def tweak_title(filepath):
if filepath != None:
id = lib_find_id(filepath)
title = lib_get_attr(id, "title")
id = file_to_id(filepath)
title = get_attr(id, "title")
else:
title = ocarina.__vers__
ocarina.set_window_title(title)