Extract tracks_to_mpd_format from playlist_to_mpd_format

This commit is contained in:
Stein Magnus Jodal 2010-07-01 01:08:01 +02:00
parent d65c76c528
commit 454d304953

View File

@ -40,14 +40,14 @@ def track_artists_to_mpd_format(track):
artists.sort(key=lambda a: a.name)
return u', '.join([a.name for a in artists])
def playlist_to_mpd_format(playlist, start=0, end=None, search_result=False):
def tracks_to_mpd_format(tracks, start=0, end=None, search_result=False):
"""
Format playlist for output to MPD client.
Format list of tracks for output to MPD client.
Optionally limit output to the slice ``[start:end]`` of the playlist.
Optionally limit output to the slice ``[start:end]`` of the list.
:param playlist: the playlist
:type playlist: :class:`mopidy.models.Playlist`
:param tracks: the tracks
:type tracks: list of :class:`mopidy.models.Track`
:param start: position of first track to include in output
:type start: int (positive or negative)
:param end: position after last track to include in output
@ -55,17 +55,25 @@ def playlist_to_mpd_format(playlist, start=0, end=None, search_result=False):
:rtype: list of lists of two-tuples
"""
if start < 0:
range_start = playlist.length + start
range_start = len(tracks) + start
else:
range_start = start
if end is not None and end < 0:
range_end = playlist.length - end
range_end = len(tracks) - end
elif end is not None and end >= 0:
range_end = end
else:
range_end = playlist.length
tracks = []
for track, position in zip(playlist.tracks[start:end],
range_end = len(tracks)
result = []
for track, position in zip(tracks[start:end],
range(range_start, range_end)):
tracks.append(track.mpd_format(position, search_result))
return tracks
result.append(track.mpd_format(position, search_result))
return result
def playlist_to_mpd_format(playlist, *args, **kwargs):
"""
Format playlist for output to MPD client.
Arguments as for :func:`tracks_to_mpd_format`, except the first one.
"""
return tracks_to_mpd_format(playlist.tracks, *args, **kwargs)