Mercurial > vixm
annotate 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 
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 
34
8028f2addb00
Destroy all traces of xmms
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
33
diff
changeset

9 import random, re 
3
dd00b69169c6
Created initial ui thread code, fixed up playlist code
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
2
diff
changeset

10 
dd00b69169c6
Created initial ui thread code, fixed up playlist code
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
2
diff
changeset

11 LIST_PRIO = 0 
dd00b69169c6
Created initial ui thread code, fixed up playlist code
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
2
diff
changeset

12 LIST_DEFAULT = 1 
dd00b69169c6
Created initial ui thread code, fixed up playlist code
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
2
diff
changeset

13 
2
80a717f97cef
Basic definition of a playlist class
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

14 class playlist: 
3
dd00b69169c6
Created initial ui thread code, fixed up playlist code
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
2
diff
changeset

15 def __init__(self, allowrandom=False): 
2
80a717f97cef
Basic definition of a playlist class
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

16 self.__list = [] 
3
dd00b69169c6
Created initial ui thread code, fixed up playlist code
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
2
diff
changeset

17 self.__allowrand = allowrandom # allow randomization of this list 
39
9995931be5b1
Select first song randomly if needed
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
34
diff
changeset

18 self.__cursor = allowrandom and self.__rnd() or 0 
2
80a717f97cef
Basic definition of a playlist class
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

19 
3
dd00b69169c6
Created initial ui thread code, fixed up playlist code
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
2
diff
changeset

20 def enqueue(self, song): 
dd00b69169c6
Created initial ui thread code, fixed up playlist code
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
2
diff
changeset

21 """ Append a song to the list """ 
2
80a717f97cef
Basic definition of a playlist class
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

22 self.__list.append(song) 
80a717f97cef
Basic definition of a playlist class
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

23 
3
dd00b69169c6
Created initial ui thread code, fixed up playlist code
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
2
diff
changeset

24 def dequeue(self, song): 
dd00b69169c6
Created initial ui thread code, fixed up playlist code
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
2
diff
changeset

25 """ Remove a song from the list """ 
2
80a717f97cef
Basic definition of a playlist class
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

26 self.__list.remove(song) 
80a717f97cef
Basic definition of a playlist class
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

27 
80a717f97cef
Basic definition of a playlist class
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

28 def pop(self): 
3
dd00b69169c6
Created initial ui thread code, fixed up playlist code
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
2
diff
changeset

29 """ Pop the next song """ 
11
50584a5c300e
Fixed up code to make more sense
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
9
diff
changeset

30 return self.__list.pop(0) 
2
80a717f97cef
Basic definition of a playlist class
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
diff
changeset

31 
39
9995931be5b1
Select first song randomly if needed
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
34
diff
changeset

32 def __rnd(self): 
9995931be5b1
Select first song randomly if needed
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
34
diff
changeset

33 return random.randint(0, len(self.__list)1) 
9995931be5b1
Select first song randomly if needed
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
34
diff
changeset

34 
32
f97eb9f0c207
Add random song selection for default queue
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
21
diff
changeset

35 def next(self): 
f97eb9f0c207
Add random song selection for default queue
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
21
diff
changeset

36 """ Get the next song """ 
33
99983189d4b2
Redo next song selection logic
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
32
diff
changeset

37 idx = self.__cursor 
32
f97eb9f0c207
Add random song selection for default queue
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
21
diff
changeset

38 
f97eb9f0c207
Add random song selection for default queue
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
21
diff
changeset

39 if not self.__allowrand: 
f97eb9f0c207
Add random song selection for default queue
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
21
diff
changeset

40 self.__cursor += 1 
f97eb9f0c207
Add random song selection for default queue
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
21
diff
changeset

41 if self.__cursor >= len(self.__list): 
f97eb9f0c207
Add random song selection for default queue
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
21
diff
changeset

42 self.__cursor = 0 
33
99983189d4b2
Redo next song selection logic
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
32
diff
changeset

43 else: 
39
9995931be5b1
Select first song randomly if needed
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
34
diff
changeset

44 self.__cursor = self.__rnd() 
32
f97eb9f0c207
Add random song selection for default queue
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
21
diff
changeset

45 
f97eb9f0c207
Add random song selection for default queue
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
21
diff
changeset

46 return self.__list[idx] 
f97eb9f0c207
Add random song selection for default queue
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
21
diff
changeset

47 
3
dd00b69169c6
Created initial ui thread code, fixed up playlist code
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
2
diff
changeset

48 def __getitem__(self, i): 
dd00b69169c6
Created initial ui thread code, fixed up playlist code
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
2
diff
changeset

49 """ 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

50 return self.__list[i] 
dd00b69169c6
Created initial ui thread code, fixed up playlist code
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
2
diff
changeset

51 
8
79340745c952
Reimplemented enqueuing & quit properly, implemented list and number commands
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
3
diff
changeset

52 def __len__(self): 
79340745c952
Reimplemented enqueuing & quit properly, implemented list and number commands
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
3
diff
changeset

53 """ 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

54 return len(self.__list) 
79340745c952
Reimplemented enqueuing & quit properly, implemented list and number commands
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
3
diff
changeset

55 
15
ae3451bedeb6
Implemented playlist searching
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
11
diff
changeset

56 def search(self, regexp): 
ae3451bedeb6
Implemented playlist searching
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
11
diff
changeset

57 """ Yield all the songs matching regexp """ 
ae3451bedeb6
Implemented playlist searching
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
11
diff
changeset

58 for song in self.__list: 
ae3451bedeb6
Implemented playlist searching
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
11
diff
changeset

59 if re.search(regexp, str(song), re.IGNORECASE): 
ae3451bedeb6
Implemented playlist searching
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
11
diff
changeset

60 yield song 
ae3451bedeb6
Implemented playlist searching
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
11
diff
changeset

61 
9
eaa800169f5b
Hooked up the playlists to control xmms!
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
8
diff
changeset

62 def index(self, song): 
eaa800169f5b
Hooked up the playlists to control xmms!
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
8
diff
changeset

63 """ 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

64 return self.__list.index(song) 
eaa800169f5b
Hooked up the playlists to control xmms!
Josef "Jeff" Sipek <jeffpc@josefsipek.net>
parents:
8
diff
changeset

65 