bc6b4825c56a2cc8d04b4b703bf2d103ebc56cd1
Stefan Schuermans reading playlist

Stefan Schuermans authored 10 years ago

1) #! /usr/bin/env python
2) 
3) import re
Stefan Schuermans show current playlist item...

Stefan Schuermans authored 10 years ago

4) import pango
Stefan Schuermans reading playlist

Stefan Schuermans authored 10 years ago

5) 
6) import time_fmt
7) 
8) class Playlist:
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

41)         self.entries.append({"type":     "normal",
42)                              "name":     name,
43)                              "duration": duration})
Stefan Schuermans show current playlist item...

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

46)       else:
Stefan Schuermans show playlist in TreeView

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

49)     f.close()
50) 
Stefan Schuermans show playlist in TreeView

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

55)     idx = 0
Stefan Schuermans show playlist in TreeView

Stefan Schuermans authored 10 years ago

56)     for entry in self.entries:
57)       if entry["type"] == "normal":
58)         name = entry["name"]
59)         duration = time_fmt.sec2str(entry["duration"])
60)       else:
61)         name = ""
62)         duration = "STOP"
Stefan Schuermans show current playlist item...

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

64)       idx = idx + 1