Debian Codegraph @

beetsplug/albumtypes.py

raw
1 """Adds an album template field for formatted album types."""
2
3 from __future__ import annotations
4
5 from typing import TYPE_CHECKING
6
7 from beets.plugins import BeetsPlugin
8
9 from .musicbrainz import VARIOUS_ARTISTS_ID
10
11 if TYPE_CHECKING:
12 from beets.library import Album
13
14
15 class AlbumTypesPlugin(BeetsPlugin):
16 """Adds an album template field for formatted album types."""
17
18 def __init__(self) -> None:
19 """Init AlbumTypesPlugin."""
20 super().__init__()
21 self.album_template_fields["atypes"] = self._atypes
22 self.config.add(
23 {
24 "types": [
25 ("ep", "EP"),
26 ("single", "Single"),
27 ("soundtrack", "OST"),
28 ("live", "Live"),
29 ("compilation", "Anthology"),
30 ("remix", "Remix"),
31 ],
32 "ignore_va": ["compilation"],
33 "bracket": "[]",
34 }
35 )
36
37 def _atypes(self, item: Album) -> str:
38 """Returns a formatted string based on album's types."""
39 types = self.config["types"].as_pairs()
40 ignore_va = self.config["ignore_va"].as_str_seq()
41 bracket = self.config["bracket"].as_str()
42
43 # Assign a left and right bracket or leave blank if argument is empty.
44 if len(bracket) == 2:
45 bracket_l = bracket[0]
46 bracket_r = bracket[1]
47 else:
48 bracket_l = ""
49 bracket_r = ""
50
51 res = ""
52 albumtypes = item.albumtypes
53 is_va = item.mb_albumartistid == VARIOUS_ARTISTS_ID
54 for type_ in types:
55 if type_[0] in albumtypes and type_[1]:
56 if not is_va or (type_[0] not in ignore_va and is_va):
57 res += f"{bracket_l}{type_[1]}{bracket_r}"
58
59 return res