53027b6006
calling library.get_sources() will iterate over the list of sources contained in the library. This is mostly used for storing in the backup xml.
268 lines
4.9 KiB
Python
268 lines
4.9 KiB
Python
# Bryan Schumaker (11/05/2010)
|
|
|
|
import os
|
|
import tagpy
|
|
import libsaria
|
|
import libsaria.path.files
|
|
import string
|
|
from libsaria import threads
|
|
from track import Track
|
|
splitext = libsaria.path.splitext
|
|
|
|
fs_tree = None
|
|
tag_tree = None
|
|
index = None
|
|
tracks = None
|
|
sources = None
|
|
size = None
|
|
visible = None
|
|
|
|
FileRef = tagpy.FileRef
|
|
|
|
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 reset():
|
|
from libsaria.trees import FSTree, DLFSTree, DLValTree
|
|
from index import Index
|
|
|
|
global index
|
|
global tracks
|
|
global fs_tree
|
|
global tag_tree
|
|
global sources
|
|
global visible
|
|
|
|
sources = []
|
|
|
|
fs_tree = DLFSTree()
|
|
tag_tree = DLValTree()
|
|
index = Index()
|
|
tracks = dict()
|
|
size = 0
|
|
visible = set()
|
|
|
|
def load():
|
|
global fs_tree
|
|
global tag_tree
|
|
global index
|
|
global tracks
|
|
global sources
|
|
global lib_lock
|
|
global loaded
|
|
|
|
objects = libsaria.path.files.load("library", ".lib")
|
|
if objects == None or len(objects) != 5:
|
|
reset()
|
|
else:
|
|
(sources, tracks, fs_tree, tag_tree, index) = objects
|
|
|
|
lib_lock.acquire()
|
|
loaded = True
|
|
lib_lock.release()
|
|
libsaria.event.start("POSTLIBLOAD")
|
|
|
|
def is_loaded():
|
|
lib_lock.acquire()
|
|
val = loaded
|
|
lib_lock.release()
|
|
return val
|
|
|
|
def load_bg2(callback):
|
|
load()
|
|
callback()
|
|
|
|
def load_bg(callback):
|
|
thr = threads.BG_Thread(load_bg2, callback)
|
|
thr.start()
|
|
|
|
def save():
|
|
global sources
|
|
libsaria.path.files.save( (sources, tracks, fs_tree, tag_tree, index),
|
|
"library", ".lib")
|
|
|
|
def walk():
|
|
for tag in tag_tree.walk():
|
|
yield tag[3]
|
|
|
|
def get_sources():
|
|
for source in sources:
|
|
yield source
|
|
|
|
def file_to_id(file):
|
|
return os.stat(file).st_ino
|
|
|
|
def artists():
|
|
keys = tag_tree.keys()
|
|
keys.sort()
|
|
res = []
|
|
for key in keys:
|
|
res.append( (tag_tree[key].value, key) )
|
|
return res
|
|
|
|
def albums(artist):
|
|
art = tag_tree[artist]
|
|
keys = art.keys()
|
|
keys.sort()
|
|
res = []
|
|
for key in keys:
|
|
res.append( (art[key].value, key) )
|
|
return res
|
|
|
|
def titles(artist, album):
|
|
art = tag_tree[artist]
|
|
alb = art[album]
|
|
keys = alb.keys()
|
|
keys.sort()
|
|
res = []
|
|
for key in keys:
|
|
res.append( (alb[key].value, key) )
|
|
return res
|
|
|
|
def song_id(artist, album, title):
|
|
art = tag_tree[artist]
|
|
alb = art[album]
|
|
title = alb[title]
|
|
return title.keys()[0]
|
|
|
|
def get_attrs(id, *attrs):
|
|
res = []
|
|
rec = tracks.get(id, None)
|
|
if rec == None:
|
|
return [0] * len(attrs)
|
|
|
|
get = rec.__dict__.get
|
|
tags = rec.tags.walk_vals_backwards()
|
|
for attr in attrs:
|
|
if attr == "id":
|
|
res += [id]
|
|
elif attr == "filepath":
|
|
res += [rec.fs.walk_path_backwards()]
|
|
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 set_attr(id, attr, new_value):
|
|
rec = tracks.get(id, None)
|
|
if rec:
|
|
if attr == "art":
|
|
from libsaria import lastfm
|
|
artist, album = get_attrs(id, "artist", "album")
|
|
lastfm.set_artwork_tags(artist, album, new_value)
|
|
else:
|
|
rec.__dict__[attr] = new_value
|
|
|
|
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.sources.cur_lib_id = id
|
|
libsaria.audio.load(get_attrs(id, "filepath")[0])
|
|
libsaria.controls.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
|
|
|
|
def add_source(path):
|
|
global sources
|
|
sources.append(path)
|
|
|
|
def scan(path):
|
|
add_source(path)
|
|
update()
|
|
save()
|
|
|
|
def insert_track(path, ref):
|
|
global tracks
|
|
global ttable
|
|
global fs_tree
|
|
global tag_tree
|
|
global index
|
|
tags = ref.tag()
|
|
audio = ref.audioProperties()
|
|
ino = os.stat(path).st_ino
|
|
track = Track(tags, audio)
|
|
artist = tags.artist or u"Unknown Artist"
|
|
album = tags.album or u"Unknown Album"
|
|
title = tags.title or u"Unknown Title"
|
|
fs = fs_tree.insert_path(path)
|
|
tag = tag_tree.insert( [artist.translate(ttable),
|
|
album.translate(ttable),
|
|
title.translate(ttable), ino],
|
|
[artist, album, title, ino]
|
|
)
|
|
|
|
index.insert(ino, (artist, album, title))
|
|
track.fs = fs
|
|
track.tags = tag
|
|
tracks[ino] = track
|
|
|
|
def update_path(path):
|
|
global badfiles
|
|
tree = libsaria.path.make_tree(path)
|
|
for path in tree.walk_paths():
|
|
ext = splitext(path)[1]
|
|
if ext in badfiles:
|
|
continue
|
|
try:
|
|
ref = FileRef(path)
|
|
except:
|
|
badfiles.add(ext)
|
|
continue
|
|
try:
|
|
insert_track(path, ref)
|
|
except Exception, e:
|
|
print e
|
|
|
|
def update():
|
|
global sources
|
|
sep = libsaria.path.sep
|
|
for path in sources:
|
|
update_path(path)
|