emmental/curds/playlist/playlist.py

36 lines
1.1 KiB
Python

# Copyright 2019 (c) Anna Schumaker.
class Playlist(list):
notifications = dict()
def __init__(self, name):
self.name = name
def add(self, track):
if track is not None:
self.append(track)
self.notify("on-add", track)
def never_mind(name, func):
cb = Playlist.notifications.get(name, [])
if func in cb:
cb.remove(func)
def notify_me(name, func):
Playlist.notifications.setdefault(name, []).append(func)
def notify(self, name, *args):
for cb in Playlist.notifications.get(name, []):
cb(self, *args)
def runtime(self):
m, s = divmod(sum([ track.length for track in self ]), 60)
h, m = divmod(m, 60)
d, h = divmod(h, 24)
res = [ ]
if d > 0: res.append(f"{int(d)} day{'s' if d != 1 else ''}")
if h > 0: res.append(f"{int(h)} hour{'s' if h != 1 else ''}")
if m > 0: res.append(f"{int(m)} minute{'s' if m != 1 else ''}")
if s > 0: res.append(f"{int(s)} second{'s' if s != 1 else ''}")
return ", ".join(res)