ocarina/libsaria/collection/library.py

136 lines
2.4 KiB
Python

# Bryan Schumaker (11/05/2010)
import libsaria
fs_tree = None
tag_tree = None
index = None
tracks = None
next_id = None
sources = None
size = None
visible = None
filtered = False
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
global songs
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):
print "Please don't use library.get_attr..."
return get_attrs(id, attr)[0]
def get_attrs(id, *attrs):
res = []
rec = tracks.get(id, None)
if rec == None:
return None
get = rec.__dict__.get
tags = rec.tags.walk_backwards()
for attr in attrs:
if attr == "id":
res += [id]
elif attr == "filepath":
cmp = rec.fs.walk_backwards()
res += [libsaria.path.sep.join([""] + cmp)]
elif attr == "art":
from libsaria import lastfm
res += [lastfm.get_artwork_id(id)]
elif attr == "artist":
res += [tags[0]]
elif attr == "album":
res += [tags[1]]
elif attr == "title":
res += [tags[2]]
else:
res += [get(attr, None)]
return res
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 play_id(id):
libsaria.collection.cur_lib_id = id
libsaria.audio.load(get_attrs(id, "filepath")[0])
libsaria.audio.play()
def filter(text):
global visible
global index
global filtered
if len(text) > 0:
visible = index.filter(text)
filtered = True
else:
visible = None
filtered = False
def test_filter(text):
return index.filter(text)
def is_visible(id):
global filtered
global visible
if filtered == False:
return True
else:
return id in visible