Skip to content

Commit

Permalink
Merge pull request #45 from splintah/feat/multiple_tags
Browse files Browse the repository at this point in the history
Add support for multiple tags for multiple values (fixes #44)
  • Loading branch information
jodal authored Sep 11, 2021
2 parents 408c7c0 + 380e52b commit 4f44dfe
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
37 changes: 27 additions & 10 deletions mopidy_mpd/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def track_to_mpd_format(track, position=None, stream_title=None):
result = [
("file", track.uri),
("Time", track.length and (track.length // 1000) or 0),
("Artist", concat_multi_values(track.artists, "name")),
*multi_tag_list(track.artists, "name", "Artist"),
("Album", track.album and track.album.name or ""),
]

Expand All @@ -69,9 +69,8 @@ def track_to_mpd_format(track, position=None, stream_title=None):
result.append(("MUSICBRAINZ_ALBUMID", track.album.musicbrainz_id))

if track.album is not None and track.album.artists:
result.append(
("AlbumArtist", concat_multi_values(track.album.artists, "name"))
)
result += multi_tag_list(track.album.artists, "name", "AlbumArtist")

musicbrainz_ids = concat_multi_values(
track.album.artists, "musicbrainz_id"
)
Expand All @@ -84,14 +83,10 @@ def track_to_mpd_format(track, position=None, stream_title=None):
result.append(("MUSICBRAINZ_ARTISTID", musicbrainz_ids))

if track.composers:
result.append(
("Composer", concat_multi_values(track.composers, "name"))
)
result += multi_tag_list(track.composers, "name", "Composer")

if track.performers:
result.append(
("Performer", concat_multi_values(track.performers, "name"))
)
result += multi_tag_list(track.performers, "name", "Performer")

if track.genre:
result.append(("Genre", track.genre))
Expand Down Expand Up @@ -151,6 +146,28 @@ def concat_multi_values(models, attribute):
)


def multi_tag_list(objects, attribute, tag):
"""
Format multiple objects for output to MPD client in a list with one tag per
value.
:param objects: the model objects
:type objects: array of :class:`mopidy.models.Artist`,
:class:`mopidy.models.Album`, or :class:`mopidy.models.Track`
:param attribute: the attribute to use
:type attribute: string
:param tag: the name of the tag
:type tag: string
:rtype: list of tuples of string and attribute value
"""

return [
(tag, getattr(obj, attribute))
for obj in objects
if getattr(obj, attribute, None) is not None
]


def tracks_to_mpd_format(tracks, start=0, end=None):
"""
Format list of tracks for output to MPD client.
Expand Down
20 changes: 15 additions & 5 deletions tests/test_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,23 @@
class TrackMpdFormatTest(unittest.TestCase):
track = Track(
uri="à uri",
artists=[Artist(name="an artist")],
artists=[Artist(name="an artist"), Artist(name="yet another artist")],
name="a nàme",
album=Album(
name="an album",
num_tracks=13,
artists=[Artist(name="an other artist")],
artists=[
Artist(name="an other artist"),
Artist(name="still another artist"),
],
uri="urischeme:àlbum:12345",
),
track_no=7,
composers=[Artist(name="a composer")],
performers=[Artist(name="a performer")],
composers=[Artist(name="a composer"), Artist(name="another composer")],
performers=[
Artist(name="a performer"),
Artist(name="another performer"),
],
genre="a genre",
date="1977-01-01",
disc_no=1,
Expand Down Expand Up @@ -69,11 +75,15 @@ def test_track_to_mpd_format_for_nonempty_track(self):
assert ("file", "à uri") in result
assert ("Time", 137) in result
assert ("Artist", "an artist") in result
assert ("Artist", "yet another artist") in result
assert ("Title", "a nàme") in result
assert ("Album", "an album") in result
assert ("AlbumArtist", "an other artist") in result
assert ("AlbumArtist", "still another artist") in result
assert ("Composer", "a composer") in result
assert ("Composer", "another composer") in result
assert ("Performer", "a performer") in result
assert ("Performer", "another performer") in result
assert ("Genre", "a genre") in result
assert ("Track", "7/13") in result
assert ("Date", "1977-01-01") in result
Expand All @@ -82,7 +92,7 @@ def test_track_to_mpd_format_for_nonempty_track(self):
assert ("Id", 122) in result
assert ("X-AlbumUri", "urischeme:àlbum:12345") in result
assert ("Comment", "a comment") not in result
assert len(result) == 15
assert len(result) == 19

def test_track_to_mpd_format_with_last_modified(self):
track = self.track.replace(last_modified=995303899000)
Expand Down

0 comments on commit 4f44dfe

Please sign in to comment.