Automatic logger level changes on import

Breaking changes: plugins should set set _import_stages instead of
import_stages. From outside the latter is replaced by import_stages().
This is because it is now wrapped with log level-getting & -setting
statements.
This commit is contained in:
Bruno Cauet 2015-01-05 12:35:01 +01:00
parent d38d264b6b
commit 427f7e7035
10 changed files with 27 additions and 13 deletions

View file

@ -18,6 +18,7 @@ import traceback
import inspect import inspect
import re import re
from collections import defaultdict from collections import defaultdict
from functools import wraps
import beets import beets
@ -51,7 +52,7 @@ class BeetsPlugin(object):
def __init__(self, name=None): def __init__(self, name=None):
"""Perform one-time plugin setup. """Perform one-time plugin setup.
""" """
self.import_stages = [] self._import_stages = []
self.name = name or self.__module__.split('.')[-1] self.name = name or self.__module__.split('.')[-1]
self.config = beets.config[self.name] self.config = beets.config[self.name]
if not self.template_funcs: if not self.template_funcs:
@ -63,7 +64,7 @@ class BeetsPlugin(object):
logger_name = '{0}.{1}'.format('beets', self.name) logger_name = '{0}.{1}'.format('beets', self.name)
self._log = logging.getLogger(logger_name) self._log = logging.getLogger(logger_name)
self._log.setLevel(logging.WARNING) self._log.setLevel(logging.INFO)
def commands(self): def commands(self):
"""Should return a list of beets.ui.Subcommand objects for """Should return a list of beets.ui.Subcommand objects for
@ -71,6 +72,20 @@ class BeetsPlugin(object):
""" """
return () return ()
def import_stages(self):
return [self._set_log_level(logging.WARNING, import_stage)
for import_stage in self._import_stages]
def _set_log_level(self, log_level, func):
@wraps(func)
def wrapper(*args, **kwargs):
old_log_level = self._log.getEffectiveLevel()
self._log.setLevel(log_level)
result = func(*args, **kwargs)
self._log.setLevel(old_log_level)
return result
return wrapper
def queries(self): def queries(self):
"""Should return a dict mapping prefixes to Query subclasses. """Should return a dict mapping prefixes to Query subclasses.
""" """
@ -353,8 +368,7 @@ def import_stages():
"""Get a list of import stage functions defined by plugins.""" """Get a list of import stage functions defined by plugins."""
stages = [] stages = []
for plugin in find_plugins(): for plugin in find_plugins():
if hasattr(plugin, 'import_stages'): stages += plugin.import_stages()
stages += plugin.import_stages
return stages return stages

View file

@ -365,7 +365,7 @@ class ConvertPlugin(BeetsPlugin):
u'never_convert_lossy_files': False, u'never_convert_lossy_files': False,
u'copy_album_art': False, u'copy_album_art': False,
}) })
self.import_stages = [self.auto_convert] self._import_stages = [self.auto_convert]
def commands(self): def commands(self):
cmd = ui.Subcommand('convert', help='convert to external location') cmd = ui.Subcommand('convert', help='convert to external location')

View file

@ -139,7 +139,7 @@ class EchonestMetadataPlugin(plugins.BeetsPlugin):
config['echonest']['apikey'].get(unicode) config['echonest']['apikey'].get(unicode)
if self.config['auto']: if self.config['auto']:
self.import_stages = [self.imported] self._import_stages = [self.imported]
def _echofun(self, func, **kwargs): def _echofun(self, func, **kwargs):
"""Wrapper for requests to the EchoNest API. Will retry up to """Wrapper for requests to the EchoNest API. Will retry up to

View file

@ -336,7 +336,7 @@ class FetchArtPlugin(plugins.BeetsPlugin):
self.maxwidth = self.config['maxwidth'].get(int) self.maxwidth = self.config['maxwidth'].get(int)
if self.config['auto']: if self.config['auto']:
# Enable two import hooks when fetching is enabled. # Enable two import hooks when fetching is enabled.
self.import_stages = [self.fetch_art] self._import_stages = [self.fetch_art]
self.register_listener('import_task_files', self.assign_art) self.register_listener('import_task_files', self.assign_art)
available_sources = list(SOURCES_ALL) available_sources = list(SOURCES_ALL)

View file

@ -128,7 +128,7 @@ class FtInTitlePlugin(plugins.BeetsPlugin):
help='drop featuring from artists and ignore title update') help='drop featuring from artists and ignore title update')
if self.config['auto']: if self.config['auto']:
self.import_stages = [self.imported] self._import_stages = [self.imported]
def commands(self): def commands(self):

View file

@ -36,7 +36,7 @@ class KeyFinderPlugin(BeetsPlugin):
u'overwrite': False, u'overwrite': False,
}) })
self.config['auto'].get(bool) self.config['auto'].get(bool)
self.import_stages = [self.imported] self._import_stages = [self.imported]
def commands(self): def commands(self):
cmd = ui.Subcommand('keyfinder', cmd = ui.Subcommand('keyfinder',

View file

@ -145,7 +145,7 @@ class LastGenrePlugin(plugins.BeetsPlugin):
"""Setup plugin from config options """Setup plugin from config options
""" """
if self.config['auto']: if self.config['auto']:
self.import_stages = [self.imported] self._import_stages = [self.imported]
self._genre_cache = {} self._genre_cache = {}

View file

@ -442,7 +442,7 @@ SOURCE_BACKENDS = {
class LyricsPlugin(plugins.BeetsPlugin): class LyricsPlugin(plugins.BeetsPlugin):
def __init__(self): def __init__(self):
super(LyricsPlugin, self).__init__() super(LyricsPlugin, self).__init__()
self.import_stages = [self.imported] self._import_stages = [self.imported]
self.config.add({ self.config.add({
'auto': True, 'auto': True,
'google_API_key': None, 'google_API_key': None,

View file

@ -105,7 +105,7 @@ class MusicBrainzCollectionPlugin(BeetsPlugin):
) )
self.config.add({'auto': False}) self.config.add({'auto': False})
if self.config['auto']: if self.config['auto']:
self.import_stages = [self.imported] self._import_stages = [self.imported]
def commands(self): def commands(self):
return [update_mb_collection_cmd] return [update_mb_collection_cmd]

View file

@ -601,7 +601,7 @@ class ReplayGainPlugin(BeetsPlugin):
def __init__(self): def __init__(self):
super(ReplayGainPlugin, self).__init__() super(ReplayGainPlugin, self).__init__()
self.import_stages = [self.imported] self._import_stages = [self.imported]
# default backend is 'command' for backward-compatibility. # default backend is 'command' for backward-compatibility.
self.config.add({ self.config.add({