Skip to content

Commit 71acf93

Browse files
committed
Merge pull request #1641 from adkow/zero-whitelist
Add whitelist feature to zero plugin
2 parents 13fede5 + d180d42 commit 71acf93

File tree

2 files changed

+45
-10
lines changed

2 files changed

+45
-10
lines changed

beetsplug/zero.py

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,26 +41,54 @@ def __init__(self):
4141

4242
self.config.add({
4343
'fields': [],
44+
'keep_fields': [],
4445
'update_database': False,
4546
})
4647

4748
self.patterns = {}
4849
self.warned = False
4950

50-
for field in self.config['fields'].as_str_seq():
51-
if field in ('id', 'path', 'album_id'):
52-
self._log.warn(u'field \'{0}\' ignored, zeroing '
53-
u'it would be dangerous', field)
54-
continue
51+
if self.config['fields']:
52+
self.validate_config('fields')
53+
for field in self.config['fields'].as_str_seq():
54+
self.set_pattern(field)
55+
56+
elif self.config['keep_fields']:
57+
self.validate_config('keep_fields')
58+
59+
for field in MediaFile.fields():
60+
if field in self.config['keep_fields'].as_str_seq():
61+
continue
62+
self.set_pattern(field)
63+
64+
# These fields should be preserved
65+
for key in ('id', 'path', 'album_id'):
66+
if key in self.patterns:
67+
del self.patterns[key]
68+
69+
def validate_config(self, mode):
70+
"""Check if fields written in config are correct."""
71+
if self.config['fields'] and self.config['keep_fields']:
72+
self._log.warn(u'cannot blacklist and whitelist at the same time')
73+
for field in self.config[mode].as_str_seq():
5574
if field not in MediaFile.fields():
5675
self._log.error(u'invalid field: {0}', field)
5776
continue
77+
if mode == 'fields' and field in ('id', 'path', 'album_id'):
78+
self._log.warn(u'field \'{0}\' ignored, zeroing '
79+
u'it would be dangerous', field)
80+
continue
5881

59-
try:
60-
self.patterns[field] = self.config[field].as_str_seq()
61-
except confit.NotFoundError:
62-
# Matches everything
63-
self.patterns[field] = True
82+
def set_pattern(self, field):
83+
"""Set a field in `self.patterns` to a string list corresponding to
84+
the configuration, or `True` if the field has no specific
85+
configuration.
86+
"""
87+
try:
88+
self.patterns[field] = self.config[field].as_str_seq()
89+
except confit.NotFoundError:
90+
# Matches everything
91+
self.patterns[field] = True
6492

6593
def import_task_choice_event(self, session, task):
6694
"""Listen for import_task_choice event."""

docs/plugins/zero.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ fields to nullify and the conditions for nullifying them:
1818
get the list of all available fields by running ``beet fields``. In
1919
addition, the ``images`` field allows you to remove any images
2020
embedded in the media file.
21+
* Set ``keep_fields`` to *invert* the logic of the plugin. Only these fields
22+
will be kept; other fields will be removed. Remember to set only
23+
``fields`` or ``keep_fields``, not both!
2124
* To conditionally filter a field, use ``field: [regexp, regexp]`` to specify
2225
regular expressions.
2326
* By default this plugin only affects files' tags ; the beets database is left
@@ -31,6 +34,10 @@ For example::
3134
genre: [rnb, 'power metal']
3235
update_database: true
3336

37+
The plugin can work in one of two modes. The first mode, the default,
38+
is a blacklist, where you choose the tags you want to remove. The second mode
39+
is a whitelist where you instead specify the tags you want to keep.
40+
3441
If a custom pattern is not defined for a given field, the field will be nulled
3542
unconditionally.
3643

0 commit comments

Comments
 (0)