libsaria: Added library index

This makes it easier to search for songs.
This commit is contained in:
Bryan Schumaker 2011-05-21 21:40:32 -04:00
parent 297775896e
commit f676b5b44d
2 changed files with 44 additions and 0 deletions

View File

@ -3,6 +3,7 @@
import library
import update
import tree
import index
# Function pointers for conveniance
add_path = library.add_path
@ -11,6 +12,7 @@ list_ids = tree.list_ids
library.load()
tree.make_tree()
index.reindex()
def update_lib():
update.update()

View File

@ -0,0 +1,42 @@
# Bryan Schumaker (5 / 21 / 2011)
import string
import library
lib_index = dict()
all_ids = set()
space_ord = ord(" ")
stripc = u"\"#$%&'*+<=>@[]^`{|}~.?!"
splitc = u"-\/,:;()_~+"
upper = string.uppercase
lower = string.lowercase
# Create a translation table for formatting text
ttable = dict((ord(c), None) for c in stripc)
splitt = dict((ord(c), space_ord) for c in splitc)
lowert = dict((ord(c), ord(lower[i])) for i, c in enumerate(upper))
for t in (splitt, lowert):
for c in t:
ttable[c] = t[c]
def format(text):
return text.translate(ttable).split()
def add_to_index(id, text):
for word in format(text):
if lib_index.get(word) == None:
lib_index[word] = set([id])
else:
lib_index[word].add(id)
def add_tracks(tracks):
for id, track in tracks.iteritems():
all_ids.add(id)
add_to_index(id, track.artist)
add_to_index(id, track.album)
add_to_index(id, track.title)
def reindex():
for src, tracks in library.lib_dict.iteritems():
add_tracks(tracks)