From d2a95158f21374c38cf42011f55a9f2ffa8c7124 Mon Sep 17 00:00:00 2001 From: Amish Bhadeshia Date: Sat, 21 Mar 2015 15:36:39 +0000 Subject: [PATCH 1/5] Updated ftintitle plugin to follow a format --- beetsplug/ftintitle.py | 13 ++++++++----- docs/plugins/ftintitle.rst | 3 +++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/beetsplug/ftintitle.py b/beetsplug/ftintitle.py index f7116df15..0c3b7122c 100644 --- a/beetsplug/ftintitle.py +++ b/beetsplug/ftintitle.py @@ -82,6 +82,7 @@ class FtInTitlePlugin(plugins.BeetsPlugin): self.config.add({ 'auto': True, 'drop': False, + 'format': u'feat. {}' }) self._command = ui.Subcommand( @@ -116,12 +117,13 @@ class FtInTitlePlugin(plugins.BeetsPlugin): """Import hook for moving featuring artist automatically. """ drop_feat = self.config['drop'].get(bool) + feat_format = self.config['format'].get(unicode) for item in task.imported_items(): - self.ft_in_title(item, drop_feat) + self.ft_in_title(item, drop_feat, feat_format) item.store() - def update_metadata(self, item, feat_part, drop_feat): + def update_metadata(self, item, feat_part, drop_feat, feat_format): """Choose how to add new artists to the title and set the new metadata. Also, print out messages about any changes that are made. If `drop_feat` is set, then do not add the artist to the title; just @@ -137,11 +139,12 @@ class FtInTitlePlugin(plugins.BeetsPlugin): # Only update the title if it does not already contain a featured # artist and if we do not drop featuring information. if not drop_feat and not contains_feat(item.title): - new_title = u"{0} feat. {1}".format(item.title, feat_part) + new_format = feat_format.format(feat_part) + new_title = u"{0} {1}".format(item.title, new_format) self._log.info(u'title: {0} -> {1}', item.title, new_title) item.title = new_title - def ft_in_title(self, item, drop_feat): + def ft_in_title(self, item, drop_feat, feat_format): """Look for featured artists in the item's artist fields and move them to the title. """ @@ -162,6 +165,6 @@ class FtInTitlePlugin(plugins.BeetsPlugin): # If we have a featuring artist, move it to the title. if feat_part: - self.update_metadata(item, feat_part, drop_feat) + self.update_metadata(item, feat_part, drop_feat, feat_format) else: self._log.info(u'no featuring artists found') diff --git a/docs/plugins/ftintitle.rst b/docs/plugins/ftintitle.rst index e2258942d..9da66d7be 100644 --- a/docs/plugins/ftintitle.rst +++ b/docs/plugins/ftintitle.rst @@ -24,6 +24,9 @@ file. The available options are: - **drop**: Remove featured artists entirely instead of adding them to the title field. Default: ``no``. +- **format**: Defines the format for the feat part of the new title field. + In this format the ``{}`` is used to define where the featured artists are placed + Default: ``feat. {}`` Running Manually ---------------- From 232ff0576663caa0108d4427d29c7a301063dbfc Mon Sep 17 00:00:00 2001 From: Amish Bhadeshia Date: Fri, 27 Mar 2015 17:59:35 +0000 Subject: [PATCH 2/5] Added tests and simplified implementation method --- beetsplug/ftintitle.py | 10 ++++---- test/test_ftintitle.py | 56 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 5 deletions(-) diff --git a/beetsplug/ftintitle.py b/beetsplug/ftintitle.py index 0c3b7122c..83940a471 100644 --- a/beetsplug/ftintitle.py +++ b/beetsplug/ftintitle.py @@ -117,13 +117,12 @@ class FtInTitlePlugin(plugins.BeetsPlugin): """Import hook for moving featuring artist automatically. """ drop_feat = self.config['drop'].get(bool) - feat_format = self.config['format'].get(unicode) for item in task.imported_items(): - self.ft_in_title(item, drop_feat, feat_format) + self.ft_in_title(item, drop_feat) item.store() - def update_metadata(self, item, feat_part, drop_feat, feat_format): + def update_metadata(self, item, feat_part, drop_feat): """Choose how to add new artists to the title and set the new metadata. Also, print out messages about any changes that are made. If `drop_feat` is set, then do not add the artist to the title; just @@ -139,12 +138,13 @@ class FtInTitlePlugin(plugins.BeetsPlugin): # Only update the title if it does not already contain a featured # artist and if we do not drop featuring information. if not drop_feat and not contains_feat(item.title): + feat_format = self.config['format'].get(unicode) new_format = feat_format.format(feat_part) new_title = u"{0} {1}".format(item.title, new_format) self._log.info(u'title: {0} -> {1}', item.title, new_title) item.title = new_title - def ft_in_title(self, item, drop_feat, feat_format): + def ft_in_title(self, item, drop_feat): """Look for featured artists in the item's artist fields and move them to the title. """ @@ -165,6 +165,6 @@ class FtInTitlePlugin(plugins.BeetsPlugin): # If we have a featuring artist, move it to the title. if feat_part: - self.update_metadata(item, feat_part, drop_feat, feat_format) + self.update_metadata(item, feat_part, drop_feat) else: self._log.info(u'no featuring artists found') diff --git a/test/test_ftintitle.py b/test/test_ftintitle.py index 435230fb1..e849d3631 100644 --- a/test/test_ftintitle.py +++ b/test/test_ftintitle.py @@ -18,9 +18,65 @@ from __future__ import (division, absolute_import, print_function, unicode_literals) from test._common import unittest +from test.helper import TestHelper from beetsplug import ftintitle +class FtInTitlePluginFunctional(unittest.TestCase, TestHelper): + def setUp(self): + """Set up configuration""" + self.setup_beets() + self.load_plugins('ftintitle') + + def tearDown(self): + self.unload_plugins() + self.teardown_beets() + + def _ft_add_item(self,path, artist, title, aartist): + return self.add_item(path=path, + artist=artist, + title=title, + albumartist=aartist) + + def _ft_set_config(self, ftformat, drop = False, auto = True): + self.config['ftintitle']['format'] = ftformat + self.config['ftintitle']['drop'] = drop + self.config['ftintitle']['auto'] = auto + + def test_functional_drop(self): + item = self._ft_add_item('/', u'Alice ft Bob', u'Song 1', u'Alice') + self.run_command('ftintitle', '-d') + item.load() + self.assertEqual(item['artist'], u'Alice') + self.assertEqual(item['title'], u'Song 1') + + def test_functional_custom_format_1(self): + self._ft_set_config('feat. {}') + + item = self._ft_add_item('/', u'Alice ft Bob', u'Song 1', u'Alice') + self.run_command('ftintitle') + item.load() + self.assertEqual(item['artist'], u'Alice') + self.assertEqual(item['title'], u'Song 1 feat. Bob') + + def test_functional_custom_format_2(self): + self._ft_set_config('featuring {}') + + item = self._ft_add_item('/', u'Alice feat. Bob', u'Song 1', u'Alice') + self.run_command('ftintitle') + item.load() + self.assertEqual(item['artist'], u'Alice') + self.assertEqual(item['title'], u'Song 1 featuring Bob') + + def test_functional_custom_format_3(self): + self._ft_set_config('with {}') + + item = self._ft_add_item('/', u'Alice feat Bob', u'Song 1', u'Alice') + self.run_command('ftintitle') + item.load() + self.assertEqual(item['artist'], u'Alice') + self.assertEqual(item['title'], u'Song 1 with Bob') + class FtInTitlePluginTest(unittest.TestCase): def setUp(self): """Set up configuration""" From 20debd020e9c11c90254f1ac9b22b242fe14171a Mon Sep 17 00:00:00 2001 From: Amish Bhadeshia Date: Fri, 27 Mar 2015 20:45:28 +0000 Subject: [PATCH 3/5] Fixed travis errors --- test/test_ftintitle.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/test/test_ftintitle.py b/test/test_ftintitle.py index e849d3631..534ae8e93 100644 --- a/test/test_ftintitle.py +++ b/test/test_ftintitle.py @@ -32,13 +32,13 @@ class FtInTitlePluginFunctional(unittest.TestCase, TestHelper): self.unload_plugins() self.teardown_beets() - def _ft_add_item(self,path, artist, title, aartist): + def _ft_add_item(self, path, artist, title, aartist): return self.add_item(path=path, artist=artist, title=title, albumartist=aartist) - def _ft_set_config(self, ftformat, drop = False, auto = True): + def _ft_set_config(self, ftformat, drop=False, auto=True): self.config['ftintitle']['format'] = ftformat self.config['ftintitle']['drop'] = drop self.config['ftintitle']['auto'] = auto @@ -50,33 +50,29 @@ class FtInTitlePluginFunctional(unittest.TestCase, TestHelper): self.assertEqual(item['artist'], u'Alice') self.assertEqual(item['title'], u'Song 1') - def test_functional_custom_format_1(self): + def test_functional_custom_format(self): self._ft_set_config('feat. {}') - item = self._ft_add_item('/', u'Alice ft Bob', u'Song 1', u'Alice') self.run_command('ftintitle') item.load() self.assertEqual(item['artist'], u'Alice') self.assertEqual(item['title'], u'Song 1 feat. Bob') - def test_functional_custom_format_2(self): self._ft_set_config('featuring {}') - item = self._ft_add_item('/', u'Alice feat. Bob', u'Song 1', u'Alice') self.run_command('ftintitle') item.load() self.assertEqual(item['artist'], u'Alice') self.assertEqual(item['title'], u'Song 1 featuring Bob') - def test_functional_custom_format_3(self): self._ft_set_config('with {}') - item = self._ft_add_item('/', u'Alice feat Bob', u'Song 1', u'Alice') self.run_command('ftintitle') item.load() self.assertEqual(item['artist'], u'Alice') self.assertEqual(item['title'], u'Song 1 with Bob') + class FtInTitlePluginTest(unittest.TestCase): def setUp(self): """Set up configuration""" From 6365a9b538142e5fb27bd5c9379e42118f76836a Mon Sep 17 00:00:00 2001 From: Amish Bhadeshia Date: Fri, 27 Mar 2015 20:54:42 +0000 Subject: [PATCH 4/5] Added indice into curly brackets, for py26 compatabiity --- beetsplug/ftintitle.py | 2 +- docs/plugins/ftintitle.rst | 4 ++-- test/test_ftintitle.py | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/beetsplug/ftintitle.py b/beetsplug/ftintitle.py index 83940a471..9e71113f3 100644 --- a/beetsplug/ftintitle.py +++ b/beetsplug/ftintitle.py @@ -82,7 +82,7 @@ class FtInTitlePlugin(plugins.BeetsPlugin): self.config.add({ 'auto': True, 'drop': False, - 'format': u'feat. {}' + 'format': u'feat. {0}' }) self._command = ui.Subcommand( diff --git a/docs/plugins/ftintitle.rst b/docs/plugins/ftintitle.rst index 9da66d7be..be2911975 100644 --- a/docs/plugins/ftintitle.rst +++ b/docs/plugins/ftintitle.rst @@ -25,8 +25,8 @@ file. The available options are: title field. Default: ``no``. - **format**: Defines the format for the feat part of the new title field. - In this format the ``{}`` is used to define where the featured artists are placed - Default: ``feat. {}`` + In this format the ``{0}`` is used to define where the featured artists are placed + Default: ``feat. {0}`` Running Manually ---------------- diff --git a/test/test_ftintitle.py b/test/test_ftintitle.py index 534ae8e93..67b952a51 100644 --- a/test/test_ftintitle.py +++ b/test/test_ftintitle.py @@ -51,21 +51,21 @@ class FtInTitlePluginFunctional(unittest.TestCase, TestHelper): self.assertEqual(item['title'], u'Song 1') def test_functional_custom_format(self): - self._ft_set_config('feat. {}') + self._ft_set_config('feat. {0}') item = self._ft_add_item('/', u'Alice ft Bob', u'Song 1', u'Alice') self.run_command('ftintitle') item.load() self.assertEqual(item['artist'], u'Alice') self.assertEqual(item['title'], u'Song 1 feat. Bob') - self._ft_set_config('featuring {}') + self._ft_set_config('featuring {0}') item = self._ft_add_item('/', u'Alice feat. Bob', u'Song 1', u'Alice') self.run_command('ftintitle') item.load() self.assertEqual(item['artist'], u'Alice') self.assertEqual(item['title'], u'Song 1 featuring Bob') - self._ft_set_config('with {}') + self._ft_set_config('with {0}') item = self._ft_add_item('/', u'Alice feat Bob', u'Song 1', u'Alice') self.run_command('ftintitle') item.load() From 9a38b07ed4c8708f346af549fe86ec1cd0846393 Mon Sep 17 00:00:00 2001 From: Amish Bhadeshia Date: Sat, 28 Mar 2015 11:20:28 +0000 Subject: [PATCH 5/5] Housekeeping and styling changes --- beetsplug/ftintitle.py | 2 +- docs/plugins/ftintitle.rst | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/beetsplug/ftintitle.py b/beetsplug/ftintitle.py index 9e71113f3..377548ee8 100644 --- a/beetsplug/ftintitle.py +++ b/beetsplug/ftintitle.py @@ -82,7 +82,7 @@ class FtInTitlePlugin(plugins.BeetsPlugin): self.config.add({ 'auto': True, 'drop': False, - 'format': u'feat. {0}' + 'format': u'feat. {0}', }) self._command = ui.Subcommand( diff --git a/docs/plugins/ftintitle.rst b/docs/plugins/ftintitle.rst index be2911975..8a080b3e2 100644 --- a/docs/plugins/ftintitle.rst +++ b/docs/plugins/ftintitle.rst @@ -24,8 +24,8 @@ file. The available options are: - **drop**: Remove featured artists entirely instead of adding them to the title field. Default: ``no``. -- **format**: Defines the format for the feat part of the new title field. - In this format the ``{0}`` is used to define where the featured artists are placed +- **format**: Defines the format for the featuring X part of the new title field. + In this format the ``{0}`` is used to define where the featured artists are placed. Default: ``feat. {0}`` Running Manually