diff --git a/beets/dbcore/db.py b/beets/dbcore/db.py index 11db94d47..86078fb6a 100644 --- a/beets/dbcore/db.py +++ b/beets/dbcore/db.py @@ -346,6 +346,10 @@ class Model(object): value = unicode(value) if for_path: + replacements = self._db.replacements or [] + for regex, repl in replacements: + value = regex.sub(repl, value) + sep_repl = beets.config['path_sep_replace'].get(unicode) for sep in (os.path.sep, os.path.altsep): if sep: diff --git a/docs/changelog.rst b/docs/changelog.rst index cd3fe1d0a..52da489b2 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -70,6 +70,8 @@ Other little fixes: are now treated as directories. Thanks to Pedro Silva. * The :ref:`modify-cmd` command now skips confirmation and prints a message if no changes are necessary. Thanks to brilnius. +* The replacement characters for path separators can be set in the + "replace" configuration. 1.3.2 (December 22, 2013) diff --git a/test/_common.py b/test/_common.py index 00640f5bc..540397cce 100644 --- a/test/_common.py +++ b/test/_common.py @@ -257,14 +257,18 @@ class Bag(object): def platform_windows(): import ntpath old_path = os.path - os.path = ntpath - yield - os.path = old_path + try: + os.path = ntpath + yield + finally: + os.path = old_path @contextmanager def platform_posix(): import posixpath old_path = os.path - os.path = posixpath - yield - os.path = old_path + try: + os.path = posixpath + yield + finally: + os.path = old_path diff --git a/test/test_library.py b/test/test_library.py index 8eabb4122..ac6ffbe37 100644 --- a/test/test_library.py +++ b/test/test_library.py @@ -248,6 +248,21 @@ class DestinationTest(_common.TestCase): self.assertFalse('>' in p) self.assertFalse('|' in p) + def test_replace_unix_path_separator_from_config(self): + self.i.title = 'one \\ two / three.mp3' + self.lib.replacements = [(re.compile(r'[\\/]'), 'x')] + with _common.platform_windows(): + p = self.i.destination() + self.assertTrue('one x two x three.mp3' in p) + self.lib.replacements = None + + def test_replace_windows_path_separator_from_config(self): + self.i.title = 'one \\ two / three.mp3' + self.lib.replacements = [(re.compile(r'[\\/]'), 'x')] + with _common.platform_windows(): + p = self.i.destination() + self.assertTrue('one x two x three.mp3' in p) + def test_path_with_format(self): self.lib.path_formats = [('default', '$artist/$album ($format)')] p = self.i.destination()