view vixm/control.py @ 16:447b47ab396f

Implemented dequeue the proper way; Cleaned up stupidity in enqueue code
author Josef "Jeff" Sipek <jeffpc@josefsipek.net>
date Sun, 20 Aug 2006 00:05:14 -0400
parents 2ecc611d198f
children 1c769ae67af4
line wrap: on
line source

import playlist, util

def cmd_quit(ui, start, stop, args):
	ui.shutdown = True

def cmd_number(ui, start, stop, args):
	do_list(ui, start, stop, True, args)

def cmd_list(ui, start, stop, args):
	do_list(ui, start, stop, False, args)

def cmd_enqueue(ui, start, stop, args):
	try:
		id = int(args[0])-1

		if (id < 0) or \
		   (id >= len(ui.lists[playlist.LIST_DEFAULT])):
			raise ValueError

	except ValueError:
		print "Invalid song id"
		return

	s = ui.lists[playlist.LIST_DEFAULT][id]
	print "Enqueuing song: %s (%s)" % (s["title"], util.strtime(s["time"]))
	ui.lists[playlist.LIST_PRIO].enqueue(s)

def cmd_dequeue(ui, start, stop, args):
	l = ui.lists[playlist.LIST_PRIO]

	max = len(l)
	if not max:
		return

	start = util.fixupint(start, max)
	stop = util.fixupint(stop, max)

	for i in range(start-1, stop):
		s = l[start-1]
		print "Dequeuing song: %s (%s)" % (s["title"],
				util.strtime(s["time"]))
		l.dequeue(s)

def cmd_help(ui, start, stop, args):
	for c in args[0]:
		print "  %s\t%s" % (args[0][c][2],args[0][c][3])

def do_list(ui, start, stop, number, args):
	# get the list id from the argument or default to
	# LIST_PRIO
	try:
		if args[1]:
			listid = int(args[1])
		else:
			listid = playlist.LIST_PRIO

		if listid < 0 or listid >= len(ui.lists):
			raise ValueError
	except ValueError, e:
		print "Invalid list number"
		raise e

	max = len(ui.lists[listid])
	if not max:
		return

	start = util.fixupint(start, max)
	stop = util.fixupint(stop, max)

	# starting number should be less than the ending
	# number, as well as positive; ending number should
	# be less than or equal to the size of the playlist
	if start > stop or \
	   start < 1 or \
	   stop > max:
		raise ValueError

	i = 1
	pfx = ""
	for s in ui.lists[listid]:
		if i < start or i > stop:
			i += 1
			continue

		if number:
			pfx = "%d. " % (i,)

		print "%s%s (%s)" % (pfx, s["title"],
				util.strtime(s["time"]))
		i += 1