6d1a6cb3ad2b61c8c417a85ead564d8bf58c3dda
Stefan Schuermans reading playlist

Stefan Schuermans authored 10 years ago

1) #! /usr/bin/env python
2) 
Stefan Schuermans comments by ST

Stefan Schuermans authored 10 years ago

3) # BlinkenArea Stage Director
Stefan Schuermans add copyright, remove statu...

Stefan Schuermans authored 10 years ago

4) # Copyright 2013 Stefan Schuermans <stefan@blinkenarea.org>
5) # Copyleft: GNU public license - http://www.gnu.org/copyleft/gpl.html
6) # a blinkenarea.org project - https://www.blinkenarea.org/
7) 
Stefan Schuermans reading playlist

Stefan Schuermans authored 10 years ago

8) import re
Stefan Schuermans show current playlist item...

Stefan Schuermans authored 10 years ago

9) import pango
Stefan Schuermans reading playlist

Stefan Schuermans authored 10 years ago

10) 
11) import time_fmt
12) 
13) class Playlist:
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

14)   """ playlist object, reads a playlist form a file and handles the playlist
15)   entries
16) 
17)   playlist file format:
18)     - each line is an entry or a stop point: <line> = <entry> | <stop point>
19)     - entries are played one after another
20)     - playing halts at stop points
21)     - <entry> = <name> <whitespace> <duration>
22)     - <stop point> = ""
23)     - <name> = [A-Za-Z0-9_]+
24)     - <duration> = ((<hours>:)?<minutes>:)?<seconds>
25)     - <hours> = [0-9]+
26)     - <minutes> = [0-9]+
27)     - <seconds> = [0-9]+(.[0-9]+)"""
28) 
Stefan Schuermans reading playlist

Stefan Schuermans authored 10 years ago

29)   def __init__(self):
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

30)     """create a new, empty playlist object"""
31)     self.entries = [] # list of entries
32)                       # entry = dictionary { "type": "normal" or "stop"
33)                       #                      "name": string, name of entry
34)                       #                      "durtaion": float, in seconds }
Stefan Schuermans show playlist in TreeView

Stefan Schuermans authored 10 years ago

35)     self.reEntry = re.compile("^\s*([A-Za-z0-9_]+)\s+([0-9:.]+)\s*$")
Stefan Schuermans reading playlist

Stefan Schuermans authored 10 years ago

36) 
37)   def read(self, filename):
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

38)     """read the playlist from filename, replacing the current playlist"""
Stefan Schuermans reading playlist

Stefan Schuermans authored 10 years ago

39)     self.entries = []
40)     f = open(filename, "r")
41)     for line in f:
42)       mEntry = self.reEntry.match(line)
43)       if mEntry:
44)         name = mEntry.group(1)
45)         duration = time_fmt.str2sec(mEntry.group(2))
Stefan Schuermans show playlist in TreeView

Stefan Schuermans authored 10 years ago

46)         self.entries.append({"type":     "normal",
47)                              "name":     name,
48)                              "duration": duration})
Stefan Schuermans show current playlist item...

Stefan Schuermans authored 10 years ago

49)         #print("DEBUG playlist entry normal %s %f" %
50)         #      (self.entries[-1]["name"], self.entries[-1]["duration"]))
Stefan Schuermans reading playlist

Stefan Schuermans authored 10 years ago

51)       else:
Stefan Schuermans show playlist in TreeView

Stefan Schuermans authored 10 years ago

52)         self.entries.append({"type": "stop"})
Stefan Schuermans show current playlist item...

Stefan Schuermans authored 10 years ago

53)         #print("DEBUG playlist entry stop")
Stefan Schuermans reading playlist

Stefan Schuermans authored 10 years ago

54)     f.close()
55) 
Stefan Schuermans show playlist in TreeView

Stefan Schuermans authored 10 years ago

56)   def update(self, store):
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

57)     """update the contents of a Gtk ListStore with the contents of this
58)     playlist"""
Stefan Schuermans show playlist in TreeView

Stefan Schuermans authored 10 years ago

59)     store.clear()
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

60)     idx = 0
Stefan Schuermans show playlist in TreeView

Stefan Schuermans authored 10 years ago

61)     for entry in self.entries:
62)       if entry["type"] == "normal":
63)         name = entry["name"]
64)         duration = time_fmt.sec2str(entry["duration"])
65)       else:
66)         name = ""
67)         duration = "STOP"
Stefan Schuermans show current playlist item...

Stefan Schuermans authored 10 years ago

68)       store.append([idx, pango.WEIGHT_NORMAL, name, duration])
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

69)       idx = idx + 1