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.
This commit is contained in:
Thomas Scholtes 2014-09-17 12:17:20 +02:00
parent 0bf7c06f7d
commit db391c8f20
3 changed files with 36 additions and 22 deletions

View file

@ -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

View file

@ -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

View file

@ -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__)