6b79f33d6dc7021218eccb0924a443fbeec50f4f
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
21)     - <entry> = <name> <whitespace> <duration>
Stefan Schuermans allow names for STOP playli...

Stefan Schuermans authored 10 years ago

22)     - <stop point> = <name>
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]+
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 allow names for STOP playli...

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

41)     try:
42)       f = open(filename, "r")
43)       for line in f:
44)         mEntry = self.reEntry.match(line)
45)         if mEntry:
46)           name = mEntry.group(1)
47)           duration = time_fmt.str2sec(mEntry.group(2))
48)           self.entries.append({"type":     "normal",
49)                                "name":     name,
50)                                "duration": duration})
51)           #print("DEBUG playlist entry normal %s %f" %
52)           #      (self.entries[-1]["name"], self.entries[-1]["duration"]))
53)         else:
54)           mStop = self.reStop.match(line)
55)           if mStop:
56)             name = mStop.group(1)
57)             self.entries.append({"type": "stop",
58)                                  "name": name})
59)             #print("DEBUG playlist entry stop")
60)       f.close()
61)     except IOError:
62)       pass
Stefan Schuermans reading playlist

Stefan Schuermans authored 10 years ago

63) 
Stefan Schuermans show playlist in TreeView

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

68)     idx = 0
Stefan Schuermans show playlist in TreeView

Stefan Schuermans authored 10 years ago

69)     for entry in self.entries:
70)       if entry["type"] == "normal":
71)         name = entry["name"]
72)         duration = time_fmt.sec2str(entry["duration"])
73)       else:
Stefan Schuermans allow names for STOP playli...

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

75)         duration = "STOP"
Stefan Schuermans fix pause flag in output, u...

Stefan Schuermans authored 10 years ago

76)       store.append([idx, Pango.Weight.NORMAL, name, duration])
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

77)       idx = idx + 1