forked from albertz/music-player
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathState.py
240 lines (204 loc) · 6.62 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# -*- coding: utf-8 -*-
# MusicPlayer, https://github.com/albertz/music-player
# Copyright (c) 2012, Albert Zeyer, www.az2000.de
# All rights reserved.
# This code is under the 2-clause BSD license, see License.txt in the root directory of this project.
from utils import *
import Traits
from Song import Song
from collections import deque
from threading import RLock
import appinfo
import gui
class RecentlyplayedList(object):
GuiLimit = 5
Limit = 500
def __init__(self, list=[], previous=None, index=0):
self.lock = RLock()
self.index = index
self.list = deque(list)
# Be careful what we do with `previous` here. If it is not None,
# we expect it to be a PersistentObject and we really don't want
# to load it here, otherwise *all* of RecentlyplayedList would
# be unfolded!
# Even `PyObject_IsTrue(previous)` would load it, so just
# compare with `None`.
if previous is not None:
if not getattr(previous, "_isPersistentObject", False):
# This was some bug from earlier... Fix it now.
print "Warning: RecentlyplayedList.previous is not a PersistentObject"
previous = PersistentObject(RecentlyplayedList, "recentlyplayed-%i.dat" % previous.index, persistentRepr=True)
elif not previous._persistentRepr:
# This was some bug from earlier... Fix it now.
print "Warning: RecentlyplayedList.previous not persistentRepr"
previous = PersistentObject(RecentlyplayedList, previous._filename, persistentRepr=True)
assert previous._isPersistentObject
assert previous._persistentRepr
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.push(guiOldLen, song)
if guiOldLen == self.GuiLimit: self.onRemove.push(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)
@initBy
def onInsert(self): return Event() # (index, value)
@initBy
def onRemove(self): return Event() # (index)
@initBy
def onClear(self): return Event() # ()
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):
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 Exception: 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 Exception: 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, variableHeight=True)
@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):
# XXX: Is this still used?
# XXX: doesn't really work. OSX ignores the SIGINT if the cocoa mainloop runs
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 thread
thread.start_new_thread(doQuit, ())
# Only init new state if it is new, not at module reload.
try:
state
except NameError:
state = State()
gui.registerRootObj(obj=state, name="Main", title=appinfo.progname, priority=0, keyShortcut='1')
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",
"socketcontrol",
"mpdBackend",
"notifications",
"preloader",
"songdb",
]:
if not getModule(modname):
modules.append(Module(modname))
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()
class About(object):
@UserAttrib(type=Traits.OneLineText)
def appname(self):
import appinfo
return appinfo.progname
@UserAttrib(type=Traits.OneLineText)
def developer(self):
return "by Albert Zeyer"
@UserAttrib(type=Traits.Action, name="Homepage")
def homepage(self):
import gui
gui.about()
about = About()
gui.registerRootObj(obj=about, name="About", priority=-9)