forked from albertz/music-player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
State.py
190 lines (163 loc) · 4.79 KB
/
State.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# -*- 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)
self.list.append(song)
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
newList.save()
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)]
else:
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)" % (
betterRepr(list(self.list)),
betterRepr(self.previous),
self.index)
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:
attrib.name = "❚❚"
else:
attrib.name = "▶"
@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):
self.player.nextSong()
@UserAttrib(type=Traits.OneLineText, alignRight=True, variableWidth=True, withBorder=True)
@property
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)
@property
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
@initBy
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)
@property
def volume(self):
return self._volume
@volume.callDeco.setter
def volume(self, updateValue):
self._volume = updateValue
self._volume.save()
self.player.volume = updateValue
@UserAttrib(type=Traits.List, lowlight=True, autoScrolldown=True)
@initBy
def recentlyPlayedList(self): return PersistentObject(RecentlyplayedList, "recentlyplayed.dat")
@UserAttrib(type=Traits.Object, spaceY=0, highlight=True)
@initBy
def curSong(self): return PersistentObject(Song, "cursong.dat")
@UserAttrib(type=Traits.Object, spaceY=0)
@initBy
def queue(self):
import queue
return queue.queue
@initBy
def updates(self): return OnRequestQueue()
@initBy
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 main.py.
"""
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.
try:
state
except NameError:
state = State()
try:
modules
except NameError:
modules = []
def getModule(modname):
for m in modules:
if m.name == modname: return m
return None
for modname in [
"player",
"queue",
"tracker",
"tracker_lastfm",
"mediakeys",
"gui",
"stdinconsole",
"notifications",
"preloader",
"songdb",
]:
if not getModule(modname):
modules.append(Module(modname))
for m in modules:
print m
def reloadModules():
# reload some custom random Python modules
import utils
reload(utils)
import Song, State
reload(Song)
reload(State)
# reload all our modules
for m in modules:
m.reload()