From 442d9d4b1edcdbe860cf4f99ed0613a2b28ee3b8 Mon Sep 17 00:00:00 2001 From: Thomas Adamcik Date: Thu, 23 Jan 2014 00:40:46 +0100 Subject: [PATCH] mpd: Convert almost all of protocol.current_playlist --- mopidy/mpd/protocol/current_playlist.py | 141 ++++++++++-------------- 1 file changed, 57 insertions(+), 84 deletions(-) diff --git a/mopidy/mpd/protocol/current_playlist.py b/mopidy/mpd/protocol/current_playlist.py index de8721d3..41a4782b 100644 --- a/mopidy/mpd/protocol/current_playlist.py +++ b/mopidy/mpd/protocol/current_playlist.py @@ -1,12 +1,9 @@ from __future__ import unicode_literals -from mopidy.mpd import translator -from mopidy.mpd.exceptions import ( - MpdArgError, MpdNoExistError, MpdNotImplemented) -from mopidy.mpd.protocol import handle_request +from mopidy.mpd import exceptions, protocol, translator -@handle_request(r'add\ "(?P[^"]*)"$') +@protocol.commands.add('add') def add(context, uri): """ *musicpd.org, current playlist section:* @@ -29,8 +26,7 @@ def add(context, uri): try: uri = context.directory_path_to_uri(translator.normalize_path(uri)) - except MpdNoExistError as e: - e.command = 'add' + except exceptions.MpdNoExistError as e: e.message = 'directory or file not found' raise @@ -48,12 +44,12 @@ def add(context, uri): tracks.extend(future.get()) if not tracks: - raise MpdNoExistError('directory or file not found') + raise exceptions.MpdNoExistError('directory or file not found') context.core.tracklist.add(tracks=tracks) -@handle_request(r'addid\ "(?P[^"]*)"(\ "(?P\d+)")*$') +@protocol.commands.add('addid', songpos=protocol.integer) def addid(context, uri, songpos=None): """ *musicpd.org, current playlist section:* @@ -72,20 +68,19 @@ def addid(context, uri, songpos=None): - ``addid ""`` should return an error. """ + # TODO: check that songpos is positive if not uri: - raise MpdNoExistError('No such song') - if songpos is not None: - songpos = int(songpos) - if songpos and songpos > context.core.tracklist.length.get(): - raise MpdArgError('Bad song index') + raise exceptions.MpdNoExistError('No such song') + if songpos is not None and songpos > context.core.tracklist.length.get(): + raise exceptions.MpdArgError('Bad song index') tl_tracks = context.core.tracklist.add(uri=uri, at_position=songpos).get() if not tl_tracks: - raise MpdNoExistError('No such song') + raise exceptions.MpdNoExistError('No such song') return ('Id', tl_tracks[0].tlid) -@handle_request(r'delete\ "(?P\d+):(?P\d+)*"$') -def delete_range(context, start, end=None): +@protocol.commands.add('delete', position=protocol.position_or_range) +def delete(context, position): """ *musicpd.org, current playlist section:* @@ -93,31 +88,18 @@ def delete_range(context, start, end=None): Deletes a song from the playlist. """ - start = int(start) - if end is not None: - end = int(end) - else: + start = position.start + end = position.stop + if end is None: end = context.core.tracklist.length.get() tl_tracks = context.core.tracklist.slice(start, end).get() if not tl_tracks: - raise MpdArgError('Bad song index', command='delete') + raise exceptions.MpdArgError('Bad song index', command='delete') for (tlid, _) in tl_tracks: context.core.tracklist.remove(tlid=[tlid]) -@handle_request(r'delete\ "(?P\d+)"$') -def delete_songpos(context, songpos): - """See :meth:`delete_range`""" - try: - songpos = int(songpos) - (tlid, _) = context.core.tracklist.slice( - songpos, songpos + 1).get()[0] - context.core.tracklist.remove(tlid=[tlid]) - except IndexError: - raise MpdArgError('Bad song index', command='delete') - - -@handle_request(r'deleteid\ "(?P\d+)"$') +@protocol.commands.add('deleteid', tlid=protocol.integer) def deleteid(context, tlid): """ *musicpd.org, current playlist section:* @@ -126,13 +108,13 @@ def deleteid(context, tlid): Deletes the song ``SONGID`` from the playlist """ - tlid = int(tlid) + # TODO: check that tlid is positive tl_tracks = context.core.tracklist.remove(tlid=[tlid]).get() if not tl_tracks: - raise MpdNoExistError('No such song') + raise exceptions.MpdNoExistError('No such song') -@handle_request(r'clear$') +@protocol.commands.add('clear') def clear(context): """ *musicpd.org, current playlist section:* @@ -144,8 +126,9 @@ def clear(context): context.core.tracklist.clear() -@handle_request(r'move\ "(?P\d+):(?P\d+)*"\ "(?P\d+)"$') -def move_range(context, start, to, end=None): +@protocol.commands.add( + 'move', position=protocol.position_or_range, to=protocol.integer) +def move_range(context, position, to): """ *musicpd.org, current playlist section:* @@ -154,23 +137,15 @@ def move_range(context, start, to, end=None): Moves the song at ``FROM`` or range of songs at ``START:END`` to ``TO`` in the playlist. """ + # TODO: check that to is positive + start = position.start + end = position.stop if end is None: end = context.core.tracklist.length.get() - start = int(start) - end = int(end) - to = int(to) context.core.tracklist.move(start, end, to) -@handle_request(r'move\ "(?P\d+)"\ "(?P\d+)"$') -def move_songpos(context, songpos, to): - """See :meth:`move_range`.""" - songpos = int(songpos) - to = int(to) - context.core.tracklist.move(songpos, songpos + 1, to) - - -@handle_request(r'moveid\ "(?P\d+)"\ "(?P\d+)"$') +@protocol.commands.add('moveid', tlid=protocol.integer, to=protocol.integer) def moveid(context, tlid, to): """ *musicpd.org, current playlist section:* @@ -181,16 +156,15 @@ def moveid(context, tlid, to): the playlist. If ``TO`` is negative, it is relative to the current song in the playlist (if there is one). """ - tlid = int(tlid) - to = int(to) + # TODO: check that tlid and to are positive tl_tracks = context.core.tracklist.filter(tlid=[tlid]).get() if not tl_tracks: - raise MpdNoExistError('No such song') + raise exceptions.MpdNoExistError('No such song') position = context.core.tracklist.index(tl_tracks[0]).get() context.core.tracklist.move(position, position + 1, to) -@handle_request(r'playlist$') +@protocol.commands.add('playlist') def playlist(context): """ *musicpd.org, current playlist section:* @@ -206,7 +180,7 @@ def playlist(context): return playlistinfo(context) -@handle_request(r'playlistfind\ ("?)(?P[^"]+)\1\ "(?P[^"]+)"$') +@protocol.commands.add('playlistfind') def playlistfind(context, tag, needle): """ *musicpd.org, current playlist section:* @@ -225,11 +199,10 @@ def playlistfind(context, tag, needle): return None position = context.core.tracklist.index(tl_tracks[0]).get() return translator.track_to_mpd_format(tl_tracks[0], position=position) - raise MpdNotImplemented # TODO + raise exceptions.MpdNotImplemented # TODO -@handle_request(r'playlistid$') -@handle_request(r'playlistid\ "(?P\d+)"$') +@protocol.commands.add('playlistid', tlid=protocol.integer) def playlistid(context, tlid=None): """ *musicpd.org, current playlist section:* @@ -239,11 +212,11 @@ def playlistid(context, tlid=None): Displays a list of songs in the playlist. ``SONGID`` is optional and specifies a single song to display info for. """ + # TODO: check that tlid is positive if not none if tlid is not None: - tlid = int(tlid) tl_tracks = context.core.tracklist.filter(tlid=[tlid]).get() if not tl_tracks: - raise MpdNoExistError('No such song') + raise exceptions.MpdNoExistError('No such song') position = context.core.tracklist.index(tl_tracks[0]).get() return translator.track_to_mpd_format(tl_tracks[0], position=position) else: @@ -251,9 +224,10 @@ def playlistid(context, tlid=None): context.core.tracklist.tl_tracks.get()) -@handle_request(r'playlistinfo$') -@handle_request(r'playlistinfo\ "(?P-?\d+)"$') -@handle_request(r'playlistinfo\ "(?P\d+):(?P\d+)*"$') +# TODO: convert +@protocol.handle_request(r'playlistinfo$') +@protocol.handle_request(r'playlistinfo\ "(?P-?\d+)"$') +@protocol.handle_request(r'playlistinfo\ "(?P\d+):(?P\d+)*"$') def playlistinfo(context, songpos=None, start=None, end=None): """ *musicpd.org, current playlist section:* @@ -280,7 +254,7 @@ def playlistinfo(context, songpos=None, start=None, end=None): start = 0 start = int(start) if not (0 <= start <= context.core.tracklist.length.get()): - raise MpdArgError('Bad song index') + raise exceptions.MpdArgError('Bad song index') if end is not None: end = int(end) if end > context.core.tracklist.length.get(): @@ -289,7 +263,7 @@ def playlistinfo(context, songpos=None, start=None, end=None): return translator.tracks_to_mpd_format(tl_tracks, start, end) -@handle_request(r'playlistsearch\ ("?)(?P\w+)\1\ "(?P[^"]+)"$') +@protocol.commands.add('playlistsearch') def playlistsearch(context, tag, needle): """ *musicpd.org, current playlist section:* @@ -304,10 +278,10 @@ def playlistsearch(context, tag, needle): - does not add quotes around the tag - uses ``filename`` and ``any`` as tags """ - raise MpdNotImplemented # TODO + raise exceptions.MpdNotImplemented # TODO -@handle_request(r'plchanges\ ("?)(?P-?\d+)\1$') +@protocol.commands.add('plchanges', version=protocol.integer) def plchanges(context, version): """ *musicpd.org, current playlist section:* @@ -329,7 +303,7 @@ def plchanges(context, version): context.core.tracklist.tl_tracks.get()) -@handle_request(r'plchangesposid\ "(?P\d+)"$') +@protocol.commands.add('plchangesposid', version=protocol.integer) def plchangesposid(context, version): """ *musicpd.org, current playlist section:* @@ -353,9 +327,8 @@ def plchangesposid(context, version): return result -@handle_request(r'shuffle$') -@handle_request(r'shuffle\ "(?P\d+):(?P\d+)*"$') -def shuffle(context, start=None, end=None): +@protocol.commands.add('shuffle', position=protocol.position_or_range) +def shuffle(context, position=None): """ *musicpd.org, current playlist section:* @@ -364,14 +337,15 @@ def shuffle(context, start=None, end=None): Shuffles the current playlist. ``START:END`` is optional and specifies a range of songs. """ - if start is not None: - start = int(start) - if end is not None: - end = int(end) + if position is None: + start, end = None, None + else: + start, end = position.start, position.stop context.core.tracklist.shuffle(start, end) -@handle_request(r'swap\ "(?P\d+)"\ "(?P\d+)"$') +@protocol.commands.add( + 'swap', songpos1=protocol.integer, songpos2=protocol.integer) def swap(context, songpos1, songpos2): """ *musicpd.org, current playlist section:* @@ -380,8 +354,7 @@ def swap(context, songpos1, songpos2): Swaps the positions of ``SONG1`` and ``SONG2``. """ - songpos1 = int(songpos1) - songpos2 = int(songpos2) + # TODO: check that songpos is positive tracks = context.core.tracklist.tracks.get() song1 = tracks[songpos1] song2 = tracks[songpos2] @@ -393,7 +366,8 @@ def swap(context, songpos1, songpos2): context.core.tracklist.add(tracks) -@handle_request(r'swapid\ "(?P\d+)"\ "(?P\d+)"$') +@protocol.commands.add( + 'swapid', tlid1=protocol.integer, tlid2=protocol.integer) def swapid(context, tlid1, tlid2): """ *musicpd.org, current playlist section:* @@ -402,12 +376,11 @@ def swapid(context, tlid1, tlid2): Swaps the positions of ``SONG1`` and ``SONG2`` (both song ids). """ - tlid1 = int(tlid1) - tlid2 = int(tlid2) + # TODO: check that tlid is positive tl_tracks1 = context.core.tracklist.filter(tlid=[tlid1]).get() tl_tracks2 = context.core.tracklist.filter(tlid=[tlid2]).get() if not tl_tracks1 or not tl_tracks2: - raise MpdNoExistError('No such song') + raise exceptions.MpdNoExistError('No such song') position1 = context.core.tracklist.index(tl_tracks1[0]).get() position2 = context.core.tracklist.index(tl_tracks2[0]).get() swap(context, position1, position2)