# Bryan Schumaker (2 / 12 / 2011)

import string
import library
ttable = dict()
for s in string.punctuation:
	ttable[ord(s)] = u""
for s in string.lowercase:
	ttable[ord(s)] = ord(s) - 32

class Tree(dict):
	def __init__(self):
		dict.__init__(self)

	def walk(self):
		keys = self.keys()
		keys.sort()

		for key in keys:
			child = self[key]
			if len(child.keys()) == 0:
				yield key
				continue
			for res in child.walk():
				yield res

	def __insert__(self, path):
		path_part = path[0]
		child     = self.get(path_part, None)
		if child == None:
			child = self.__class__()
			self[path_part] = child
		return child

	def insert(self, path):
		child = self.__insert__(path)
		if len(path) > 1:
			child.insert(path[1:])


lib_tree = None

def list_ids():
	for id in lib_tree.walk():
		yield id

def add_tracks(tracks):
	for id, track in tracks.iteritems():
		artist = track.artist.translate(ttable)
		album  = track.album.translate(ttable)
		lib_tree.insert([artist, album, track.track, id])

def make_tree():
	global lib_tree
	lib_tree = Tree()
	for src, tracks in library.lib_dict.iteritems():
		add_tracks(tracks)
