#! /usr/bin/env python import re import pango import time_fmt class Playlist: """ playlist object, reads a playlist form a file and handles the playlist entries playlist file format: - each line is an entry or a stop point: = | - entries are played one after another - playing halts at stop points - = - = "" - = [A-Za-Z0-9_]+ - = ((:)?:)? - = [0-9]+ - = [0-9]+ - = [0-9]+(.[0-9]+)""" def __init__(self): """create a new, empty playlist object""" self.entries = [] # list of entries # entry = dictionary { "type": "normal" or "stop" # "name": string, name of entry # "durtaion": float, in seconds } self.reEntry = re.compile("^\s*([A-Za-z0-9_]+)\s+([0-9:.]+)\s*$") def read(self, filename): """read the playlist from filename, replacing the current playlist""" self.entries = [] f = open(filename, "r") for line in f: mEntry = self.reEntry.match(line) if mEntry: name = mEntry.group(1) duration = time_fmt.str2sec(mEntry.group(2)) self.entries.append({"type": "normal", "name": name, "duration": duration}) #print("DEBUG playlist entry normal %s %f" % # (self.entries[-1]["name"], self.entries[-1]["duration"])) else: self.entries.append({"type": "stop"}) #print("DEBUG playlist entry stop") f.close() def update(self, store): """update the contents of a Gtk ListStore with the contents of this playlist""" store.clear() idx = 0 for entry in self.entries: if entry["type"] == "normal": name = entry["name"] duration = time_fmt.sec2str(entry["duration"]) else: name = "" duration = "STOP" store.append([idx, pango.WEIGHT_NORMAL, name, duration]) idx = idx + 1