Skip to content

Commit

Permalink
Introduce MetadataItem.suspend_metadata_images_update context manager
Browse files Browse the repository at this point in the history
It just enable/disable update_metadata_images_enabled
  • Loading branch information
zas committed May 27, 2024
1 parent 5e24e80 commit e25df4c
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 78 deletions.
86 changes: 41 additions & 45 deletions picard/album.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,40 +468,39 @@ def _load_track(node, mm, artists, extra_metadata):
self._tracks_loaded = True

def _finalize_loading_album(self):
self.enable_update_metadata_images(False)
for track in self._new_tracks:
track.orig_metadata.copy(track.metadata)
track.metadata_images_changed.connect(self.update_metadata_images)

# Prepare parser for user's script
for script in iter_active_tagging_scripts():
parser = ScriptParser()
with self.suspend_metadata_images_update:
for track in self._new_tracks:
# Run tagger script for each track
track.orig_metadata.copy(track.metadata)
track.metadata_images_changed.connect(self.update_metadata_images)

# Prepare parser for user's script
for script in iter_active_tagging_scripts():
parser = ScriptParser()
for track in self._new_tracks:
# Run tagger script for each track
try:
parser.eval(script.content, track.metadata)
except ScriptError:
log.exception("Failed to run tagger script %s on track", script.name)
track.metadata.strip_whitespace()
track.scripted_metadata.update(track.metadata)
# Run tagger script for the album itself
try:
parser.eval(script.content, track.metadata)
parser.eval(script.content, self._new_metadata)
except ScriptError:
log.exception("Failed to run tagger script %s on track", script.name)
track.metadata.strip_whitespace()
track.scripted_metadata.update(track.metadata)
# Run tagger script for the album itself
try:
parser.eval(script.content, self._new_metadata)
except ScriptError:
log.exception("Failed to run tagger script %s on album", script.name)
self._new_metadata.strip_whitespace()

unmatched_files = [file for track in self.tracks for file in track.files]
self.metadata = self._new_metadata
self.orig_metadata.copy(self.metadata)
self.orig_metadata.images.clear()
self.tracks = self._new_tracks
del self._new_metadata
del self._new_tracks
self.loaded = True
self.status = AlbumStatus.LOADED
self.match_files(unmatched_files + self.unmatched_files.files)
self.enable_update_metadata_images(True)
log.exception("Failed to run tagger script %s on album", script.name)
self._new_metadata.strip_whitespace()

