view vixm/playlist.py @ 39:9995931be5b1 pure

Select first song randomly if needed
author Josef "Jeff" Sipek <jeffpc@josefsipek.net>
date Sat, 04 Nov 2006 17:57:38 -0500
parents 8028f2addb00
children 06b5a7db3d19
line wrap: on
line source

# playlist.py - playlist interface
#
# Copyright (C) 2006  Josef "Jeff" Sipek <jeffpc@josefsipek.net>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.

import random, re

LIST_PRIO	= 0
LIST_DEFAULT	= 1

class playlist:
	def __init__(self, allowrandom=False):
		self.__list = []
		self.__allowrand = allowrandom # allow randomization of this list
		self.__cursor = allowrandom and self.__rnd() or 0
	
	def enqueue(self, song):
		""" Append a song to the list """
		self.__list.append(song)
	
	def dequeue(self, song):
		""" Remove a song from the list """
		self.__list.remove(song)
	
	def pop(self):
		""" Pop the next song """
		return self.__list.pop(0)

	def __rnd(self):
		return random.randint(0, len(self.__list)-1)

	def next(self):
		""" Get the next song """
		idx = self.__cursor

		if not self.__allowrand:
			self.__cursor += 1
			if self.__cursor >= len(self.__list):
				self.__cursor = 0
		else:
			self.__cursor = self.__rnd()

		return self.__list[idx]

	def __getitem__(self, i):
		""" Get item at position i """
		return self.__list[i]

	def __len__(self):
		""" Return the length of the playlist """
		return len(self.__list)

	def search(self, regexp):
		""" Yield all the songs matching regexp """
		for song in self.__list:
			if re.search(regexp, str(song), re.IGNORECASE):
				yield song

	def index(self, song):
		""" Returns the index of the first occurence of song """
		return self.__list.index(song)