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:
parent
71b3289f62
commit
d8cadb83e9
|
@ -2,6 +2,12 @@
|
||||||
|
|
||||||
import libsaria
|
import libsaria
|
||||||
|
|
||||||
|
tag = None
|
||||||
|
audio = None
|
||||||
|
ins_table = None
|
||||||
|
ins_index = None
|
||||||
|
ins_tree = None
|
||||||
|
|
||||||
class Collection:
|
class Collection:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
pass
|
pass
|
||||||
|
@ -9,37 +15,73 @@ class Collection:
|
||||||
|
|
||||||
def scan(self, path):
|
def scan(self, path):
|
||||||
print "Scanning path:", path
|
print "Scanning path:", path
|
||||||
|
global ins_table
|
||||||
|
global ins_index
|
||||||
|
global ins_tree
|
||||||
self.reset()
|
self.reset()
|
||||||
|
ins_table = self.table.insert
|
||||||
|
ins_index = self.index.insert
|
||||||
|
ins_tree = self.tree. insert
|
||||||
self.update(path)
|
self.update(path)
|
||||||
|
self.tree.sort()
|
||||||
|
self.save()
|
||||||
|
|
||||||
|
|
||||||
def reset(self):
|
def reset(self):
|
||||||
print "Erasing collection ... "
|
print "Resetting collection ... "
|
||||||
from table import Table
|
from table import Table
|
||||||
from index import Index
|
from index import Index
|
||||||
|
from tree import Tree
|
||||||
self.table = Table()
|
self.table = Table()
|
||||||
self.index = Index()
|
self.index = Index()
|
||||||
|
self.tree = Tree()
|
||||||
|
|
||||||
|
|
||||||
def insert(self, file):
|
def save(self):
|
||||||
tags = file.tag()
|
from libsaria import data
|
||||||
tuple = (tags.artist, tags.album, tags.title)
|
data.save((self.table, self.index, self.tree), "library", "")
|
||||||
id = self.table.insert(tuple)
|
|
||||||
self.index.insert(id, tuple)
|
|
||||||
|
|
||||||
|
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):
|
def update(self, path):
|
||||||
|
global tag
|
||||||
|
global audio
|
||||||
FileRef = libsaria.collection.FileRef
|
FileRef = libsaria.collection.FileRef
|
||||||
join = libsaria.path.join
|
join = libsaria.path.join
|
||||||
insert = self.insert
|
insert = self.insert
|
||||||
|
tag = FileRef.tag
|
||||||
|
audio = FileRef.audioProperties
|
||||||
|
|
||||||
for root,dirs,files in libsaria.path.walk(path):
|
for root,dirs,files in libsaria.path.walk(path):
|
||||||
for file in files:
|
for file in files:
|
||||||
file = join(root,file)
|
file = join(root,file)
|
||||||
try:
|
try:
|
||||||
insert(FileRef(file))
|
insert(FileRef(file), file)
|
||||||
except Exception,e:
|
except Exception,e:
|
||||||
pass
|
pass
|
||||||
|
#print Exception, e
|
||||||
|
#print e
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,23 +1,35 @@
|
||||||
# Bryan Schumaker (8/10/2010)
|
# Bryan Schumaker (8/10/2010)
|
||||||
|
|
||||||
ttable = None
|
ttable = None
|
||||||
|
get = None
|
||||||
|
update = None
|
||||||
translate = None
|
translate = None
|
||||||
|
split = None
|
||||||
|
|
||||||
class Index:
|
class Index(dict):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.tokens = dict()
|
dict.__init__(self)
|
||||||
|
|
||||||
def setup(self):
|
def setup(self):
|
||||||
import string
|
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(" ")
|
space = ord(" ")
|
||||||
strip = u"\"#$%&'*+<=>@[]^`{|}~.?!"
|
stripc = u"\"#$%&'*+<=>@[]^`{|}~.?!"
|
||||||
split = u"-\/,:;()_~+"
|
splitc = u"-\/,:;()_~+"
|
||||||
upper = string.uppercase
|
upper = string.uppercase
|
||||||
lower = string.lowercase
|
lower = string.lowercase
|
||||||
translate = string.translate
|
|
||||||
|
|
||||||
ttable = dict((ord(c),None) for c in strip)
|
ttable = dict((ord(c),None) for c in stripc)
|
||||||
splitt = dict((ord(c),space) for c in split)
|
splitt = dict((ord(c),space) for c in splitc)
|
||||||
lowert = dict((ord(c),ord(lower[i])) for i,c in enumerate(upper))
|
lowert = dict((ord(c),ord(lower[i])) for i,c in enumerate(upper))
|
||||||
for t in (splitt, lowert):
|
for t in (splitt, lowert):
|
||||||
for c in t:
|
for c in t:
|
||||||
|
@ -25,25 +37,28 @@ class Index:
|
||||||
|
|
||||||
def insert(self, id, tags):
|
def insert(self, id, tags):
|
||||||
global ttable
|
global ttable
|
||||||
|
global get
|
||||||
|
global update
|
||||||
|
global translate
|
||||||
|
global split
|
||||||
idset = set([id])
|
idset = set([id])
|
||||||
tokens = self.tokens
|
|
||||||
|
|
||||||
if ttable == None:
|
if ttable == None:
|
||||||
self.setup()
|
self.setup()
|
||||||
|
|
||||||
for tag in tags:
|
for tag in tags:
|
||||||
words = translate(tag,ttable).split()
|
words = split(translate(tag,ttable))
|
||||||
for word in words:
|
for word in words:
|
||||||
set = tokens.get(word,None)
|
word_set = get(word,None)
|
||||||
if set == None:
|
if word_set == None:
|
||||||
tokens[word] = idset
|
self[word] = idset
|
||||||
else:
|
else:
|
||||||
set.update(idset)
|
update(word_set, idset)
|
||||||
for l in word:
|
for l in word:
|
||||||
set = tokens.get(l, None)
|
word_set = get(l, None)
|
||||||
if set == None:
|
if word_set == None:
|
||||||
tokens[word] = idset
|
self[word] = idset
|
||||||
else:
|
else:
|
||||||
set.update(idset)
|
update(word_set, idset)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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,3 +1,4 @@
|
||||||
|
#!/usr/bin/python
|
||||||
# Bryan Schumaker (8/7/2010)
|
# Bryan Schumaker (8/7/2010)
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
|
|
@ -7,3 +7,4 @@ src = "~/Music"
|
||||||
#src = "~/Music/Foo Fighters/Foo Fighters"
|
#src = "~/Music/Foo Fighters/Foo Fighters"
|
||||||
|
|
||||||
collection.new_source(src, bg=False)
|
collection.new_source(src, bg=False)
|
||||||
|
#print collection.source.index
|
||||||
|
|
Loading…
Reference in New Issue