unmatched_files = [file for track in self.tracks for file in track.files]
self.metadata = self._new_metadata
self.orig_metadata.copy(self.metadata)
self.orig_metadata.images.clear()
self.tracks = self._new_tracks
del self._new_metadata
del self._new_tracks
self.loaded = True
self.status = AlbumStatus.LOADED
self.match_files(unmatched_files + self.unmatched_files.files)
self.update_metadata_images()
self.update()
self.tagger.window.set_statusbar_message(
Expand Down Expand Up @@ -851,13 +850,11 @@ def update_metadata_images(self):
self.metadata_images_changed.emit()

def keep_original_images(self):
self.enable_update_metadata_images(False)
for track in self.tracks:
track.keep_original_images()
for file in list(self.unmatched_files.files):
file.keep_original_images()
self.enable_update_metadata_images(True)
self.update_metadata_images()
with self.suspend_metadata_images_update:
for track in self.tracks:
track.keep_original_images()
for file in list(self.unmatched_files.files):
file.keep_original_images()

def children_metadata_items(self):
for track in self.tracks:
Expand All @@ -875,15 +872,14 @@ def __init__(self):

def update(self, update_tracks=True, update_selection=True):
config = get_config()
self.enable_update_metadata_images(False)
old_album_title = self.metadata['album']
self.metadata['album'] = config.setting['nat_name']
for track in self.tracks:
if old_album_title == track.metadata['album']:
track.metadata['album'] = self.metadata['album']
for file in track.files:
track.update_file_metadata(file)
self.enable_update_metadata_images(True)
with self.suspend_metadata_images_update:
for track in self.tracks:
if old_album_title == track.metadata['album']:
track.metadata['album'] = self.metadata['album']
for file in track.files:
track.update_file_metadata(file)
super().update(update_tracks=update_tracks, update_selection=update_selection)

def _finalize_loading(self, error):
Expand Down
18 changes: 12 additions & 6 deletions picard/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

from functools import partial

from PyQt6 import QtCore

from picard import log
from picard.i18n import ngettext
from picard.metadata import Metadata
from picard.util import IgnoreUpdatesContext


class Item(object):
Expand Down Expand Up @@ -164,9 +167,13 @@ def __init__(self):
super().__init__()
self.metadata = Metadata()
self.orig_metadata = Metadata()
self.update_metadata_images_enabled = True
self.enable_update_metadata_images(True)
self.update_children_metadata_attrs = {}
self.iter_children_items_metadata_ignore_attrs = {}
self.suspend_metadata_images_update = IgnoreUpdatesContext(
on_first_enter=partial(self.enable_update_metadata_images, False),
on_last_exit=partial(self.enable_update_metadata_images, True),
)

def enable_update_metadata_images(self, enabled):
self.update_metadata_images_enabled = enabled
Expand All @@ -177,11 +184,10 @@ def update_metadata_images(self):
self.metadata_images_changed.emit()

def keep_original_images(self):
self.enable_update_metadata_images(False)
for file in list(self.files):
if file.can_show_coverart:
file.keep_original_images()
self.enable_update_metadata_images(True)
with self.suspend_metadata_images_update:
for file in list(self.files):
if file.can_show_coverart:
file.keep_original_images()
self.update_metadata_images()

def children_metadata_items(self):
Expand Down
52 changes: 25 additions & 27 deletions picard/ui/coverartbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,37 +567,35 @@ def _try_load_remote_image(self, url, data):

if isinstance(self.item, Album):
album = self.item
album.enable_update_metadata_images(False)
set_image(album, coverartimage)
for track in album.tracks:
track.enable_update_metadata_images(False)
set_image(track, coverartimage)
for file in album.iterfiles():
set_image(file, coverartimage)
file.update(signal=False)
for track in album.tracks:
track.enable_update_metadata_images(True)
album.enable_update_metadata_images(True)
with album.suspend_metadata_images_update:
set_image(album, coverartimage)
for track in album.tracks:
track.enable_update_metadata_images(False)
set_image(track, coverartimage)
for file in album.iterfiles():
set_image(file, coverartimage)
file.update(signal=False)
for track in album.tracks:
track.enable_update_metadata_images(True)
album.update(update_tracks=False)
elif isinstance(self.item, FileListItem):
parents = set()
filelist = self.item
filelist.enable_update_metadata_images(False)
set_image(filelist, coverartimage)
for file in filelist.iterfiles():
for parent in iter_file_parents(file):
parent.enable_update_metadata_images(False)
parents.add(parent)
set_image(file, coverartimage)
file.update(signal=False)
for parent in parents:
set_image(parent, coverartimage)
parent.enable_update_metadata_images(True)
if isinstance(parent, Album):
parent.update(update_tracks=False)
else:
parent.update()
filelist.enable_update_metadata_images(True)
with filelist.suspend_metadata_images_update:
set_image(filelist, coverartimage)
for file in filelist.iterfiles():
for parent in iter_file_parents(file):
parent.enable_update_metadata_images(False)
parents.add(parent)
set_image(file, coverartimage)
file.update(signal=False)
for parent in parents:
set_image(parent, coverartimage)
parent.enable_update_metadata_images(True)
if isinstance(parent, Album):
parent.update(update_tracks=False)
else:
parent.update()
filelist.update()
elif isinstance(self.item, File):
file = self.item
Expand Down

0 comments on commit e25df4c

Please sign in to comment.