Collection Tree

I have implemented a tree to store store information about each track.
I now support tracks that have the same artist, album, and title.
This commit is contained in:
Bryan Schumaker 2010-08-12 22:11:13 -04:00
parent 71b3289f62
commit d8cadb83e9
5 changed files with 132 additions and 25 deletions

View File

@ -2,6 +2,12 @@
import libsaria
tag = None
audio = None
ins_table = None
ins_index = None
ins_tree = None
class Collection:
def __init__(self):
pass
@ -9,37 +15,73 @@ class Collection:
def scan(self, path):
print "Scanning path:", path
global ins_table
global ins_index
global ins_tree
self.reset()
ins_table = self.table.insert
ins_index = self.index.insert
ins_tree = self.tree. insert
self.update(path)
self.tree.sort()
self.save()
def reset(self):
print "Erasing collection ... "
print "Resetting collection ... "
from table import Table
from index import Index
from tree import Tree
self.table = Table()
self.index = Index()
self.tree = Tree()
def insert(self, file):
tags = file.tag()
tuple = (tags.artist, tags.album, tags.title)
id = self.table.insert(tuple)
self.index.insert(id, tuple)
def save(self):
from libsaria import data
data.save((self.table, self.index, self.tree), "library", "")
def insert(self, file, filepath):
global tag
global audio
global ins_table
global ins_index
global ins_tree
tags = tag(file)
audio_prop = audio(file)
if tags.artist == u"":
tags.artist = "Unknown Artist"
if tags.album == u"":
tags.album = "Unknown Album"
if tags.title == u"":
tags.title = "Unknown Title"
list = [tags.artist, tags.album, tags.title]
id = ins_table(list)
ins_index(id, list)
list.append(id)
ins_tree(list, tags, audio_prop, filepath)
def update(self, path):
global tag
global audio
FileRef = libsaria.collection.FileRef
join = libsaria.path.join
insert = self.insert
tag = FileRef.tag
audio = FileRef.audioProperties
for root,dirs,files in libsaria.path.walk(path):
for file in files:
file = join(root,file)
try:
insert(FileRef(file))
insert(FileRef(file), file)
except Exception,e:
pass
#print Exception, e
#print e

View File

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

View File

@ -0,0 +1,48 @@
# Bryan Schumaker (8 / 12 / 2010)
get = dict.get
class TagNode:
def __init__(self):
pass
def insert(self, tag_list, tags, audio, filepath):
self.artist = tags.artist
self.album = tags.album
self.title = tags.title
self.year = tags.year
self.filepath = filepath
self.playcount = 0
self.length = audio.length
def sort(self, level):
pass
class Tree(dict):
def __init__(self):
dict.__init__(self)
self.sorted_keys = None
def sort(self, level=0):
self.sorted_keys = self.keys()
self.sorted_keys.sort()
space = ' '*level
for key in self.sorted_keys:
self[key].sort(level+1)
def insert(self, tag_list, tags, audio, filepath):
if len(tag_list) == 0:
return
global get
global insert
tag = tag_list[0]
node = get(self, tag, None)
if node == None:
if len(tag_list) == 1:
node = TagNode()
else:
node = Tree()
self[tag] = node
node.insert(tag_list[1:], tags, audio, filepath)

1
saria-test.py Normal file → Executable file
View File

@ -1,3 +1,4 @@
#!/usr/bin/python
# Bryan Schumaker (8/7/2010)
import os

View File

@ -7,3 +7,4 @@ src = "~/Music"
#src = "~/Music/Foo Fighters/Foo Fighters"
collection.new_source(src, bg=False)
#print collection.source.index