115 lines
2.2 KiB
Python
115 lines
2.2 KiB
Python
# Bryan Schumaker (8 / 12 / 2010)
|
|
|
|
get = dict.get
|
|
|
|
class TagNode(dict):
|
|
def __init__(self, id):
|
|
dict.__init__(self)
|
|
self["id"] = id
|
|
|
|
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
|
|
|
|
length = audio.length
|
|
sf = 0
|
|
sec = length % 60
|
|
if sec >= 10:
|
|
sf = ""
|
|
min = (length - sec) / 60
|
|
mf = 0
|
|
if min >= 10:
|
|
mf = ""
|
|
|
|
self["seconds"] = audio.length
|
|
self["length"] = "%s%s:%s%s" % (mf, min, sf, sec)
|
|
|
|
def walk(self):
|
|
yield self
|
|
|
|
|
|
class Tree(dict):
|
|
def __init__(self):
|
|
dict.__init__(self)
|
|
|
|
def walk(self):
|
|
keys = self.keys()
|
|
keys.sort()
|
|
get_item = self.__getitem__
|
|
for key in keys:
|
|
for track in get_item(key).walk():
|
|
yield track
|
|
|
|
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(tag)
|
|
else:
|
|
node = Tree()
|
|
self[tag] = node
|
|
node.insert(tag_list[1:], tags, audio, filepath)
|
|
|
|
|
|
class DLTree:
|
|
def __init__(self, parent=None):
|
|
self.parent = parent
|
|
self.children = None
|
|
|
|
def disp(self, level=0):
|
|
space = " " * level
|
|
children = self.children
|
|
|
|
if children == None:
|
|
return
|
|
|
|
keys = children.keys()
|
|
keys.sort()
|
|
|
|
for key in keys:
|
|
if key.__class__ == str or key.__class__ == int:
|
|
value = children[key]
|
|
print space, key
|
|
value.disp(level+1)
|
|
|
|
#if len(self.children) == 0:
|
|
# print self.get_path()
|
|
|
|
def get_path(self, child=None):
|
|
if self.parent == None:
|
|
return [self.children[child]]
|
|
cmp = self.children.get(child, None)
|
|
child = self
|
|
path = self.parent.get_path(child)
|
|
if cmp != None:
|
|
path.append(cmp)
|
|
return path
|
|
|
|
def insert(self, path):
|
|
cmp = path[0]
|
|
child = None
|
|
try:
|
|
child = self.children[cmp]
|
|
except TypeError:
|
|
self.children = dict()
|
|
child = DLTree(self)
|
|
self.children[cmp] = child
|
|
self.children[child] = cmp
|
|
except KeyError:
|
|
child = DLTree(self)
|
|
self.children[cmp] = child
|
|
self.children[child] = cmp
|
|
if len(path) > 1:
|
|
return child.insert(path[1:])
|
|
return self
|