59e6b12ec37c677806c4d472118815ffb85f0e06
Stefan Schuermans reading playlist

Stefan Schuermans authored 10 years ago

1) #! /usr/bin/env python
2) 
Stefan Schuermans add copyright, remove statu...

Stefan Schuermans authored 10 years ago

3) # BlinkenArea Sync GUI
4) # version 0.1.0 date 2013-11-23
5) # Copyright 2013 Stefan Schuermans <stefan@blinkenarea.org>
6) # Copyleft: GNU public license - http://www.gnu.org/copyleft/gpl.html
7) # a blinkenarea.org project - https://www.blinkenarea.org/
8) 
Stefan Schuermans reading playlist

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

10) import pango
Stefan Schuermans reading playlist

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

36)     self.reEntry = re.compile("^\s*([A-Za-z0-9_]+)\s+([0-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 = []
41)     f = open(filename, "r")
42)     for line in f:
43)       mEntry = self.reEntry.match(line)
44)       if mEntry:
45)         name = mEntry.group(1)
46)         duration = time_fmt.str2sec(mEntry.group(2))
Stefan Schuermans show playlist in TreeView

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

52)       else:
Stefan Schuermans show playlist in TreeView

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

61)     idx = 0
Stefan Schuermans show playlist in TreeView

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

70)       idx = idx + 1