Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

persist set_fields to media files #3927

Merged
merged 5 commits into from
May 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions beets/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,18 +572,24 @@ def remove_duplicates(self, lib):
util.prune_dirs(os.path.dirname(item.path),
lib.directory)

def set_fields(self):
def set_fields(self, lib):
"""Sets the fields given at CLI or configuration to the specified
values.
values, for both the album and all its items.
"""
items = self.imported_items()
for field, view in config['import']['set_fields'].items():
value = view.get()
log.debug(u'Set field {1}={2} for {0}',
displayable_path(self.paths),
field,
value)
self.album[field] = value
self.album.store()
for item in items:
item[field] = value
with lib.transaction():
for item in items:
item.store()
self.album.store()
Comment on lines +590 to +592
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be sufficient for the transaction to extend only over these three lines, setting the fields above shouldn't interact with the database.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thx, fixed


def finalize(self, session):
"""Save progress, clean up files, and emit plugin event.
Expand Down Expand Up @@ -946,9 +952,9 @@ def choose_match(self, session):
def reload(self):
self.item.load()

def set_fields(self):
def set_fields(self, lib):
"""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():
value = view.get()
Expand Down Expand Up @@ -1516,7 +1522,7 @@ def apply_choice(session, task):
# because then the ``ImportTask`` won't have an `album` for which
# it can set the fields.
if config['import']['set_fields']:
task.set_fields()
task.set_fields(session.lib)


@pipeline.mutator_stage
Expand Down
2 changes: 2 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ Other new things:
``check_on_import`` config option.
* :doc:`/plugins/export`: big speedups when `--include-keys` option is used
Thanks to :user:`ssssam`.
* The `importer` persists all fields set using :ref:`set_fields` to the
mediafiles of all imported tracks.
* Added 7z support via the `py7zr`_ library
Thanks to :user:`arogl`. :bug:`3906`
* Get ISRC identifiers from musicbrainz
Expand Down
3 changes: 3 additions & 0 deletions docs/reference/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,9 @@ Here's an example::
Other field/value pairs supplied via the ``--set`` option on the command-line
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).

.. _musicbrainz-config:
Expand Down
28 changes: 25 additions & 3 deletions test/test_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -740,10 +740,12 @@ def test_asis_no_data_source(self):
def test_set_fields(self):
genre = u"\U0001F3B7 Jazz"
collection = u"To Listen"
comments = u"managed by beets"

config['import']['set_fields'] = {
u'genre': genre,
u'collection': collection,
u'genre': genre
u'comments': comments
}

# As-is album import.
Expand All @@ -754,7 +756,17 @@ def test_set_fields(self):
for album in self.lib.albums():
album.load() # TODO: Not sure this is necessary.
self.assertEqual(album.genre, genre)
self.assertEqual(album.collection, collection)
self.assertEqual(album.comments, comments)
for item in album.items():
self.assertEqual(
item.get("genre", with_album=False),
genre)
self.assertEqual(
item.get("collection", with_album=False),
collection)
self.assertEqual(
item.get("comments", with_album=False),
comments)
# Remove album from library to test again with APPLY choice.
album.remove()

Expand All @@ -767,7 +779,17 @@ def test_set_fields(self):
for album in self.lib.albums():
album.load()
self.assertEqual(album.genre, genre)
self.assertEqual(album.collection, collection)
self.assertEqual(album.comments, comments)
for item in album.items():
self.assertEqual(
item.get("genre", with_album=False),
genre)
self.assertEqual(
item.get("collection", with_album=False),
collection)
self.assertEqual(
item.get("comments", with_album=False),
comments)


class ImportTracksTest(_common.TestCase, ImportHelper):
Expand Down