ae5def016f9ebe9e7be4f31f286bb6451f2eff7f
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 allow names for STOP playli...

Stefan Schuermans authored 10 years ago

4) # Copyright 2013-2014 Stefan Schuermans <stefan@blinkenarea.org>
Stefan Schuermans add copyright, remove statu...

Stefan Schuermans authored 10 years ago

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 use Pango from GTK3/gobj, d...

Stefan Schuermans authored 10 years ago

9) from gi.repository 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
Stefan Schuermans add optional comment in pla...

Stefan Schuermans authored 10 years ago

21)     - <entry> = <name> <whitespace> <duration> <opt_comment>
22)     - <stop point> = <name> <opt_comment>
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

23)     - <name> = [A-Za-Z0-9_]+
24)     - <duration> = ((<hours>:)?<minutes>:)?<seconds>
25)     - <hours> = [0-9]+
26)     - <minutes> = [0-9]+
Stefan Schuermans add optional comment in pla...

Stefan Schuermans authored 10 years ago

27)     - <seconds> = [0-9]+(.[0-9]+)
28)     - <opt_comment> = <whitespace> "#" <whitespace> <some text>"""
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

29) 
Stefan Schuermans reading playlist

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

41)     self.entries = []
Stefan Schuermans allow names for STOP playli...

Stefan Schuermans authored 10 years ago

42)     try:
43)       f = open(filename, "r")
44)       for line in f:
45)         mEntry = self.reEntry.match(line)
46)         if mEntry:
47)           name = mEntry.group(1)
48)           duration = time_fmt.str2sec(mEntry.group(2))
Stefan Schuermans add optional comment in pla...

Stefan Schuermans authored 10 years ago

49)           if mEntry.group(4) is not None:
50)             comment = mEntry.group(4)
51)           else:
52)             comment = ""
Stefan Schuermans allow names for STOP playli...

Stefan Schuermans authored 10 years ago

53)           self.entries.append({"type":     "normal",
54)                                "name":     name,
Stefan Schuermans add optional comment in pla...

Stefan Schuermans authored 10 years ago

55)                                "duration": duration,
56)                                "comment":  comment})
Stefan Schuermans allow names for STOP playli...

Stefan Schuermans authored 10 years ago

57)           #print("DEBUG playlist entry normal %s %f" %
58)           #      (self.entries[-1]["name"], self.entries[-1]["duration"]))
59)         else:
60)           mStop = self.reStop.match(line)
61)           if mStop:
62)             name = mStop.group(1)
Stefan Schuermans add optional comment in pla...

Stefan Schuermans authored 10 years ago

63)             if mStop.group(3) is not None:
64)               comment = mStop.group(3)
65)             else:
66)               comment = ""
Stefan Schuermans allow names for STOP playli...

Stefan Schuermans authored 10 years ago

67)             self.entries.append({"type": "stop",
Stefan Schuermans add optional comment in pla...

Stefan Schuermans authored 10 years ago

68)                                  "name": name,
69)                                  "comment":  comment})
Stefan Schuermans allow names for STOP playli...

Stefan Schuermans authored 10 years ago

70)             #print("DEBUG playlist entry stop")
71)       f.close()
72)     except IOError:
73)       pass
Stefan Schuermans reading playlist

Stefan Schuermans authored 10 years ago

74) 
Stefan Schuermans show playlist in TreeView

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

79)     idx = 0
Stefan Schuermans show playlist in TreeView

Stefan Schuermans authored 10 years ago

80)     for entry in self.entries:
81)       if entry["type"] == "normal":
82)         name = entry["name"]
83)         duration = time_fmt.sec2str(entry["duration"])
Stefan Schuermans add optional comment in pla...

Stefan Schuermans authored 10 years ago

84)         comment = entry["comment"]
Stefan Schuermans show playlist in TreeView

Stefan Schuermans authored 10 years ago

85)       else:
Stefan Schuermans allow names for STOP playli...

Stefan Schuermans authored 10 years ago

86)         name = entry["name"]
Stefan Schuermans show playlist in TreeView

Stefan Schuermans authored 10 years ago

87)         duration = "STOP"
Stefan Schuermans add optional comment in pla...

Stefan Schuermans authored 10 years ago

88)         comment = entry["comment"]
89)       store.append([idx, Pango.Weight.NORMAL, name, duration, comment])
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

90)       idx = idx + 1