196 lines
3.6 KiB
Python
196 lines
3.6 KiB
Python
# Bryan Schumaker (11/05/2010)
|
|
|
|
import os
|
|
import tagpy
|
|
import libsaria
|
|
import string
|
|
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
|
|
|
|
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 = FSTree()
|
|
|
|
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
|
|
objects = libsaria.data.load("library", ".lib")
|
|
if objects == None or len(objects) != 5:
|
|
reset()
|
|
return
|
|
(sources, tracks, fs_tree, tag_tree, index) = objects
|
|
libsaria.event.start("POSTLIBLOAD")
|
|
|
|
def save():
|
|
global sources
|
|
libsaria.data.save( (sources, tracks, fs_tree, tag_tree, index),
|
|
"library", ".lib")
|
|
|
|
def walk():
|
|
for tag in tag_tree.walk():
|
|
yield tag[3]
|
|
|
|
def file_to_id(file):
|
|
return os.stat(file).st_ino
|
|
|
|
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 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
|
|
|
|
def add_source(path):
|
|
global sources
|
|
sources.insert_path(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.walk_paths():
|
|
update_path(path)
|