1

Add cleantags plugin

This commit is contained in:
Kim Wittenburg
2018-09-03 21:44:22 +02:00
parent 73b3de8985
commit cd92f160ed

42
beetsplug/cleantags.py Normal file
View File

@@ -0,0 +1,42 @@
from mutagen import File
from beets.plugins import BeetsPlugin
from beets.util.confit import ConfigValueError
class CleanTagsPlugin(BeetsPlugin):
def __init__(self):
super(CleanTagsPlugin, self).__init__()
self.config.add({
'clean': True, # Whether to remove all tags before writing any new data.
'keep': [], # Tags to write to files.
'discard': [] # Tags to discard
})
self.register_listener('write', self.clear_metadata)
@property
def clean(self):
return self.config['clean']
@property
def keep_tags(self):
return self.config['keep'].as_str_seq()
@property
def discard_tags(self):
return self.config['discard'].as_str_seq()
def clear_metadata(self, item, path, tags):
if self.keep_tags and self.discard_tags:
raise ConfigValueError('Cannot specify both "keep" and "discard" for cleantags plugin.')
if self.clean:
file = File(path)
file.delete()
copy = tags.copy()
if self.keep_tags:
for tag in copy.keys():
if tag not in self.keep_tags:
del tags[tag]
elif self.discard_tags:
for tag in self.discard_tags:
del tags[tag]