changeset 12:b5370c42288b

Code refactoring & implemented help command
author Josef "Jeff" Sipek <jeffpc@josefsipek.net>
date Sat, 19 Aug 2006 20:18:57 -0400
parents 50584a5c300e
children 2ecc611d198f
files vixm/control.py vixm/ui.py vixm/util.py
diffstat 3 files changed, 112 insertions(+), 111 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/vixm/control.py	Sat Aug 19 20:18:57 2006 -0400
@@ -0,0 +1,76 @@
+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
+
+	ui.enqueue(id)
+
+def cmd_dequeue(ui, start, stop, args):
+	print "not implemented yet"
+
+def cmd_help(ui, start, stop, args):
+	for c in args[0]:
+		print "\t%s" % (args[0][c][2],)
+
+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
+
--- a/vixm/ui.py	Sat Aug 19 20:06:56 2006 -0400
+++ b/vixm/ui.py	Sat Aug 19 20:18:57 2006 -0400
@@ -4,7 +4,7 @@
 from threading import Thread
 import xmms
 
-import playlist, song, util
+import playlist, song, util, control
 
 def run():
 	""" this is where we start execution """
@@ -65,14 +65,6 @@
 		self.lists = lists
 		self.shutdown = False
 
-	def __enqueue(self, id):
-		s = self.lists[playlist.LIST_DEFAULT][id]
-		print "Enqueuing song: %s (%s)" % (s["title"],
-				util.strtime(s["time"]))
-		self.lists[playlist.LIST_PRIO].enqueue(s)
-
-
-	def __cmd(self, txt):
 		# commad list:
 		#	quit	'q[!]'
 		#		quit fails if there are list changes in
@@ -111,111 +103,35 @@
 		#		ending on line n. Both m and n are included
 		#		in the list. m or n can both be an integer,
 		#		'' or '$'.
-
+		#
 		# command table format:
 		#	"command regexp":
-		#		(function to call, range allowed)
-		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
-
-			ui.__enqueue(id)
-
-		def __cmd_dequeue(ui, start, stop, args):
-			print "not implemented yet"
-
-		def __cmd_help(ui, start, stop, args):
-			print "not implemented yet"
-
-		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 = fixupint(start, max)
-			stop = 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
-
-		cmdtable = {
+		#		(function to call, range allowed, help str)
+		self.cmdtable = {
 			"q([!]){,1}":
-				(__cmd_quit, False, "q[!]"),
+				(control.cmd_quit, False, "q[!]"),
 			"n( ([0-9]+)){,1}":
-				(__cmd_number, True, "[range]n [playlistid]"),
+				(control.cmd_number, True, "[range]n [playlistid]"),
 			"l( ([0-9]+)){,1}":
-				(__cmd_list, True, "[range]l [playlistid]"),
+				(control.cmd_list, True, "[range]l [playlistid]"),
 			"a ([0-9]+)":
-				(__cmd_enqueue, False, "a [songid]"),
+				(control.cmd_enqueue, False, "a [songid]"),
 			"d ([0-9]+)":
-				(__cmd_dequeue, False, "d [songid]"),
+				(control.cmd_dequeue, False, "d [songid]"),
 			"h":
-				(__cmd_help, False, "h"),
+				(control.cmd_help, False, "h"),
 		}
 
-		def special2int(s):
-			if s == '$':
-				return -1
-			if s == '':
-				return 1
-			return int(s)
+	def enqueue(self, id):
+		s = self.lists[playlist.LIST_DEFAULT][id]
+		print "Enqueuing song: %s (%s)" % (s["title"],
+				util.strtime(s["time"]))
+		self.lists[playlist.LIST_PRIO].enqueue(s)
 
-		def fixupint(i, m):
-			if i == -1:
-				return m
-			return i
+	def __cmd(self, txt):
+		range_str = "(%|\\$|(\\$|[0-9]+){,1}(,(\\$|[0-9]+)){,1}){,1}"
 
-		range_str = "(%|\\$|(\\$|[0-9]+){,1}(,(\\$|[0-9]+)){,1}){,1}"
+		cmdtable = self.cmdtable
 		
 		for c in cmdtable:
 			rstr  = "^"
@@ -250,15 +166,17 @@
 					start = '1'
 					end = '1'
 				
-				start = special2int(start)
-				end = special2int(end)
+				start = util.special2int(start)
+				end = util.special2int(end)
 
 				gr = list(gr)
 				gr.pop(0)
 				gr.pop(0)
 				gr.pop(0)
 				gr.pop(0)
-				gr = tuple(gr)
+
+			gr = list(gr)
+			gr.append(self.cmdtable)
 
 			try:
 				cmdtable[c][0](self, start, end, gr)
@@ -272,12 +190,6 @@
 		while not self.shutdown:
 			tmp = sys.stdin.readline().strip()
 			
-			# FIXME: we should use regexps for this
-			# 
-			# search:	'^\/(.+)'
-			# enqueue:	'^([0-9]+)'
-			# commands:	'^([A-Za-z]+)(([^ ]*) *)*'
-
 			if tmp.startswith("/"):
 				# '/ABC' - searching
 				print "Searching not yet implemented"
--- a/vixm/util.py	Sat Aug 19 20:06:56 2006 -0400
+++ b/vixm/util.py	Sat Aug 19 20:18:57 2006 -0400
@@ -12,3 +12,16 @@
 		ret = "%d:%s" % (m, ret)
 	
 	return ret
+
+def special2int(s):
+	if s == '$':
+		return -1
+	if s == '':
+		return 1
+	return int(s)
+
+def fixupint(i, m):
+	if i == -1:
+		return m
+	return i
+