From 58d9a775cc4e4c9ae46870d2b1fa41fd4e85a315 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Thu, 31 Jan 2013 12:13:19 -0800 Subject: [PATCH] remove singleton enforcement from plugins The plugin system itself now enforces single instances. --- beets/plugins.py | 2 ++ beetsplug/ihate.py | 16 +++------------- beetsplug/the.py | 13 ++----------- beetsplug/zero.py | 22 ++++++---------------- test/test_zero.py | 6 ------ 5 files changed, 13 insertions(+), 46 deletions(-) diff --git a/beets/plugins.py b/beets/plugins.py index 69609bec4..44e94e7df 100755 --- a/beets/plugins.py +++ b/beets/plugins.py @@ -44,6 +44,8 @@ class BeetsPlugin(object): self.import_stages = [] self.name = name or self.__module__.split('.')[-1] self.config = beets.config[self.name] + self.template_funcs = {} + self.template_fields = {} def commands(self): """Should return a list of beets.ui.Subcommand objects for diff --git a/beetsplug/ihate.py b/beetsplug/ihate.py index f9adf4017..d385f2d18 100644 --- a/beetsplug/ihate.py +++ b/beetsplug/ihate.py @@ -17,7 +17,6 @@ import re import logging from beets.plugins import BeetsPlugin -from beets import config from beets.importer import action @@ -39,14 +38,10 @@ class IHatePlugin(BeetsPlugin): skip_album = [] skip_whitelist = [] - def __new__(cls, *args, **kwargs): - if cls._instance is None: - cls._instance = super(IHatePlugin, - cls).__new__(cls, *args, **kwargs) - return cls._instance - def __init__(self): super(IHatePlugin, self).__init__() + self.register_listener('import_task_choice', + self.import_task_choice_event) self.config.add({ 'warn_genre': [], 'warn_artist': [], @@ -98,7 +93,7 @@ class IHatePlugin(BeetsPlugin): ('warn_genre', 'warn_artist', 'warn_album', 'skip_genre', 'skip_artist', 'skip_album')) - def import_task_choice_event(self, task): + def import_task_choice_event(self, session, task): if task.choice_flag == action.APPLY: if self.job_to_do(): self._log.debug('[ihate] processing your hate') @@ -122,8 +117,3 @@ class IHatePlugin(BeetsPlugin): self._log.debug('[ihate] nothing to do') else: self._log.debug('[ihate] user made a decision, nothing to do') - - -@IHatePlugin.listen('import_task_choice') -def ihate_import_task_choice(task, session): - IHatePlugin().import_task_choice_event(task) diff --git a/beetsplug/the.py b/beetsplug/the.py index 1f2b0dd85..fc397ac24 100644 --- a/beetsplug/the.py +++ b/beetsplug/the.py @@ -36,15 +36,11 @@ class ThePlugin(BeetsPlugin): strip = False patterns = [] - def __new__(cls, *args, **kwargs): - if cls._instance is None: - cls._instance = super(ThePlugin, - cls).__new__(cls, *args, **kwargs) - return cls._instance - def __init__(self): super(ThePlugin, self).__init__() + self.template_funcs['the'] = self.the_template_func + self.config.add({ 'the': True, 'a': True, @@ -108,8 +104,3 @@ class ThePlugin(BeetsPlugin): return r else: return u'' - -@ThePlugin.template_func('the') -def func_the(text): - """Provides beets template function %the""" - return ThePlugin().the_template_func(text) diff --git a/beetsplug/zero.py b/beetsplug/zero.py index b6460a554..947cf518f 100644 --- a/beetsplug/zero.py +++ b/beetsplug/zero.py @@ -30,15 +30,14 @@ class ZeroPlugin(BeetsPlugin): _instance = None _log = logging.getLogger('beets') - def __new__(cls, *args, **kwargs): - if cls._instance is None: - cls._instance = super(ZeroPlugin, - cls).__new__(cls, *args, **kwargs) - return cls._instance - def __init__(self): super(ZeroPlugin, self).__init__() + # Listeners. + self.register_listener('write', self.write_event) + self.register_listener('import_task_choice', + self.import_task_choice_event) + self.config.add({ 'fields': [], }) @@ -57,7 +56,7 @@ class ZeroPlugin(BeetsPlugin): except confit.NotFoundError: self.patterns[f] = [u''] - def import_task_choice_event(self, task): + def import_task_choice_event(self, session, task): """Listen for import_task_choice event.""" if task.choice_flag == action.ASIS and not self.warned: self._log.warn(u'[zero] cannot zero in \"as-is\" mode') @@ -95,12 +94,3 @@ class ZeroPlugin(BeetsPlugin): setattr(item, fn, type(fval)()) self._log.debug(u'[zero] {0}={1}' .format(fn, getattr(item, fn))) - - -@ZeroPlugin.listen('import_task_choice') -def zero_choice(session, task): - ZeroPlugin().import_task_choice_event(task) - -@ZeroPlugin.listen('write') -def zero_write(item): - ZeroPlugin().write_event(item) diff --git a/test/test_zero.py b/test/test_zero.py index 7732ca4e9..2cc9bcf2e 100644 --- a/test/test_zero.py +++ b/test/test_zero.py @@ -6,12 +6,6 @@ from beetsplug.zero import ZeroPlugin class ZeroPluginTest(unittest.TestCase): - - def test_singleton(self): - z1 = ZeroPlugin() - z2 = ZeroPlugin() - self.assertTrue(z1 is z2) - def test_no_patterns(self): v = {'comments' : 'test comment', 'day' : 13,