ocarina/libsaria/collection/tree.py

62 lines
1.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)