Skip to content

Commit e848ada

Browse files
committed
Implement from_scratch option
Fixes #934, and also helps with #1173.
1 parent d932aa4 commit e848ada

File tree

7 files changed

+40
-0
lines changed

7 files changed

+40
-0
lines changed

beets/config_default.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import:
1010
delete: no
1111
resume: ask
1212
incremental: no
13+
from_scratch: no
1314
quiet_fallback: skip
1415
none_rec_action: ask
1516
timid: no

beets/importer.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,10 @@ def imported_items(self):
534534
def apply_metadata(self):
535535
"""Copy metadata from match info to the items.
536536
"""
537+
if config['import']['from_scratch']:
538+
for item in self.match.mapping:
539+
item.clear()
540+
537541
autotag.apply_metadata(self.match.info, self.match.mapping)
538542

539543
def duplicate_items(self, lib):

beets/library.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,11 @@ def update(self, values):
561561
if self.mtime == 0 and 'mtime' in values:
562562
self.mtime = values['mtime']
563563

564+
def clear(self):
565+
"""Set all key/value pairs to None."""
566+
for key in self._media_fields:
567+
setattr(self, key, None)
568+
564569
def get_album(self):
565570
"""Get the Album object that this item belongs to, if any, or
566571
None if the item is a singleton or is not associated with a

beets/ui/commands.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,10 @@ def import_func(lib, opts, args):
10041004
u'-I', u'--noincremental', dest='incremental', action='store_false',
10051005
help=u'do not skip already-imported directories'
10061006
)
1007+
import_cmd.parser.add_option(
1008+
u'--from-scratch', dest='from_scratch', action='store_true',
1009+
help=u'erase existing metadata before applying new metadata'
1010+
)
10071011
import_cmd.parser.add_option(
10081012
u'--flat', dest='flat', action='store_true',
10091013
help=u'import an entire tree as a single album'

docs/reference/cli.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ Optional command flags:
111111
time, when no subdirectories will be skipped. So consider enabling the
112112
``incremental`` configuration option.
113113

114+
* When beets applies metadata to your music, it will retain the value of any
115+
existing tags that weren't overwritten, and import them into the database. You
116+
may prefer to only use existing metadata for finding matches, and to erase it
117+
completely when new metadata is applied. You can enforce this behavior with
118+
the ``--from-scratch`` option, or the ``from_scratch`` configuration option.
119+
114120
* By default, beets will proceed without asking if it finds a very close
115121
metadata match. To disable this and have the importer ask you every time,
116122
use the ``-t`` (for *timid*) option.

docs/reference/config.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,15 @@ Either ``yes`` or ``no``, controlling whether imported directories are
475475
recorded and whether these recorded directories are skipped. This
476476
corresponds to the ``-i`` flag to ``beet import``.
477477

478+
.. _from_scratch:
479+
480+
from_scratch
481+
~~~~~~~~~~~~
482+
483+
Either ``yes`` or ``no`` (default), controlling whether existing metadata is
484+
discarded when a match is applied. This corresponds to the ``-from_scratch``
485+
flag to ``beet import``.
486+
478487
quiet_fallback
479488
~~~~~~~~~~~~~~
480489

test/test_importer.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,17 @@ def test_apply_candidate_adds_album_path(self):
633633
self.assert_file_in_lib(
634634
b'Applied Artist', b'Applied Album', b'Applied Title 1.mp3')
635635

636+
def test_apply_from_scratch_removes_other_metadata(self):
637+
config['import']['from_scratch'] = True
638+
639+
for mediafile in self.import_media:
640+
mediafile.genre = u'Tag Genre'
641+
mediafile.save()
642+
643+
self.importer.add_choice(importer.action.APPLY)
644+
self.importer.run()
645+
self.assertEqual(self.lib.items().get().genre, u'')
646+
636647
def test_apply_with_move_deletes_import(self):
637648
config['import']['move'] = True
638649

0 commit comments

Comments
 (0)