From 168025fb6075ec2444d7aa6605c6c9898de3f300 Mon Sep 17 00:00:00 2001 From: Bryan Schumaker Date: Sun, 10 Oct 2010 13:45:52 -0400 Subject: [PATCH] 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) --- libsaria/__init__.py | 2 +- libsaria/collection/__init__.py | 4 ++ libsaria/collection/collection.py | 11 ++-- libsaria/collection/index.py | 85 +++++++++++++------------------ libsaria/collection/lens.py | 23 ++++++--- libsaria/data.py | 1 - ocarina/collection.py | 3 ++ ocarina/entry.py | 5 +- ocarina/tabs.py | 9 ++++ tests/dl_tree.py | 4 +- 10 files changed, 81 insertions(+), 66 deletions(-) diff --git a/libsaria/__init__.py b/libsaria/__init__.py index 77a5b421..93c79e14 100644 --- a/libsaria/__init__.py +++ b/libsaria/__init__.py @@ -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 diff --git a/libsaria/collection/__init__.py b/libsaria/collection/__init__.py index b8a83dcf..ffd2b42b 100644 --- a/libsaria/collection/__init__.py +++ b/libsaria/collection/__init__.py @@ -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) + diff --git a/libsaria/collection/collection.py b/libsaria/collection/collection.py index fc0800ab..b79ea1d6 100644 --- a/libsaria/collection/collection.py +++ b/libsaria/collection/collection.py @@ -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 diff --git a/libsaria/collection/index.py b/libsaria/collection/index.py index 7f244066..1fa939dc 100644 --- a/libsaria/collection/index.py +++ b/libsaria/collection/index.py @@ -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 diff --git a/libsaria/collection/lens.py b/libsaria/collection/lens.py index a4c352ba..0693ac2b 100644 --- a/libsaria/collection/lens.py +++ b/libsaria/collection/lens.py @@ -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 diff --git a/libsaria/data.py b/libsaria/data.py index aefa49e3..c9757b79 100644 --- a/libsaria/data.py +++ b/libsaria/data.py @@ -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) diff --git a/ocarina/collection.py b/ocarina/collection.py index 4dbf26d4..a53c1381 100644 --- a/ocarina/collection.py +++ b/ocarina/collection.py @@ -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() diff --git a/ocarina/entry.py b/ocarina/entry.py index e2b70595..504def21 100644 --- a/ocarina/entry.py +++ b/ocarina/entry.py @@ -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()) diff --git a/ocarina/tabs.py b/ocarina/tabs.py index 079ea7f5..0442c311 100644 --- a/ocarina/tabs.py +++ b/ocarina/tabs.py @@ -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) diff --git a/tests/dl_tree.py b/tests/dl_tree.py index f4558d05..a094649c 100644 --- a/tests/dl_tree.py +++ b/tests/dl_tree.py @@ -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