Skip to content

Commit

Permalink
Implement multi-tag genres field
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrokemypie committed Sep 20, 2024
1 parent d3c6296 commit 7ef0726
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
4 changes: 4 additions & 0 deletions beets/autotag/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def __init__(
country: Optional[str] = None,
style: Optional[str] = None,
genre: Optional[str] = None,
genres: Optional[str] = None,
albumstatus: Optional[str] = None,
media: Optional[str] = None,
albumdisambig: Optional[str] = None,
Expand Down Expand Up @@ -152,6 +153,7 @@ def __init__(
self.country = country
self.style = style
self.genre = genre
self.genres = genres
self.albumstatus = albumstatus
self.media = media
self.albumdisambig = albumdisambig
Expand Down Expand Up @@ -221,6 +223,7 @@ def __init__(
bpm: Optional[str] = None,
initial_key: Optional[str] = None,
genre: Optional[str] = None,
genres: Optional[str] = None,
album: Optional[str] = None,
**kwargs,
):
Expand Down Expand Up @@ -255,6 +258,7 @@ def __init__(
self.bpm = bpm
self.initial_key = initial_key
self.genre = genre
self.genres = genres
self.album = album
self.update(kwargs)

Expand Down
3 changes: 3 additions & 0 deletions beets/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,7 @@ class Item(LibModel):
"albumartist_credit": types.STRING,
"albumartists_credit": types.MULTI_VALUE_DSV,
"genre": types.STRING,
"genres": types.MULTI_VALUE_DSV,
"style": types.STRING,
"discogs_albumid": types.INTEGER,
"discogs_artistid": types.INTEGER,
Expand Down Expand Up @@ -1173,6 +1174,7 @@ class Album(LibModel):
"albumartists_credit": types.MULTI_VALUE_DSV,
"album": types.STRING,
"genre": types.STRING,
"genres": types.MULTI_VALUE_DSV,
"style": types.STRING,
"discogs_albumid": types.INTEGER,
"discogs_artistid": types.INTEGER,
Expand Down Expand Up @@ -1229,6 +1231,7 @@ class Album(LibModel):
"albumartists_credit",
"album",
"genre",
"genres",
"style",
"discogs_albumid",
"discogs_artistid",
Expand Down
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ New features:
* :doc:`plugins/autobpm`: Add new configuration option ``beat_track_kwargs``
which enables adjusting keyword arguments supplied to librosa's
``beat_track`` function call.
* New multi-value `genres` tag

Bug fixes:

Expand Down
30 changes: 26 additions & 4 deletions test/test_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -708,13 +708,13 @@ def test_if_def_false_complete(self):
self._assert_dest(b"/base/not_played")

def test_first(self):
self.i.genres = "Pop; Rock; Classical Crossover"
self._setf("%first{$genres}")
self.i.sub_genres = "Pop; Rock; Classical Crossover"
self._setf("%first{$sub_genres}")
self._assert_dest(b"/base/Pop")

def test_first_skip(self):
self.i.genres = "Pop; Rock; Classical Crossover"
self._setf("%first{$genres,1,2}")
self.i.sub_genres = "Pop; Rock; Classical Crossover"
self._setf("%first{$sub_genres,1,2}")
self._assert_dest(b"/base/Classical Crossover")

def test_first_different_sep(self):
Expand Down Expand Up @@ -1306,6 +1306,28 @@ def test_write_date_field(self):
item.write()
assert MediaFile(syspath(item.path)).year == clean_year

def test_write_multi_genres(self):
item = self.add_item_fixture(genre="old genre")
item.write(
tags={"genres": ["g1", "g2"]},
)

# Ensure it reads all genres
self.assertEqual(MediaFile(syspath(item.path)).genres, ["g1", "g2"])

# Ensure reading single genre outputs the first of the genres
self.assertEqual(MediaFile(syspath(item.path)).genre, "g1")

def test_write_multi_genres_both_single_and_multi(self):
item = self.add_item_fixture(genre="old genre 1")
item.write(
tags={"genre": "single genre", "genres": ["multi genre"]},
)

# Ensure the multi takes precedence
self.assertEqual(MediaFile(syspath(item.path)).genre, "multi genre")
self.assertEqual(MediaFile(syspath(item.path)).genres, ["multi genre"])


class ItemReadTest(unittest.TestCase):
def test_unreadable_raise_read_error(self):
Expand Down

0 comments on commit 7ef0726

Please sign in to comment.