8ab741326ebac654317cb1db408bf0694d6feb84
Stefan Schuermans reading playlist

Stefan Schuermans authored 10 years ago

1) #! /usr/bin/env python
2) 
3) import re
4) 
5) import time_fmt
6) 
7) class Playlist:
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

24)     """create a new, empty playlist object"""
25)     self.entries = [] # list of entries
26)                       # entry = dictionary { "type": "normal" or "stop"
27)                       #                      "name": string, name of entry
28)                       #                      "durtaion": float, in seconds }
Stefan Schuermans show playlist in TreeView

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

33)     self.entries = []
34)     f = open(filename, "r")
35)     for line in f:
36)       mEntry = self.reEntry.match(line)
37)       if mEntry:
38)         name = mEntry.group(1)
39)         duration = time_fmt.str2sec(mEntry.group(2))
Stefan Schuermans show playlist in TreeView

Stefan Schuermans authored 10 years ago

40)         self.entries.append({"type":     "normal",
41)                              "name":     name,
42)                              "duration": duration})
43)         print("entry normal %s %f" % (self.entries[-1]["name"],
44)                                       self.entries[-1]["duration"]))
Stefan Schuermans reading playlist

Stefan Schuermans authored 10 years ago

45)       else:
Stefan Schuermans show playlist in TreeView

Stefan Schuermans authored 10 years ago

46)         self.entries.append({"type": "stop"})
Stefan Schuermans reading playlist

Stefan Schuermans authored 10 years ago

47)         print("entry stop")
48)     f.close()
49) 
Stefan Schuermans show playlist in TreeView

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

54)     idx = 0
Stefan Schuermans show playlist in TreeView

Stefan Schuermans authored 10 years ago

55)     for entry in self.entries:
56)       if entry["type"] == "normal":
57)         name = entry["name"]
58)         duration = time_fmt.sec2str(entry["duration"])
59)       else:
60)         name = ""
61)         duration = "STOP"
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

62)       store.append([idx, name, duration])
63)       idx = idx + 1