Collection Indexing

I can now add songs to a collection's index.  The index only works for
unicode string right now (all others must be converted)
This commit is contained in:
Bryan Schumaker 2010-10-10 13:45:52 -04:00
parent 2c27185679
commit 168025fb60
10 changed files with 81 additions and 66 deletions

View File

@ -27,7 +27,7 @@ def init():
# If a preference has not already been set, set pref[key] = value
def init_pref(key, value):
global prefs
if prefs.get(key) == None:
if prefs.get(key, None) == None:
prefs[key] = value

View File

@ -52,4 +52,8 @@ def lib_get_cur_id():
global cur_lib_id
return cur_lib_id
def lib_filter(text):
global library
library.filter(text)

View File

@ -26,22 +26,24 @@ class Collection:
def save(self):
libsaria.data.save(
[self.fs_tree, self.tag_tree, self.records,
self.next_record, self.size],
(self.fs_tree, self.tag_tree, self.index, self.records,
self.next_record, self.size),
self.file, "")
def load(self):
objects = libsaria.data.load(self.file, "")
if objects == None or len(objects) != 5:
if objects == None or len(objects) != 6:
self.reset()
return
(self.fs_tree, self.tag_tree, self.records,
(self.fs_tree, self.tag_tree, self.index, self.records,
self.next_record, self.size) = objects
def reset(self):
from tree import DLTree
from index import Index
self.fs_tree = DLTree()
self.tag_tree = DLTree()
self.index = Index()
self.records = dict()
self.next_record = 0
self.size = 0
@ -99,6 +101,7 @@ class Collection:
fs = self.fs_tree.insert(components + [id])
tags = self.tag_tree.insert([artist, album, title, id])
self.index.insert([album, artist, title], id)
record.fs = fs
record.tags = tags

View File

@ -1,64 +1,49 @@
# Bryan Schumaker (8/10/2010)
ttable = None
get = None
update = None
translate = None
split = None
translate = unicode.translate
split = unicode.split
space_ord = ord(" ")
stripc = u"\"#$%&'*+<=>@[]^`{|}~.?!"
splitc = u"-\/,:;()_~+"
ttable = None
def format_once(text):
import string
global ttable
upper = string.uppercase
lower = string.lowercase
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]
format = format_rest
return format_rest(text)
def format_rest(text):
return text.translate(ttable).split()
format = format_once
class Index(dict):
def __init__(self):
dict.__init__(self)
def setup(self):
import string
global ttable
global get
global update
global translate
global split
def insert(self, tags, id):
get = self.get
update = set.update
translate = unicode.translate
split = unicode.split
space = ord(" ")
stripc = u"\"#$%&'*+<=>@[]^`{|}~.?!"
splitc = u"-\/,:;()_~+"
upper = string.uppercase
lower = string.lowercase
ttable = dict((ord(c),None) for c in stripc)
splitt = dict((ord(c),space) 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 insert(self, id, tags):
global ttable
global get
global update
global translate
global split
idset = set([id])
if ttable == None:
self.setup()
for tag in tags:
words = split(translate(tag,ttable))
for word in words:
word_set = get(word,None)
if word_set == None:
for word in format(tag):
ids = get(word, None)
if ids == None:
self[word] = idset
else:
update(word_set, idset)
for l in word:
word_set = get(l, None)
if word_set == None:
self[word] = idset
else:
update(word_set, idset)
ids.update(idset)
def filter(self, text):
text = unicode(text)
search = format(text)
print search

View File

@ -5,26 +5,40 @@ import collection
from libsaria import data
save = data.save
load = data.load
sep = libsaria.path.sep
walk = libsaria.path.walk
join = libsaria.path.join
splitext = libsaria.path.splitext
badfiles = set()
def set_badfiles():
global badfiles
bf = load("badfiles")
if bf != None:
badfiles = bf
class Library(collection.Collection):
def __init__(self):
collection.Collection.__init__(self, "DLTree_test")
self.badfiles = set()
pass
def scan(self, path):
print "Library scanning %s" % path
set_badfiles()
self.reset()
self.update(path)
self.save()
save(badfiles, "badfiles", "")
self.disp()
def filter(self, text):
self.index.filter(text)
def update(self, path):
badfiles = self.badfiles
global badfiles
FileRef = libsaria.collection.FileRef
for root,dirs,files in walk(path):
@ -44,6 +58,3 @@ class Library(collection.Collection):
self.insert_allocate(split_root + [file], ref)
except UnicodeEncodeError:
pass
#except Exception, e:
# print e
# pass

View File

@ -22,7 +22,6 @@ def savefile(item, file):
def load(file, ext=".pickle"):
file = "%s%s" % (path.join(path.saria_dir(),file),ext)
print file
return loadfile(file)

View File

@ -70,6 +70,9 @@ class Library(Collection):
after = datetime.datetime.now()
print "Populating took: %s" % (after-before)
def filter(self, text):
collection.lib_filter(text)
def refresh(self, arg):
self.clear()
self.populate()

View File

@ -3,6 +3,8 @@
import ocarina
gtk = ocarina.gtk
tabs = ocarina.tabs
class FilterEntry(gtk.Entry):
def __init__(self):
gtk.Entry.__init__(self)
@ -10,5 +12,4 @@ class FilterEntry(gtk.Entry):
self.show()
def changed(self, entry):
# We want to start filtering here
print entry.get_text()
tabs.filter(entry.get_text())

View File

@ -20,6 +20,10 @@ class TabPage(gtk.VBox):
self.content = content
self.show()
def filter(self, text):
if hasattr(self.content, "filter"):
self.content.filter(text)
def visible(self):
global bottom
global top
@ -87,3 +91,8 @@ def switch_page(notebook, page, page_num):
old.invisible()
new.visible()
cur_page = page_num
def filter(text = None):
#global cur_page
p = tabs.get_nth_page(cur_page)
p.filter(text)

View File

@ -4,6 +4,6 @@
from libsaria import collection
#collection.new_source2("~/Music/")
collection.new_source("/media/Music")
collection.new_source("~/Music/")
#collection.new_source("/media/Music")
print collection.library.size