Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
191 lines (163 sloc) 4.79 KB
# -*- coding: utf-8 -*-
from utils import *
import Traits
from Song import Song
from collections import deque
from threading import RLock
class RecentlyplayedList:
GuiLimit = 5
Limit = 500
def __init__(self, list=[], previous=None, index=0):
self.lock = RLock()
self.index = index
self.list = deque(list)
self.previous = previous
def append(self, song):
if not song: return
with self.lock:
guiOldLen = len(self)
if len(self.list) >= self.Limit:
newList = PersistentObject(RecentlyplayedList, "recentlyplayed-%i.dat" % self.index, persistentRepr=True)
newList.index = self.index
newList.list = self.list
newList.previous = self.previous
self.index += 1
self.previous = newList
self.list = deque()
self.onInsert(guiOldLen, song)
if guiOldLen == self.GuiLimit: self.onRemove(0)
def getLastN(self, n):
with self.lock:
#return list(self.list)[-n:] # not using this for now as a bit too heavy. I timeit'd it. this is 14 times slower for n=10, len(l)=10000
l = self.list
if n <= len(l):
return [l[-i] for i in range(1,n+1)]
last = [l[-i] for i in range(1,len(l)+1)]
if self.previous:
last += self.previous.getLastN(n - len(l))
return last
def __repr__(self):
return "RecentlyplayedList(list=%s, previous=%s, index=%i)" % (
def onInsert(self, index, value): pass
def onRemove(self, index): pass
def onClear(self): pass
def __getitem__(self, index):
with self.lock:
return self.getLastN(self.GuiLimit)[-index - 1]
def __len__(self):
c = len(self.list)
if c >= self.GuiLimit: return self.GuiLimit
if self.previous:
c += len(self.previous)
return min(c, self.GuiLimit)
class State(object):
def playPauseUpdate(self, attrib, *args):
if self.player.playing: = "❚❚"
else: = ""
@UserAttrib(type=Traits.Action, name="", updateHandler=playPauseUpdate)
def playPause(self):
self.player.playing = not self.player.playing
@UserAttrib(type=Traits.Action, name="▶▶|", alignRight=True)
def nextSong(self):
@UserAttrib(type=Traits.OneLineText, alignRight=True, variableWidth=True, withBorder=True)
def curSongStr(self):
if not self.player.curSong: return ""
try: return self.player.curSong.userString
except: return "???"
@UserAttrib(type=Traits.OneLineText, alignRight=True, autosizeWidth=True, withBorder=True)
def curSongPos(self):
if not self.player.curSong: return ""
try: return formatTime(self.player.curSongPos) + " / " + formatTime(self.player.curSong.duration)
except: return "???"
@UserAttrib(type=Traits.SongDisplay, variableWidth=True)
def curSongDisplay(self): pass
def _volume(self): return PersistentObject(float, "volume.dat", defaultArgs=(0.9,))
@UserAttrib(type=Traits.Real(min=0, max=2), alignRight=True, height=80, width=25)
def volume(self):
return self._volume
def volume(self, updateValue):
self._volume = updateValue
self.player.volume = updateValue
@UserAttrib(type=Traits.List, lowlight=True, autoScrolldown=True)
def recentlyPlayedList(self): return PersistentObject(RecentlyplayedList, "recentlyplayed.dat")
@UserAttrib(type=Traits.Object, spaceY=0, highlight=True)
def curSong(self): return PersistentObject(Song, "cursong.dat")
@UserAttrib(type=Traits.Object, spaceY=0)
def queue(self):
import queue
return queue.queue
def updates(self): return OnRequestQueue()
def player(self):
from player import loadPlayer
return loadPlayer(self)
def quit(self):
def doQuit():
""" This works in all threads except the main thread. It will quit the whole app.
For more information about why we do it this way, read the comment in
import sys, os, signal
os.kill(0, signal.SIGINT)
sys.stdin.close() # so that the terminal closes, if it is used
import gui
gui.quit() # might do some additional stuff
import thread
thread.start_new_thread(doQuit, ())
# Only init new state if it is new, not at module reload.
except NameError:
state = State()
except NameError:
modules = []
def getModule(modname):
for m in modules:
if == modname: return m
return None
for modname in [
if not getModule(modname):
for m in modules:
print m
def reloadModules():
# reload some custom random Python modules
import utils
import Song, State
# reload all our modules
for m in modules: