Add cleantags plugin
This commit is contained in:
42
beetsplug/cleantags.py
Normal file
42
beetsplug/cleantags.py
Normal 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]
|
||||
Reference in New Issue
Block a user