Skip to content

Commit

Permalink
Merge pull request #4125 from kergoth/destination-replacements
Browse files Browse the repository at this point in the history
  • Loading branch information
kergoth authored Oct 27, 2021
2 parents ee8a4de + 86465e6 commit b8b74a7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
6 changes: 4 additions & 2 deletions beets/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,7 @@ def move(self, operation=MoveOperation.MOVE, basedir=None,
# Templating.

def destination(self, fragment=False, basedir=None, platform=None,
path_formats=None):
path_formats=None, replacements=None):
"""Returns the path in the library directory designated for the
item (i.e., where the file ought to be). fragment makes this
method return just the path fragment underneath the root library
Expand All @@ -950,6 +950,8 @@ def destination(self, fragment=False, basedir=None, platform=None,
platform = platform or sys.platform
basedir = basedir or self._db.directory
path_formats = path_formats or self._db.path_formats
if replacements is None:
replacements = self._db.replacements

# Use a path format based on a query, falling back on the
# default.
Expand Down Expand Up @@ -994,7 +996,7 @@ def destination(self, fragment=False, basedir=None, platform=None,
maxlen = util.max_filename_length(self._db.directory)

subpath, fellback = util.legalize_path(
subpath, self._db.replacements, maxlen,
subpath, replacements, maxlen,
os.path.splitext(self.path)[1], fragment
)
if fellback:
Expand Down
5 changes: 5 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ Other new things:
* :doc:`/plugins/unimported`: Support excluding specific
subdirectories in library.

For plugin developers:

* :py:meth:`beets.library.Item.destination` now accepts a `replacements`
argument to be used in favor of the default.

Bug fixes:

* :doc:`/plugins/lyrics`: Fix crash bug when beautifulsoup4 is not installed.
Expand Down
10 changes: 10 additions & 0 deletions test/test_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,16 @@ def test_destination_with_replacements(self):
self.assertEqual(self.i.destination(),
np('base/ber/foo'))

def test_destination_with_replacements_argument(self):
self.lib.directory = b'base'
self.lib.replacements = [(re.compile(r'a'), 'f')]
self.lib.path_formats = [('default', '$album/$title')]
self.i.title = 'foo'
self.i.album = 'bar'
replacements = [(re.compile(r'a'), 'e')]
self.assertEqual(self.i.destination(replacements=replacements),
np('base/ber/foo'))

@unittest.skip('unimplemented: #359')
def test_destination_with_empty_component(self):
self.lib.directory = b'base'
Expand Down

0 comments on commit b8b74a7

Please sign in to comment.