Skip to content

Commit e52ca41

Browse files
committed
Changelog and style for #908
Use a defaultdict for more idiomatic collection.
1 parent 9ca6ae6 commit e52ca41

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

beetsplug/mbsync.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from beets import autotag, library, ui, util
2121
from beets.autotag import hooks
2222
from beets import config
23+
from collections import defaultdict
2324

2425
log = logging.getLogger('beets')
2526

@@ -64,30 +65,27 @@ def mbsync_albums(lib, query, move, pretend, write):
6465
log.info(u'Release ID not found: {0}'.format(a.mb_albumid))
6566
continue
6667

67-
# Construct an hash mapping recording MBIDs to their information. A
68-
# release can have recording MBIDs that appear multiple times in the
69-
# same release.
70-
track_index = {}
68+
# Map recording MBIDs to their information. Recordings can appear
69+
# multiple times on a release, so each MBID maps to a list of TrackInfo
70+
# objects.
71+
track_index = defaultdict(list)
7172
for track_info in album_info.tracks:
72-
if track_info.track_id in track_index:
73-
track_index[track_info.track_id].append(track_info)
74-
else:
75-
track_index[track_info.track_id] = [track_info]
73+
track_index[track_info.track_id].append(track_info)
7674

7775
# Construct a track mapping according to MBIDs. This should work
78-
# for albums that have missing or extra tracks. If a mapping is
79-
# ambiguous, the items' disc and track number need to match in order
80-
# for an item to be mapped.
76+
# for albums that have missing or extra tracks. If there are multiple
77+
# copies of a recording, they are disambiguated using their disc and
78+
# track number.
8179
mapping = {}
8280
for item in items:
83-
candidates = track_index.get(item.mb_trackid, [])
81+
candidates = track_index[item.mb_trackid]
8482
if len(candidates) == 1:
8583
mapping[item] = candidates[0]
86-
continue
87-
for c in candidates:
88-
if c.medium_index == item.track and c.medium == item.disc:
89-
mapping[item] = c
90-
break
84+
else:
85+
for c in candidates:
86+
if c.medium_index == item.track and c.medium == item.disc:
87+
mapping[item] = c
88+
break
9189

9290
# Apply.
9391
with lib.transaction():

docs/changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ Little improvements and fixes:
9090
work in ``auto`` mode. Thanks to Harry Khanna.
9191
* The :ref:`write-cmd` command now has a ``--force`` flag. Thanks again to
9292
Harry Khanna.
93+
* :doc:`/plugins/mbsync`: Track alignment now works with albums that have
94+
multiple copies of the same recording. Thanks to Rui Gonçalves.
9395

9496

9597
1.3.6 (May 10, 2014)

0 commit comments

Comments
 (0)