Skip to content

Commit

Permalink
Merge branch '2738-filename-tracknumber' of https://github.com/Vrihub…
Browse files Browse the repository at this point in the history
…/beets into 2738-filename-tracknumber
  • Loading branch information
Vrihub committed Dec 21, 2017
2 parents 4ee5f2c + 39f211e commit d0af7a7
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 11 deletions.
1 change: 1 addition & 0 deletions beets/config_default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import:
delete: no
resume: ask
incremental: no
from_scratch: no
quiet_fallback: skip
none_rec_action: ask
timid: no
Expand Down
4 changes: 4 additions & 0 deletions beets/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,10 @@ def imported_items(self):
def apply_metadata(self):
"""Copy metadata from match info to the items.
"""
if config['import']['from_scratch']:
for item in self.match.mapping:
item.clear()

autotag.apply_metadata(self.match.info, self.match.mapping)

def duplicate_items(self, lib):
Expand Down
5 changes: 5 additions & 0 deletions beets/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,11 @@ def update(self, values):
if self.mtime == 0 and 'mtime' in values:
self.mtime = values['mtime']

def clear(self):
"""Set all key/value pairs to None."""
for key in self._media_fields:
setattr(self, key, None)

def get_album(self):
"""Get the Album object that this item belongs to, if any, or
None if the item is a singleton or is not associated with a
Expand Down
4 changes: 4 additions & 0 deletions beets/ui/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,10 @@ def import_func(lib, opts, args):
u'-I', u'--noincremental', dest='incremental', action='store_false',
help=u'do not skip already-imported directories'
)
import_cmd.parser.add_option(
u'--from-scratch', dest='from_scratch', action='store_true',
help=u'erase existing metadata before applying new metadata'
)
import_cmd.parser.add_option(
u'--flat', dest='flat', action='store_true',
help=u'import an entire tree as a single album'
Expand Down
9 changes: 4 additions & 5 deletions beets/util/artresizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,13 @@ def im_resize(maxwidth, path_in, path_out=None):
log.debug(u'artresizer: ImageMagick resizing {0} to {1}',
util.displayable_path(path_in), util.displayable_path(path_out))

# "-resize widthxheight>" shrinks images with dimension(s) larger
# than the corresponding width and/or height dimension(s). The >
# "only shrink" flag is prefixed by ^ escape char for Windows
# compatibility.
# "-resize WIDTHx>" shrinks images with the width larger
# than the given width while maintaining the aspect ratio
# with regards to the height.
try:
util.command_output([
'convert', util.syspath(path_in, prefix=False),
'-resize', '{0}x^>'.format(maxwidth),
'-resize', '{0}x>'.format(maxwidth),
util.syspath(path_out, prefix=False),
])
except subprocess.CalledProcessError:
Expand Down
11 changes: 5 additions & 6 deletions beetsplug/duplicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,20 +253,19 @@ def _order(self, objs, tiebreak=None):
"completeness" (objects with more non-null fields come first)
and Albums are ordered by their track count.
"""
if tiebreak:
kind = 'items' if all(isinstance(o, Item)
for o in objs) else 'albums'
kind = 'items' if all(isinstance(o, Item) for o in objs) else 'albums'

if tiebreak and kind in tiebreak.keys():
key = lambda x: tuple(getattr(x, k) for k in tiebreak[kind])
else:
kind = Item if all(isinstance(o, Item) for o in objs) else Album
if kind is Item:
if kind == 'items':
def truthy(v):
# Avoid a Unicode warning by avoiding comparison
# between a bytes object and the empty Unicode
# string ''.
return v is not None and \
(v != '' if isinstance(v, six.text_type) else True)
fields = kind.all_keys()
fields = Item.all_keys()
key = lambda x: sum(1 for f in fields if truthy(getattr(x, f)))
else:
key = lambda x: len(x.items())
Expand Down
11 changes: 11 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ New features:
* :doc:`/plugins/acousticbrainz`: The plugin can now be configured to write only
a specific list of tags.
Thanks to :user:`woparry`.
* A new :ref:`from_scratch` configuration option makes the importer remove old
metadata before applying new metadata.
Thanks to :user:`tummychow`.
:bug:`934` :bug:`2755`

Fixes:

Expand Down Expand Up @@ -70,6 +74,10 @@ Fixes:
Python 3 on Windows with non-ASCII filenames. :bug:`2671`
* :doc:`/plugins/absubmit`: Fix an occasional crash on Python 3 when the AB
analysis tool produced non-ASCII metadata. :bug:`2673`
* :doc:`/plugins/duplicates`: Use default tiebreak for any kind (item/album) that
does not have a tiebreak specified in the configuration.
Thanks to :user:`cgevans`.
:bug:`2758`
* :doc:`/plugins/duplicates`: Fix the `--key` command line option, which was
ignored.
* :doc:`/plugins/replaygain`: Fix album replaygain calculation with the
Expand All @@ -82,6 +90,9 @@ Fixes:
with no following numbers. Thanks to :user:`eigengrau`. :bug:`2741`
* :doc:`/plugins/fromfilename`: Allow file names such as "01.mp3" to extract the track number.
Also allow "_" as a separator. Refactor some regular expressions. :bug:`2738`
* Fixed an issue where images would be resized according to their longest edge,
instead of their width. Thanks to :user:`sekjun9878`. :bug:`2729`


For developers:

Expand Down
6 changes: 6 additions & 0 deletions docs/reference/cli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ Optional command flags:
time, when no subdirectories will be skipped. So consider enabling the
``incremental`` configuration option.

* When beets applies metadata to your music, it will retain the value of any
existing tags that weren't overwritten, and import them into the database. You
may prefer to only use existing metadata for finding matches, and to erase it
completely when new metadata is applied. You can enforce this behavior with
the ``--from-scratch`` option, or the ``from_scratch`` configuration option.

* By default, beets will proceed without asking if it finds a very close
metadata match. To disable this and have the importer ask you every time,
use the ``-t`` (for *timid*) option.
Expand Down
9 changes: 9 additions & 0 deletions docs/reference/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,15 @@ Either ``yes`` or ``no``, controlling whether imported directories are
recorded and whether these recorded directories are skipped. This
corresponds to the ``-i`` flag to ``beet import``.

.. _from_scratch:

from_scratch
~~~~~~~~~~~~

Either ``yes`` or ``no`` (default), controlling whether existing metadata is
discarded when a match is applied. This corresponds to the ``--from_scratch``
flag to ``beet import``.

quiet_fallback
~~~~~~~~~~~~~~

Expand Down
11 changes: 11 additions & 0 deletions test/test_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,17 @@ def test_apply_candidate_adds_album_path(self):
self.assert_file_in_lib(
b'Applied Artist', b'Applied Album', b'Applied Title 1.mp3')

def test_apply_from_scratch_removes_other_metadata(self):
config['import']['from_scratch'] = True

for mediafile in self.import_media:
mediafile.genre = u'Tag Genre'
mediafile.save()

self.importer.add_choice(importer.action.APPLY)
self.importer.run()
self.assertEqual(self.lib.items().get().genre, u'')

def test_apply_with_move_deletes_import(self):
config['import']['move'] = True

Expand Down

0 comments on commit d0af7a7

Please sign in to comment.