persist set_fields to media files

This commit is contained in:
Bert Besser 2021-05-06 19:55:12 +02:00
parent 2cff7e8229
commit 9cbbc35a95
4 changed files with 42 additions and 22 deletions

View file

@ -572,18 +572,22 @@ class ImportTask(BaseImportTask):
util.prune_dirs(os.path.dirname(item.path), util.prune_dirs(os.path.dirname(item.path),
lib.directory) lib.directory)
def set_fields(self): def set_fields(self, lib):
"""Sets the fields given at CLI or configuration to the specified """Sets the fields given at CLI or configuration to the specified
values. values, for both the album and all its items.
""" """
for field, view in config['import']['set_fields'].items(): with lib.transaction():
value = view.get() for field, view in config['import']['set_fields'].items():
log.debug(u'Set field {1}={2} for {0}', value = view.get()
displayable_path(self.paths), log.debug(u'Set field {1}={2} for {0}',
field, displayable_path(self.paths),
value) field,
self.album[field] = value value)
self.album.store() self.album[field] = value
for item in self.imported_items():
item[field] = value
item.store()
self.album.store()
def finalize(self, session): def finalize(self, session):
"""Save progress, clean up files, and emit plugin event. """Save progress, clean up files, and emit plugin event.
@ -946,18 +950,19 @@ class SingletonImportTask(ImportTask):
def reload(self): def reload(self):
self.item.load() self.item.load()
def set_fields(self): def set_fields(self, lib):
"""Sets the fields given at CLI or configuration to the specified """Sets the fields given at CLI or configuration to the specified
values. values, for the singleton item.
""" """
for field, view in config['import']['set_fields'].items(): with lib.transaction():
value = view.get() for field, view in config['import']['set_fields'].items():
log.debug(u'Set field {1}={2} for {0}', value = view.get()
displayable_path(self.paths), log.debug(u'Set field {1}={2} for {0}',
field, displayable_path(self.paths),
value) field,
self.item[field] = value value)
self.item.store() self.item[field] = value
self.item.store()
# FIXME The inheritance relationships are inverted. This is why there # FIXME The inheritance relationships are inverted. This is why there
@ -1510,7 +1515,7 @@ def apply_choice(session, task):
# because then the ``ImportTask`` won't have an `album` for which # because then the ``ImportTask`` won't have an `album` for which
# it can set the fields. # it can set the fields.
if config['import']['set_fields']: if config['import']['set_fields']:
task.set_fields() task.set_fields(session.lib)
@pipeline.mutator_stage @pipeline.mutator_stage

View file

@ -216,6 +216,8 @@ Other new things:
``check_on_import`` config option. ``check_on_import`` config option.
* :doc:`/plugins/export`: big speedups when `--include-keys` option is used * :doc:`/plugins/export`: big speedups when `--include-keys` option is used
Thanks to :user:`ssssam`. Thanks to :user:`ssssam`.
* The `importer` persists all fields set using :ref:`set_fields` to the
mediafiles of all imported tracks.
Fixes: Fixes:

View file

@ -683,6 +683,9 @@ Here's an example::
Other field/value pairs supplied via the ``--set`` option on the command-line Other field/value pairs supplied via the ``--set`` option on the command-line
override any settings here for fields with the same name. override any settings here for fields with the same name.
Fields are set on both the album and each individual track of the album.
Fields are persisted to the media files of each track.
Default: ``{}`` (empty). Default: ``{}`` (empty).
.. _musicbrainz-config: .. _musicbrainz-config:

View file

@ -734,10 +734,12 @@ class ImportTest(_common.TestCase, ImportHelper):
def test_set_fields(self): def test_set_fields(self):
genre = u"\U0001F3B7 Jazz" genre = u"\U0001F3B7 Jazz"
collection = u"To Listen" collection = u"To Listen"
comments = u"managed by beets"
config['import']['set_fields'] = { config['import']['set_fields'] = {
u'collection': collection, u'collection': collection,
u'genre': genre u'genre': genre,
u'comments': comments
} }
# As-is album import. # As-is album import.
@ -749,6 +751,10 @@ class ImportTest(_common.TestCase, ImportHelper):
album.load() # TODO: Not sure this is necessary. album.load() # TODO: Not sure this is necessary.
self.assertEqual(album.genre, genre) self.assertEqual(album.genre, genre)
self.assertEqual(album.collection, collection) self.assertEqual(album.collection, collection)
for item in album.items():
self.assertEqual(item.genre, genre)
self.assertEqual(item.collection, collection)
self.assertEqual(item.comments, comments)
# Remove album from library to test again with APPLY choice. # Remove album from library to test again with APPLY choice.
album.remove() album.remove()
@ -762,6 +768,10 @@ class ImportTest(_common.TestCase, ImportHelper):
album.load() album.load()
self.assertEqual(album.genre, genre) self.assertEqual(album.genre, genre)
self.assertEqual(album.collection, collection) self.assertEqual(album.collection, collection)
for item in album.items():
self.assertEqual(item.genre, genre)
self.assertEqual(item.collection, collection)
self.assertEqual(item.comments, comments)
class ImportTracksTest(_common.TestCase, ImportHelper): class ImportTracksTest(_common.TestCase, ImportHelper):