Simplify 'playlistinfo' implementation further, guided by new test asserts

This commit is contained in:
Stein Magnus Jodal 2011-12-28 00:57:43 +01:00
parent 716c5b03e2
commit 7b0954bef8
2 changed files with 23 additions and 14 deletions

View File

@ -225,6 +225,7 @@ def playlistid(context, cpid=None):
context.backend.current_playlist.cp_tracks.get()) context.backend.current_playlist.cp_tracks.get())
@handle_request(r'^playlistinfo$') @handle_request(r'^playlistinfo$')
@handle_request(r'^playlistinfo "-1"$')
@handle_request(r'^playlistinfo "(?P<songpos>-?\d+)"$') @handle_request(r'^playlistinfo "(?P<songpos>-?\d+)"$')
@handle_request(r'^playlistinfo "(?P<start>\d+):(?P<end>\d+)*"$') @handle_request(r'^playlistinfo "(?P<start>\d+):(?P<end>\d+)*"$')
def playlistinfo(context, songpos=None, def playlistinfo(context, songpos=None,
@ -243,20 +244,10 @@ def playlistinfo(context, songpos=None,
- uses negative indexes, like ``playlistinfo "-1"``, to request - uses negative indexes, like ``playlistinfo "-1"``, to request
the entire playlist the entire playlist
""" """
if songpos == "-1":
songpos = None
if songpos is not None: if songpos is not None:
songpos = int(songpos) songpos = int(songpos)
start = songpos cp_track = context.backend.current_playlist.get(cpid=songpos).get()
end = songpos + 1 return track_to_mpd_format(cp_track, position=songpos)
if start == -1:
end = None
else:
# Hot code path: Fetch a single track, while avoiding deep-copying
# the entire playlist
cp_track = context.backend.current_playlist.get(cpid=songpos).get()
return tracks_to_mpd_format([cp_track], 0, 1)
else: else:
if start is None: if start is None:
start = 0 start = 0
@ -267,8 +258,8 @@ def playlistinfo(context, songpos=None,
end = int(end) end = int(end)
if end > context.backend.current_playlist.length.get(): if end > context.backend.current_playlist.length.get():
end = None end = None
cp_tracks = context.backend.current_playlist.cp_tracks.get() cp_tracks = context.backend.current_playlist.cp_tracks.get()
return tracks_to_mpd_format(cp_tracks, start, end) return tracks_to_mpd_format(cp_tracks, start, end)
@handle_request(r'^playlistsearch "(?P<tag>[^"]+)" "(?P<needle>[^"]+)"$') @handle_request(r'^playlistsearch "(?P<tag>[^"]+)" "(?P<needle>[^"]+)"$')
@handle_request(r'^playlistsearch (?P<tag>\S+) "(?P<needle>[^"]+)"$') @handle_request(r'^playlistsearch (?P<tag>\S+) "(?P<needle>[^"]+)"$')

View File

@ -271,11 +271,17 @@ class CurrentPlaylistHandlerTest(protocol.BaseTestCase):
self.sendRequest(u'playlistinfo') self.sendRequest(u'playlistinfo')
self.assertInResponse(u'Title: a') self.assertInResponse(u'Title: a')
self.assertInResponse(u'Pos: 0')
self.assertInResponse(u'Title: b') self.assertInResponse(u'Title: b')
self.assertInResponse(u'Pos: 1')
self.assertInResponse(u'Title: c') self.assertInResponse(u'Title: c')
self.assertInResponse(u'Pos: 2')
self.assertInResponse(u'Title: d') self.assertInResponse(u'Title: d')
self.assertInResponse(u'Pos: 3')
self.assertInResponse(u'Title: e') self.assertInResponse(u'Title: e')
self.assertInResponse(u'Pos: 4')
self.assertInResponse(u'Title: f') self.assertInResponse(u'Title: f')
self.assertInResponse(u'Pos: 5')
self.assertInResponse(u'OK') self.assertInResponse(u'OK')
def test_playlistinfo_with_songpos(self): def test_playlistinfo_with_songpos(self):
@ -286,11 +292,17 @@ class CurrentPlaylistHandlerTest(protocol.BaseTestCase):
self.sendRequest(u'playlistinfo "4"') self.sendRequest(u'playlistinfo "4"')
self.assertNotInResponse(u'Title: a') self.assertNotInResponse(u'Title: a')
self.assertNotInResponse(u'Pos: 0')
self.assertNotInResponse(u'Title: b') self.assertNotInResponse(u'Title: b')
self.assertNotInResponse(u'Pos: 1')
self.assertNotInResponse(u'Title: c') self.assertNotInResponse(u'Title: c')
self.assertNotInResponse(u'Pos: 2')
self.assertNotInResponse(u'Title: d') self.assertNotInResponse(u'Title: d')
self.assertNotInResponse(u'Pos: 3')
self.assertInResponse(u'Title: e') self.assertInResponse(u'Title: e')
self.assertInResponse(u'Pos: 4')
self.assertNotInResponse(u'Title: f') self.assertNotInResponse(u'Title: f')
self.assertNotInResponse(u'Pos: 5')
self.assertInResponse(u'OK') self.assertInResponse(u'OK')
def test_playlistinfo_with_negative_songpos_same_as_playlistinfo(self): def test_playlistinfo_with_negative_songpos_same_as_playlistinfo(self):
@ -306,11 +318,17 @@ class CurrentPlaylistHandlerTest(protocol.BaseTestCase):
self.sendRequest(u'playlistinfo "2:"') self.sendRequest(u'playlistinfo "2:"')
self.assertNotInResponse(u'Title: a') self.assertNotInResponse(u'Title: a')
self.assertNotInResponse(u'Pos: 0')
self.assertNotInResponse(u'Title: b') self.assertNotInResponse(u'Title: b')
self.assertNotInResponse(u'Pos: 1')
self.assertInResponse(u'Title: c') self.assertInResponse(u'Title: c')
self.assertInResponse(u'Pos: 2')
self.assertInResponse(u'Title: d') self.assertInResponse(u'Title: d')
self.assertInResponse(u'Pos: 3')
self.assertInResponse(u'Title: e') self.assertInResponse(u'Title: e')
self.assertInResponse(u'Pos: 4')
self.assertInResponse(u'Title: f') self.assertInResponse(u'Title: f')
self.assertInResponse(u'Pos: 5')
self.assertInResponse(u'OK') self.assertInResponse(u'OK')
def test_playlistinfo_with_closed_range(self): def test_playlistinfo_with_closed_range(self):