Skip to content

Commit db391c8

Browse files
author
Thomas Scholtes
committed
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.
1 parent 0bf7c06 commit db391c8

File tree

3 files changed

+36
-22
lines changed

3 files changed

+36
-22
lines changed

beetsplug/zero.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,18 @@ def match_patterns(cls, field, patterns):
7878
return True
7979
return False
8080

81-
def write_event(self, item):
81+
def write_event(self, item, path, tags):
8282
"""Listen for write event."""
8383
if not self.patterns:
8484
log.warn(u'[zero] no fields, nothing to do')
8585
return
8686

8787
for field, patterns in self.patterns.items():
88-
if field not in item.keys():
88+
if field not in tags:
8989
log.error(u'[zero] no such field: {0}'.format(field))
9090
continue
9191

92-
value = item[field]
92+
value = tags[field]
9393
if self.match_patterns(value, patterns):
9494
log.debug(u'[zero] {0}: {1} -> None'.format(field, value))
95-
item[field] = None
95+
tags[field] = None

docs/changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ Fixes:
7070
automatically.
7171
* The ``write`` event allows plugins to change the tags that are
7272
written to a media file.
73+
* :doc:`/plugins/zero`: Do not delete database values, only media file
74+
tags.
7375

7476
.. _discogs_client: https://github.com/discogs/discogs_client
7577

test/test_zero.py

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,38 @@ def tearDown(self):
1818
self.unload_plugins()
1919

2020
def test_no_patterns(self):
21-
i = Item(
22-
comments='test comment',
23-
day=13,
24-
month=3,
25-
year=2012,
26-
)
21+
tags = {
22+
'comments': 'test comment',
23+
'day': 13,
24+
'month': 3,
25+
'year': 2012,
26+
}
2727
z = ZeroPlugin()
2828
z.debug = False
2929
z.fields = ['comments', 'month', 'day']
3030
z.patterns = {'comments': ['.'],
3131
'month': ['.'],
3232
'day': ['.']}
33-
z.write_event(i)
34-
self.assertEqual(i.comments, '')
35-
self.assertEqual(i.day, 0)
36-
self.assertEqual(i.month, 0)
37-
self.assertEqual(i.year, 2012)
33+
z.write_event(None, None, tags)
34+
self.assertEqual(tags['comments'], None)
35+
self.assertEqual(tags['day'], None)
36+
self.assertEqual(tags['month'], None)
37+
self.assertEqual(tags['year'], 2012)
3838

3939
def test_patterns(self):
40-
i = Item(
41-
comments='from lame collection, ripped by eac',
42-
year=2012,
43-
)
4440
z = ZeroPlugin()
4541
z.debug = False
4642
z.fields = ['comments', 'year']
4743
z.patterns = {'comments': 'eac lame'.split(),
4844
'year': '2098 2099'.split()}
49-
z.write_event(i)
50-
self.assertEqual(i.comments, '')
51-
self.assertEqual(i.year, 2012)
45+
46+
tags = {
47+
'comments': 'from lame collection, ripped by eac',
48+
'year': 2012,
49+
}
50+
z.write_event(None, None, tags)
51+
self.assertEqual(tags['comments'], None)
52+
self.assertEqual(tags['year'], 2012)
5253

5354
def test_delete_replaygain_tag(self):
5455
path = self.create_mediafile_fixture()
@@ -70,6 +71,17 @@ def test_delete_replaygain_tag(self):
7071
self.assertIsNone(mediafile.rg_track_peak)
7172
self.assertIsNone(mediafile.rg_track_gain)
7273

74+
def test_do_not_change_database(self):
75+
item = self.add_item_fixture(year=2000)
76+
mediafile = MediaFile(item.path)
77+
78+
config['zero'] = {'fields': ['year']}
79+
self.load_plugins('zero')
80+
81+
item.write()
82+
self.assertEqual(item['year'], 2000)
83+
self.assertIsNone(mediafile.year)
84+
7385

7486
def suite():
7587
return unittest.TestLoader().loadTestsFromName(__name__)

0 commit comments

Comments
 (0)