annotate vixm/playlist.py @ 33:99983189d4b2 master

Redo next song selection logic
author Josef "Jeff" Sipek <jeffpc@josefsipek.net>
date Fri, 03 Nov 2006 23:27:49 -0500
parents f97eb9f0c207
children 8028f2addb00
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
21
dfcf1a46fc56 Added GPLv2 file & copyright headers
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 15
diff changeset
1 # playlist.py - playlist interface
dfcf1a46fc56 Added GPLv2 file & copyright headers
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 15
diff changeset
2 #
dfcf1a46fc56 Added GPLv2 file & copyright headers
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 15
diff changeset
3 # Copyright (C) 2006 Josef "Jeff" Sipek <jeffpc@josefsipek.net>
dfcf1a46fc56 Added GPLv2 file & copyright headers
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 15
diff changeset
4 #
dfcf1a46fc56 Added GPLv2 file & copyright headers
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 15
diff changeset
5 # This program is free software; you can redistribute it and/or modify
dfcf1a46fc56 Added GPLv2 file & copyright headers
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 15
diff changeset
6 # it under the terms of the GNU General Public License version 2 as
dfcf1a46fc56 Added GPLv2 file & copyright headers
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 15
diff changeset
7 # published by the Free Software Foundation.
2
80a717f97cef Basic definition of a playlist class
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
8
32
f97eb9f0c207 Add random song selection for default queue
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 21
diff changeset
9 import random
15
ae3451bedeb6 Implemented playlist searching
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 11
diff changeset
10 import xmms, re
3
dd00b69169c6 Created initial ui thread code, fixed up playlist code
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 2
diff changeset
11
dd00b69169c6 Created initial ui thread code, fixed up playlist code
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 2
diff changeset
12 LIST_PRIO = 0
dd00b69169c6 Created initial ui thread code, fixed up playlist code
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 2
diff changeset
13 LIST_DEFAULT = 1
dd00b69169c6 Created initial ui thread code, fixed up playlist code
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 2
diff changeset
14
2
80a717f97cef Basic definition of a playlist class
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
15 class playlist:
3
dd00b69169c6 Created initial ui thread code, fixed up playlist code
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 2
diff changeset
16 def __init__(self, allowrandom=False):
2
80a717f97cef Basic definition of a playlist class
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
17 self.__list = []
3
dd00b69169c6 Created initial ui thread code, fixed up playlist code
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 2
diff changeset
18 self.__allowrand = allowrandom # allow randomization of this list
32
f97eb9f0c207 Add random song selection for default queue
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 21
diff changeset
19 self.__cursor = 0
2
80a717f97cef Basic definition of a playlist class
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
20
3
dd00b69169c6 Created initial ui thread code, fixed up playlist code
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 2
diff changeset
21 def enqueue(self, song):
dd00b69169c6 Created initial ui thread code, fixed up playlist code
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 2
diff changeset
22 """ Append a song to the list """
2
80a717f97cef Basic definition of a playlist class
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
23 self.__list.append(song)
80a717f97cef Basic definition of a playlist class
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
24
3
dd00b69169c6 Created initial ui thread code, fixed up playlist code
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 2
diff changeset
25 def dequeue(self, song):
dd00b69169c6 Created initial ui thread code, fixed up playlist code
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 2
diff changeset
26 """ Remove a song from the list """
2
80a717f97cef Basic definition of a playlist class
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
27 self.__list.remove(song)
80a717f97cef Basic definition of a playlist class
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
28
80a717f97cef Basic definition of a playlist class
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
29 def pop(self):
3
dd00b69169c6 Created initial ui thread code, fixed up playlist code
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 2
diff changeset
30 """ Pop the next song """
11
50584a5c300e Fixed up code to make more sense
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 9
diff changeset
31 return self.__list.pop(0)
2
80a717f97cef Basic definition of a playlist class
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff changeset
32
32
f97eb9f0c207 Add random song selection for default queue
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 21
diff changeset
33 def next(self):
f97eb9f0c207 Add random song selection for default queue
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 21
diff changeset
34 """ Get the next song """
33
99983189d4b2 Redo next song selection logic
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 32
diff changeset
35 def rnd():
99983189d4b2 Redo next song selection logic
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 32
diff changeset
36 return random.randint(0, len(self.__list)-1)
99983189d4b2 Redo next song selection logic
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 32
diff changeset
37
99983189d4b2 Redo next song selection logic
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 32
diff changeset
38 ##
99983189d4b2 Redo next song selection logic
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 32
diff changeset
39
99983189d4b2 Redo next song selection logic
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 32
diff changeset
40 idx = self.__cursor
32
f97eb9f0c207 Add random song selection for default queue
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 21
diff changeset
41
f97eb9f0c207 Add random song selection for default queue
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 21
diff changeset
42 if not self.__allowrand:
f97eb9f0c207 Add random song selection for default queue
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 21
diff changeset
43 self.__cursor += 1
f97eb9f0c207 Add random song selection for default queue
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 21
diff changeset
44 if self.__cursor >= len(self.__list):
f97eb9f0c207 Add random song selection for default queue
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 21
diff changeset
45 self.__cursor = 0
33
99983189d4b2 Redo next song selection logic
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 32
diff changeset
46 else:
99983189d4b2 Redo next song selection logic
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 32
diff changeset
47 self.__cursor = rnd()
32
f97eb9f0c207 Add random song selection for default queue
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 21
diff changeset
48
f97eb9f0c207 Add random song selection for default queue
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 21
diff changeset
49 return self.__list[idx]
f97eb9f0c207 Add random song selection for default queue
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 21
diff changeset
50
3
dd00b69169c6 Created initial ui thread code, fixed up playlist code
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 2
diff changeset
51 def __getitem__(self, i):
dd00b69169c6 Created initial ui thread code, fixed up playlist code
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 2
diff changeset
52 """ Get item at position i """
dd00b69169c6 Created initial ui thread code, fixed up playlist code
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 2
diff changeset
53 return self.__list[i]
dd00b69169c6 Created initial ui thread code, fixed up playlist code
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 2
diff changeset
54
8
79340745c952 Reimplemented enqueuing & quit properly, implemented list and number commands
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 3
diff changeset
55 def __len__(self):
79340745c952 Reimplemented enqueuing & quit properly, implemented list and number commands
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 3
diff changeset
56 """ Return the length of the playlist """
79340745c952 Reimplemented enqueuing & quit properly, implemented list and number commands
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 3
diff changeset
57 return len(self.__list)
79340745c952 Reimplemented enqueuing & quit properly, implemented list and number commands
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 3
diff changeset
58
15
ae3451bedeb6 Implemented playlist searching
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 11
diff changeset
59 def search(self, regexp):
ae3451bedeb6 Implemented playlist searching
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 11
diff changeset
60 """ Yield all the songs matching regexp """
ae3451bedeb6 Implemented playlist searching
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 11
diff changeset
61 for song in self.__list:
ae3451bedeb6 Implemented playlist searching
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 11
diff changeset
62 if re.search(regexp, str(song), re.IGNORECASE):
ae3451bedeb6 Implemented playlist searching
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 11
diff changeset
63 yield song
ae3451bedeb6 Implemented playlist searching
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 11
diff changeset
64
9
eaa800169f5b Hooked up the playlists to control xmms!
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 8
diff changeset
65 def index(self, song):
eaa800169f5b Hooked up the playlists to control xmms!
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 8
diff changeset
66 """ Returns the index of the first occurence of song """
eaa800169f5b Hooked up the playlists to control xmms!
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 8
diff changeset
67 return self.__list.index(song)
eaa800169f5b Hooked up the playlists to control xmms!
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents: 8
diff changeset
68