From db391c8f20998d960cc7997e608b50714675143b Mon Sep 17 00:00:00 2001 From: Thomas Scholtes Date: Wed, 17 Sep 2014 12:17:20 +0200 Subject: [PATCH] zero: Only changes media file tags not database Uses the new API from the previous commit and fixes #963. There is a possible issue with backwards compatibility: Changes to the item in the 'write' event do not propagate to the tags anymore. But I'm not aware of other plugins that use the API in that way. --- beetsplug/zero.py | 8 ++++---- docs/changelog.rst | 2 ++ test/test_zero.py | 48 +++++++++++++++++++++++++++++----------------- 3 files changed, 36 insertions(+), 22 deletions(-) diff --git a/beetsplug/zero.py b/beetsplug/zero.py index b9768431c..8c65fe855 100644 --- a/beetsplug/zero.py +++ b/beetsplug/zero.py @@ -78,18 +78,18 @@ class ZeroPlugin(BeetsPlugin): return True return False - def write_event(self, item): + def write_event(self, item, path, tags): """Listen for write event.""" if not self.patterns: log.warn(u'[zero] no fields, nothing to do') return for field, patterns in self.patterns.items(): - if field not in item.keys(): + if field not in tags: log.error(u'[zero] no such field: {0}'.format(field)) continue - value = item[field] + value = tags[field] if self.match_patterns(value, patterns): log.debug(u'[zero] {0}: {1} -> None'.format(field, value)) - item[field] = None + tags[field] = None diff --git a/docs/changelog.rst b/docs/changelog.rst index 41c9853ec..223d816d3 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -70,6 +70,8 @@ Fixes: automatically. * The ``write`` event allows plugins to change the tags that are written to a media file. +* :doc:`/plugins/zero`: Do not delete database values, only media file + tags. .. _discogs_client: https://github.com/discogs/discogs_client diff --git a/test/test_zero.py b/test/test_zero.py index e0a0ba943..38b854e45 100644 --- a/test/test_zero.py +++ b/test/test_zero.py @@ -18,37 +18,38 @@ class ZeroPluginTest(unittest.TestCase, TestHelper): self.unload_plugins() def test_no_patterns(self): - i = Item( - comments='test comment', - day=13, - month=3, - year=2012, - ) + tags = { + 'comments': 'test comment', + 'day': 13, + 'month': 3, + 'year': 2012, + } z = ZeroPlugin() z.debug = False z.fields = ['comments', 'month', 'day'] z.patterns = {'comments': ['.'], 'month': ['.'], 'day': ['.']} - z.write_event(i) - self.assertEqual(i.comments, '') - self.assertEqual(i.day, 0) - self.assertEqual(i.month, 0) - self.assertEqual(i.year, 2012) + z.write_event(None, None, tags) + self.assertEqual(tags['comments'], None) + self.assertEqual(tags['day'], None) + self.assertEqual(tags['month'], None) + self.assertEqual(tags['year'], 2012) def test_patterns(self): - i = Item( - comments='from lame collection, ripped by eac', - year=2012, - ) z = ZeroPlugin() z.debug = False z.fields = ['comments', 'year'] z.patterns = {'comments': 'eac lame'.split(), 'year': '2098 2099'.split()} - z.write_event(i) - self.assertEqual(i.comments, '') - self.assertEqual(i.year, 2012) + + tags = { + 'comments': 'from lame collection, ripped by eac', + 'year': 2012, + } + z.write_event(None, None, tags) + self.assertEqual(tags['comments'], None) + self.assertEqual(tags['year'], 2012) def test_delete_replaygain_tag(self): path = self.create_mediafile_fixture() @@ -70,6 +71,17 @@ class ZeroPluginTest(unittest.TestCase, TestHelper): self.assertIsNone(mediafile.rg_track_peak) self.assertIsNone(mediafile.rg_track_gain) + def test_do_not_change_database(self): + item = self.add_item_fixture(year=2000) + mediafile = MediaFile(item.path) + + config['zero'] = {'fields': ['year']} + self.load_plugins('zero') + + item.write() + self.assertEqual(item['year'], 2000) + self.assertIsNone(mediafile.year) + def suite(): return unittest.TestLoader().loadTestsFromName(__name__)