Split _current_playlist_delete into a method for songpos and a method for ranges

This commit is contained in:
Stein Magnus Jodal 2010-02-28 17:41:10 +01:00
parent 002a0c99e7
commit 9846f7a738
2 changed files with 28 additions and 19 deletions

View File

@ -248,9 +248,8 @@ class MpdHandler(object):
self.backend.current_playlist.add(track, at_position=songpos)
return ('Id', track.id)
@handle_pattern(r'^delete "(?P<songpos>\d+)"$')
@handle_pattern(r'^delete "(?P<start>\d+):(?P<end>\d+)*"$')
def _current_playlist_delete(self, songpos=None, start=None, end=None):
def _current_playlist_delete_range(self, start, end=None):
"""
*musicpd.org, current playlist section:*
@ -258,22 +257,24 @@ class MpdHandler(object):
Deletes a song from the playlist.
"""
start = int(start)
if end is not None:
end = int(end)
else:
end = self.backend.current_playlist.playlist.length
tracks = self.backend.current_playlist.playlist.tracks[start:end]
if not tracks:
raise MpdAckError(u'Position out of bounds')
for track in tracks:
self.backend.current_playlist.remove(track)
@handle_pattern(r'^delete "(?P<songpos>\d+)"$')
def _current_playlist_delete_songpos(self, songpos):
"""See :meth:`_current_playlist_delete_range`"""
try:
tracks = []
if songpos is not None:
songpos = int(songpos)
tracks = [
self.backend.current_playlist.playlist.tracks[songpos]]
elif start is not None:
start = int(start)
if end is not None:
end = int(end)
else:
end = self.backend.current_playlist.playlist.length
tracks = self.backend.current_playlist.playlist.tracks[
start:end]
for track in tracks:
self.backend.current_playlist.remove(track)
songpos = int(songpos)
track = self.backend.current_playlist.playlist.tracks[songpos]
self.backend.current_playlist.remove(track)
except IndexError, e:
raise MpdAckError(u'Position out of bounds')

View File

@ -544,6 +544,14 @@ class CurrentPlaylistHandlerTest(unittest.TestCase):
self.assertEquals(self.b.current_playlist.playlist.length, 4)
self.assert_(u'OK' in result)
def test_delete_songpos_out_of_bounds(self):
self.b.current_playlist.playlist = Playlist(
tracks=[Track(), Track(), Track(), Track(), Track()])
self.assertEquals(self.b.current_playlist.playlist.length, 5)
result = self.h.handle_request(u'delete "5"')
self.assertEquals(self.b.current_playlist.playlist.length, 5)
self.assert_(u'ACK Position out of bounds' in result)
def test_delete_open_range(self):
self.b.current_playlist.playlist = Playlist(
tracks=[Track(), Track(), Track(), Track(), Track()])
@ -560,11 +568,11 @@ class CurrentPlaylistHandlerTest(unittest.TestCase):
self.assertEquals(self.b.current_playlist.playlist.length, 3)
self.assert_(u'OK' in result)
def test_delete_out_of_range(self):
def test_delete_range_out_of_bounds(self):
self.b.current_playlist.playlist = Playlist(
tracks=[Track(), Track(), Track(), Track(), Track()])
self.assertEquals(self.b.current_playlist.playlist.length, 5)
result = self.h.handle_request(u'delete "5"')
result = self.h.handle_request(u'delete "5:7"')
self.assertEquals(self.b.current_playlist.playlist.length, 5)
self.assert_(u'ACK Position out of bounds' in result)