Skip to content

Commit e49d7a3

Browse files
committed
lastgenre: Named types Whitelist and CanonTree
1 parent cb4192c commit e49d7a3

1 file changed

Lines changed: 16 additions & 6 deletions

File tree

beetsplug/lastgenre/__init__.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,22 @@
4545
from beets.importer import ImportSession, ImportTask
4646
from beets.library import LibModel
4747

48+
Whitelist = set[str]
49+
"""Set of valid genre names (lowercase). Empty set means all genres allowed."""
50+
51+
CanonTree = list[list[str]]
52+
"""Genre hierarchy as list of paths from general to specific.
53+
Example: [['electronic', 'house'], ['electronic', 'techno']]"""
54+
55+
56+
4857
# Canonicalization tree processing.
4958

5059

5160
def flatten_tree(
5261
elem: dict[Any, Any] | list[Any] | str,
5362
path: list[str],
54-
branches: list[list[str]],
63+
branches: CanonTree,
5564
) -> None:
5665
"""Flatten nested lists/dictionaries into lists of strings
5766
(branches).
@@ -69,7 +78,7 @@ def flatten_tree(
6978
branches.append([*path, str(elem)])
7079

7180

72-
def find_parents(candidate: str, branches: list[list[str]]) -> list[str]:
81+
def find_parents(candidate: str, branches: CanonTree) -> list[str]:
7382
"""Find parents genre of a given genre, ordered from the closest to
7483
the further parent.
7584
"""
@@ -118,10 +127,11 @@ def setup(self) -> None:
118127
self.client = LastFmClient(
119128
self._log, self.config["min_weight"].get(int)
120129
)
121-
self.whitelist = self._load_whitelist()
130+
self.whitelist: Whitelist = self._load_whitelist()
131+
self.c14n_branches: CanonTree
122132
self.c14n_branches, self.canonicalize = self._load_c14n_tree()
123133

124-
def _load_whitelist(self) -> set[str]:
134+
def _load_whitelist(self) -> Whitelist:
125135
"""Load the whitelist from a text file.
126136
127137
Default whitelist is used if config is True, empty string or set to "nothing".
@@ -139,13 +149,13 @@ def _load_whitelist(self) -> set[str]:
139149

140150
return whitelist
141151

142-
def _load_c14n_tree(self) -> tuple[list[list[str]], bool]:
152+
def _load_c14n_tree(self) -> tuple[CanonTree, bool]:
143153
"""Load the canonicalization tree from a YAML file.
144154
145155
Default tree is used if config is True, empty string, set to "nothing"
146156
or if prefer_specific is enabled.
147157
"""
148-
c14n_branches: list[list[str]] = []
158+
c14n_branches: CanonTree = []
149159
c14n_filename = self.config["canonical"].get()
150160
canonicalize = c14n_filename is not False
151161
# Default tree

0 commit comments

Comments
 (0)