Skip to content

Commit

Permalink
Define unique_list and dedupe
Browse files Browse the repository at this point in the history
  • Loading branch information
snejus committed Dec 15, 2024
1 parent a091c2e commit 3b0c477
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
28 changes: 14 additions & 14 deletions beets/autotag/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
from beets.library import Album, Item, LibModel

# Parts of external interface.
from beets.util import unique_list

from .hooks import AlbumInfo, AlbumMatch, Distance, TrackInfo, TrackMatch
from .match import (
Proposal,
Expand Down Expand Up @@ -143,24 +145,22 @@ def correct_list_fields(m: LibModel) -> None:
Note: :class:`Album` model does not have ``mb_artistids`` and
``mb_albumartistids`` fields therefore we need to check for their presence.
"""
if atype := m.albumtype:
m.albumtypes = list(dict.fromkeys([atype, *m.albumtypes]))
elif atypes := m.albumtypes:
m.albumtype = atypes[0]

def ensure_first_value(single_field: str, list_field: str) -> None:
"""Ensure the first ``list_field`` item is equal to ``single_field``."""
single_val, list_val = getattr(m, single_field), getattr(m, list_field)
if single_val:
setattr(m, list_field, unique_list([single_val, *list_val]))
elif list_val:
setattr(m, single_field, list_val[0])

ensure_first_value("albumtype", "albumtypes")

if hasattr(m, "mb_artistids"):
if aid := m.mb_artistid:
m.mb_artistids = list(dict.fromkeys([aid, *m.mb_artistids]))
elif aids := m.mb_artistids:
m.mb_artistid = aids[0]
ensure_first_value("mb_artistid", "mb_artistids")

if hasattr(m, "mb_albumartistids"):
if aaid := m.mb_albumartistid:
m.mb_albumartistids = list(
dict.fromkeys([aaid, *m.mb_albumartistids])
)
elif aaids := m.mb_albumartistids:
m.mb_albumartistid = aaids[0]
ensure_first_value("mb_albumartistid", "mb_albumartistids")


def apply_item_metadata(item: Item, track_info: TrackInfo):
Expand Down
6 changes: 6 additions & 0 deletions beets/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
Any,
AnyStr,
Callable,
Iterable,
NamedTuple,
TypeVar,
Union,
Expand Down Expand Up @@ -1126,3 +1127,8 @@ def get_temp_filename(

_, filename = tempfile.mkstemp(dir=tempdir, prefix=prefix, suffix=suffix)
return bytestring_path(filename)


def unique_list(elements: Iterable[T]) -> list[T]:
"""Return a list with unique elements in the original order."""
return list(dict.fromkeys(elements))

0 comments on commit 3b0c477

Please sign in to comment.