Allowing the whitelist to be disabled

By default it is disabled, setting the value to the empty
string will use the built-in whitelist (same behaviour than
c14n).
This commit is contained in:
Fabrice Laporte 2014-04-27 22:26:09 +02:00
parent e399173f7e
commit 817b8cc96e

View file

@ -38,7 +38,6 @@ from beets import library
log = logging.getLogger('beets')
LASTFM = pylast.LastFMNetwork(api_key=plugins.LASTFM_KEY)
C14N_TREE = os.path.join(os.path.dirname(__file__), 'genres-tree.yaml')
PYLAST_EXCEPTIONS = (
pylast.WSError,
@ -113,12 +112,16 @@ def find_parents(candidate, branches):
# Main plugin logic.
WHITELIST = os.path.join(os.path.dirname(__file__), 'genres.txt')
C14N_TREE = os.path.join(os.path.dirname(__file__), 'genres-tree.yaml')
class LastGenrePlugin(plugins.BeetsPlugin):
def __init__(self):
super(LastGenrePlugin, self).__init__()
self.config.add({
'whitelist': os.path.join(os.path.dirname(__file__), 'genres.txt'),
'whitelist': None,
'min_weight': 10,
'count': 1,
'fallback': None,
@ -139,13 +142,20 @@ class LastGenrePlugin(plugins.BeetsPlugin):
self._genre_cache = {}
# Read the whitelist file.
# Read the whitelist file if enabled.
self.whitelist = set()
with open(self.config['whitelist'].as_filename()) as f:
for line in f:
line = line.decode('utf8').strip().lower()
if line and not line.startswith(u'#'):
self.whitelist.add(line)
wl_filename = self.config['whitelist'].get()
if wl_filename is not None:
wl_filename = wl_filename.strip()
if not wl_filename:
wl_filename = WHITELIST
wl_filename = normpath(wl_filename)
with open(wl_filename, 'r') as f:
# with open(self.config['whitelist'].as_filename()) as f:
for line in f:
line = line.decode('utf8').strip().lower()
if line and not line.startswith(u'#'):
self.whitelist.add(line)
# Read the genres tree for canonicalization if enabled.
self.c14n_branches = []