Update MPD/status

This commit is contained in:
Stein Magnus Jodal 2011-03-20 00:56:43 +01:00
parent f73268c332
commit 0de8d1b4b8
3 changed files with 47 additions and 34 deletions

View File

@ -68,8 +68,8 @@ class PlaybackController(object):
self._state = self.STOPPED
self._shuffled = []
self._first_shuffle = True
self._play_time_accumulated = 0
self._play_time_started = None
self.play_time_accumulated = 0
self.play_time_started = None
def destroy(self):
"""
@ -275,7 +275,7 @@ class PlaybackController(object):
def state(self, new_state):
(old_state, self._state) = (self.state, new_state)
logger.debug(u'Changing state: %s -> %s', old_state, new_state)
# FIXME _play_time stuff assumes backend does not have a better way of
# FIXME play_time stuff assumes backend does not have a better way of
# handeling this stuff :/
if (old_state in (self.PLAYING, self.STOPPED)
and new_state == self.PLAYING):
@ -290,23 +290,23 @@ class PlaybackController(object):
"""Time position in milliseconds."""
if self.state == self.PLAYING:
time_since_started = (self._current_wall_time -
self._play_time_started)
return self._play_time_accumulated + time_since_started
self.play_time_started)
return self.play_time_accumulated + time_since_started
elif self.state == self.PAUSED:
return self._play_time_accumulated
return self.play_time_accumulated
elif self.state == self.STOPPED:
return 0
def _play_time_start(self):
self._play_time_accumulated = 0
self._play_time_started = self._current_wall_time
self.play_time_accumulated = 0
self.play_time_started = self._current_wall_time
def _play_time_pause(self):
time_since_started = self._current_wall_time - self._play_time_started
self._play_time_accumulated += time_since_started
time_since_started = self._current_wall_time - self.play_time_started
self.play_time_accumulated += time_since_started
def _play_time_resume(self):
self._play_time_started = self._current_wall_time
self.play_time_started = self._current_wall_time
@property
def _current_wall_time(self):
@ -439,8 +439,8 @@ class PlaybackController(object):
self.next()
return True
self._play_time_started = self._current_wall_time
self._play_time_accumulated = time_position
self.play_time_started = self._current_wall_time
self.play_time_accumulated = time_position
return self.provider.seek(time_position)

View File

@ -1,6 +1,6 @@
from mopidy.backends.base import PlaybackController
from mopidy.frontends.mpd.protocol import handle_pattern
from mopidy.frontends.mpd.exceptions import MpdNotImplemented
from mopidy.backends.base import PlaybackController
@handle_pattern(r'^clearerror$')
def clearerror(frontend):
@ -24,10 +24,11 @@ def currentsong(frontend):
Displays the song info of the current song (same song that is
identified in status).
"""
if frontend.backend.playback.current_track is not None:
return frontend.backend.playback.current_track.mpd_format(
position=frontend.backend.playback.current_playlist_position,
cpid=frontend.backend.playback.current_cpid)
current_cp_track = frontend.backend.playback.current_cp_track.get()
if current_cp_track is not None:
return current_cp_track[1].mpd_format(
position=frontend.backend.playback.current_playlist_position.get(),
cpid=current_cp_track[0])
@handle_pattern(r'^idle$')
@handle_pattern(r'^idle (?P<subsystems>.+)$')
@ -141,19 +142,20 @@ def status(frontend):
('xfade', _status_xfade(frontend)),
('state', _status_state(frontend)),
]
if frontend.backend.playback.current_track is not None:
if frontend.backend.playback.current_track.get() is not None:
result.append(('song', _status_songpos(frontend)))
result.append(('songid', _status_songid(frontend)))
if frontend.backend.playback.state in (frontend.backend.playback.PLAYING,
frontend.backend.playback.PAUSED):
if frontend.backend.playback.state.get() in (PlaybackController.PLAYING,
PlaybackController.PAUSED):
result.append(('time', _status_time(frontend)))
result.append(('elapsed', _status_time_elapsed(frontend)))
result.append(('bitrate', _status_bitrate(frontend)))
return result
def _status_bitrate(frontend):
if frontend.backend.playback.current_track is not None:
return frontend.backend.playback.current_track.get().bitrate
current_track = frontend.backend.playback.current_track.get()
if current_track is not None:
return current_track.bitrate
def _status_consume(frontend):
if frontend.backend.playback.consume.get():
@ -177,8 +179,9 @@ def _status_single(frontend):
return int(frontend.backend.playback.single.get())
def _status_songid(frontend):
if frontend.backend.playback.current_cpid is not None:
return frontend.backend.playback.current_cpid
current_cpid = frontend.backend.playback.current_cpid.get()
if current_cpid is not None:
return current_cpid
else:
return _status_songpos(frontend)

View File

@ -1,14 +1,24 @@
import unittest
from mopidy.backends.base import PlaybackController
from mopidy.backends.dummy import DummyBackend
from mopidy.frontends.mpd import dispatcher
from mopidy.mixers.dummy import DummyMixer
from mopidy.models import Track
PAUSED = PlaybackController.PAUSED
PLAYING = PlaybackController.PLAYING
STOPPED = PlaybackController.STOPPED
class StatusHandlerTest(unittest.TestCase):
def setUp(self):
self.b = DummyBackend(mixer_class=DummyMixer)
self.h = dispatcher.MpdDispatcher(backend=self.b)
self.b = DummyBackend.start().proxy()
self.mixer = DummyMixer.start().proxy()
self.h = dispatcher.MpdDispatcher()
def tearDown(self):
self.b.stop().get()
self.mixer.stop().get()
def test_clearerror(self):
result = self.h.handle_request(u'clearerror')
@ -77,7 +87,7 @@ class StatusHandlerTest(unittest.TestCase):
self.assertEqual(int(result['volume']), 0)
def test_status_method_contains_volume(self):
self.b.mixer.volume = 17
self.mixer.volume = 17
result = dict(dispatcher.status.status(self.h))
self.assert_('volume' in result)
self.assertEqual(int(result['volume']), 17)
@ -136,20 +146,20 @@ class StatusHandlerTest(unittest.TestCase):
self.assert_(int(result['xfade']) >= 0)
def test_status_method_contains_state_is_play(self):
self.b.playback.state = self.b.playback.PLAYING
self.b.playback.state = PLAYING
result = dict(dispatcher.status.status(self.h))
self.assert_('state' in result)
self.assertEqual(result['state'], 'play')
def test_status_method_contains_state_is_stop(self):
self.b.playback.state = self.b.playback.STOPPED
self.b.playback.state = STOPPED
result = dict(dispatcher.status.status(self.h))
self.assert_('state' in result)
self.assertEqual(result['state'], 'stop')
def test_status_method_contains_state_is_pause(self):
self.b.playback.state = self.b.playback.PLAYING
self.b.playback.state = self.b.playback.PAUSED
self.b.playback.state = PLAYING
self.b.playback.state = PAUSED
result = dict(dispatcher.status.status(self.h))
self.assert_('state' in result)
self.assertEqual(result['state'], 'pause')
@ -189,8 +199,8 @@ class StatusHandlerTest(unittest.TestCase):
self.assert_(position <= total)
def test_status_method_when_playing_contains_elapsed(self):
self.b.playback.state = self.b.playback.PAUSED
self.b.playback._play_time_accumulated = 59123
self.b.playback.state = PAUSED
self.b.playback.play_time_accumulated = 59123
result = dict(dispatcher.status.status(self.h))
self.assert_('elapsed' in result)
self.assertEqual(int(result['elapsed']), 59123)