Skip to content

Commit

Permalink
fix bug in results reporting on TagWriter (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
geo-martino authored Apr 1, 2024
1 parent 8ee0a31 commit 70e8e87
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 7 deletions.
15 changes: 10 additions & 5 deletions musify/libraries/local/track/tags/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Implements all functionality pertaining to writing and deleting metadata/tags/properties for a :py:class:`LocalTrack`.
"""
from abc import ABCMeta, abstractmethod
from collections.abc import Mapping, Collection
from collections.abc import Mapping, Collection, Callable
from dataclasses import dataclass
from typing import Any

Expand Down Expand Up @@ -115,8 +115,11 @@ def write(

updated = {}
for tag in tags:
method = getattr(self, f"write_{tag.name.lower()}")
method: Callable[Any, Mapping[Tags, int] | int | None] = getattr(self, f"write_{tag.name.lower()}")
result = method(source=source, target=target, replace=replace, dry_run=dry_run)
if result is None:
continue

if isinstance(result, Mapping):
updated |= result
else:
Expand Down Expand Up @@ -329,7 +332,9 @@ def _write_genres(self, track: Track, dry_run: bool = True) -> bool:
"""
return self.write_tag(next(iter(self.tag_map.genres), None), track.genres, dry_run)

def write_date(self, source: Track, target: Track, replace: bool = False, dry_run: bool = True) -> dict[Tags, int]:
def write_date(
self, source: Track, target: Track, replace: bool = False, dry_run: bool = True
) -> dict[Tags, int] | None:
"""
Write the track date and/or year/month/day tags to file if appropriate related conditions are met.
Expand All @@ -348,7 +353,7 @@ def write_date(self, source: Track, target: Track, replace: bool = False, dry_ru
}

if not any(conditionals):
return {}
return

date, year, month, day = self._write_date(track=target, dry_run=dry_run)

Expand All @@ -363,7 +368,7 @@ def write_date(self, source: Track, target: Track, replace: bool = False, dry_ru
if day:
updated[Tags.DAY] = condition

return updated
return updated if updated else None

def _write_date(self, track: Track, dry_run: bool = True) -> tuple[bool, bool, bool, bool]:
"""
Expand Down
2 changes: 1 addition & 1 deletion musify/libraries/local/track/track.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ def save(self, tags: UnitIterable[Tags] = Tags.ALL, replace: bool = False, dry_r
current = deepcopy(self)
current.refresh()

return self._writer.write(source=current, target=self, replace=replace, dry_run=dry_run)
return self._writer.write(source=current, target=self, tags=tags, replace=replace, dry_run=dry_run)

def delete_tags(self, tags: UnitIterable[Tags] = (), dry_run: bool = True) -> SyncResultTrack:
"""
Expand Down
1 change: 0 additions & 1 deletion musify/libraries/remote/spotify/object.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,6 @@ def load(
matched, missing = cls._merge_items_to_response(items=items, response=response[item_key][api.items_key])

if missing:
print(missing)
items_missing = cls._get_items(items=missing, api=api, use_cache=use_cache)
cls._merge_items_to_response(items=items_missing, response=response[item_key][api.items_key], skip=matched)

Expand Down
38 changes: 38 additions & 0 deletions tests/libraries/local/track/test_track.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,44 @@ def test_update_tags_with_replace(self, track: LocalTrack):
assert track_update_replace.image_links == track_update.image_links
assert track_update_replace.has_image == track_update.has_image

def test_update_tags_results(self, track: LocalTrack):
track_original = copy(track)

track.title = "new title"
track.album = "new album artist"
track.track_number += 2
track.genres = ["Big Band", "Swing"]
track.year += 10
track.bpm += 10
track.key = "F#"
track.disc_number += 5

result = track.save(tags=LocalTrackField.ALL, replace=True, dry_run=False)
assert result.saved

expected_tags = {
LocalTrackField.TITLE,
LocalTrackField.ALBUM,
LocalTrackField.TRACK,
LocalTrackField.GENRES,
(LocalTrackField.DATE if track.tag_map.date else LocalTrackField.YEAR),
LocalTrackField.BPM,
LocalTrackField.KEY,
LocalTrackField.DISC,
}
assert set(result.updated) == expected_tags

self.assert_track_tags_equal(track, deepcopy(track_original))

track.artist = "new artist"
track.album_artist = "new various"
track.compilation = not track.compilation

tags_to_update = {LocalTrackField.ARTIST, LocalTrackField.COMPILATION}
result = track.save(tags=tags_to_update, replace=True, dry_run=False)
assert result.saved
assert set(result.updated) == tags_to_update

@staticmethod
def get_update_image_test_track(track: LocalTrack) -> tuple[LocalTrack, LocalTrack]:
"""Load track and modify its tags for update tags tests"""
Expand Down

0 comments on commit 70e8e87

Please sign in to comment.