From 7854938788e0af521edd7199e40db796d3256351 Mon Sep 17 00:00:00 2001 From: kaliko Date: Sun, 1 Dec 2013 17:37:13 +0100 Subject: [PATCH] Allow choosing internal plugins from config file --- doc/examples/all_settings.cfg | 8 ++--- launch | 36 ++++++------------- setup.py | 4 ++- sima/plugins/{ => internal}/__init__.py | 0 sima/plugins/{ => internal}/crop.py | 2 +- .../{addhist.py => internal/history.py} | 2 +- sima/plugins/{ => internal}/lastfm.py | 6 ++-- .../{mpd.py => internal/mpdoptions.py} | 2 +- sima/plugins/{ => internal}/randomfallback.py | 4 +-- sima/utils/config.py | 2 ++ 10 files changed, 27 insertions(+), 39 deletions(-) rename sima/plugins/{ => internal}/__init__.py (100%) rename sima/plugins/{ => internal}/crop.py (94%) rename sima/plugins/{addhist.py => internal/history.py} (95%) rename sima/plugins/{ => internal}/lastfm.py (99%) rename sima/plugins/{mpd.py => internal/mpdoptions.py} (96%) rename sima/plugins/{ => internal}/randomfallback.py (95%) diff --git a/doc/examples/all_settings.cfg b/doc/examples/all_settings.cfg index 87621e8..bb5273b 100644 --- a/doc/examples/all_settings.cfg +++ b/doc/examples/all_settings.cfg @@ -86,7 +86,7 @@ depth = 1 # Possible values: # track : Will queue tracks from similar artists (default). # top : Will queue top tracks from similar artists. # NOT IMPLEMENTED # -# album : Will queue whole album from similar artists. # NOT IMPLEMENTED # +# album : Will queue whole album from similar artists. queue_mode = track ## SIMILARITY @@ -132,7 +132,7 @@ album_to_add = 1 ## PLUGINS # type: comma separated string list # for ex.: -# plugins = Scrobble, AwesomePlugin, +# contrib = Scrobble, AwesomePlugin, # ExperimentalTest,AnotherTest # # Plugins list declaration. @@ -140,8 +140,8 @@ album_to_add = 1 # "AwesomePlugin" declared here gets its configuration from the # "[AwesomePlugin]" or "[awesomeplugin]" section (case insensitive). # -#internals = -plugins = PlaceHolder +#internal = Crop, History, MpdOptions, Lastfm, RandomFallBack +contrib = PlaceHolder ## HISTORY_DURATION # type: integer (in hours) diff --git a/launch b/launch index 618e1bc..a4eb951 100755 --- a/launch +++ b/launch @@ -22,27 +22,19 @@ from sima.utils.config import ConfMan from sima.utils.startopt import StartOpt from sima.utils.utils import exception_log ## -# internal plugins -from sima.plugins.crop import Crop -from sima.plugins.addhist import History -from sima.plugins.lastfm import Lastfm -from sima.plugins.mpd import MpdOptions -from sima.plugins.randomfallback import RandomFallBack -# official plugins to start -PLUGINS = (Crop, History, MpdOptions, - Lastfm, RandomFallBack) - -def load_contrib_plugins(sima): - """Handles contrib/external plugins +def load_plugins(sima, source): + """Handles internal/external plugins + sima: sima.core.Sima instance + source: ['internal', 'contrib'] """ - if not sima.config.has_option('sima', 'plugins'): + if not sima.config.get('sima', source ): return logger = logging.getLogger('sima') - for plugin in sima.config.get('sima','plugins').split(','): + for plugin in sima.config.get('sima', source).split(','): plugin = plugin.strip(' \n') - module = 'sima.plugins.contrib.{}'.format(plugin.lower()) + module = 'sima.plugins.{0}.{1}'.format(source, plugin.lower()) try: mod_obj = __import__(module, fromlist=[plugin]) except ImportError as err: @@ -53,16 +45,10 @@ def load_contrib_plugins(sima): except AttributeError as err: logger.error('Failed to load plugin {0} ({1})'.format(plugin, err)) sima.shutdown() - logger.info('Loading contrib plugin: {name} ({doc})'.format(**plugin_obj.info())) + logger.info('Loading {0} plugin: {name} ({doc})'.format(source, **plugin_obj.info())) sima.register_plugin(plugin_obj) -def load_internal_plugins(sima): - """Handles contrib/external plugins - """ - raise NotImplementedError - - def main(): """Entry point, deal w/ CLI and starts application """ @@ -98,12 +84,10 @@ def main(): sima = core.Sima(config) # Loading internal plugins - for plugin in PLUGINS: - logger.info('Loading internal plugin: {name} ({doc})'.format(**plugin.info())) - sima.register_plugin(plugin) + load_plugins(sima, 'internal') # Loading contrib plugins - load_contrib_plugins(sima) + load_plugins(sima, 'contrib') # Run as a daemon if config.getboolean('daemon', 'daemon'): diff --git a/setup.py b/setup.py index 1211086..9e1d847 100755 --- a/setup.py +++ b/setup.py @@ -51,7 +51,9 @@ setup(name='sima', long_description=DESCRIPTION, classifiers=classifiers, install_requires=['distribute', 'python-musicpd'], - packages=['sima','sima.lib', 'sima.utils', 'sima.plugins', 'sima.plugins.contrib'], + packages=['sima','sima.lib', 'sima.utils', + 'sima.plugins.internal', + 'sima.plugins.contrib'], include_package_data=True, data_files=data_files, scripts=['launch'], diff --git a/sima/plugins/__init__.py b/sima/plugins/internal/__init__.py similarity index 100% rename from sima/plugins/__init__.py rename to sima/plugins/internal/__init__.py diff --git a/sima/plugins/crop.py b/sima/plugins/internal/crop.py similarity index 94% rename from sima/plugins/crop.py rename to sima/plugins/internal/crop.py index 6bfbf6d..8ea889e 100644 --- a/sima/plugins/crop.py +++ b/sima/plugins/internal/crop.py @@ -8,7 +8,7 @@ # third parties componants # local import -from ..lib.plugin import Plugin +from ...lib.plugin import Plugin class Crop(Plugin): """ diff --git a/sima/plugins/addhist.py b/sima/plugins/internal/history.py similarity index 95% rename from sima/plugins/addhist.py rename to sima/plugins/internal/history.py index e9a36e8..67414a8 100644 --- a/sima/plugins/addhist.py +++ b/sima/plugins/internal/history.py @@ -7,7 +7,7 @@ # third parties components # local import -from ..lib.plugin import Plugin +from ...lib.plugin import Plugin class History(Plugin): """ diff --git a/sima/plugins/lastfm.py b/sima/plugins/internal/lastfm.py similarity index 99% rename from sima/plugins/lastfm.py rename to sima/plugins/internal/lastfm.py index 18f86e4..b28d34e 100644 --- a/sima/plugins/lastfm.py +++ b/sima/plugins/internal/lastfm.py @@ -13,9 +13,9 @@ from hashlib import md5 # third parties componants # local import -from ..lib.plugin import Plugin -from ..lib.simafm import SimaFM, XmlFMHTTPError, XmlFMNotFound, XmlFMError -from ..lib.track import Track +from ...lib.plugin import Plugin +from ...lib.simafm import SimaFM, XmlFMHTTPError, XmlFMNotFound, XmlFMError +from ...lib.track import Track def cache(func): diff --git a/sima/plugins/mpd.py b/sima/plugins/internal/mpdoptions.py similarity index 96% rename from sima/plugins/mpd.py rename to sima/plugins/internal/mpdoptions.py index f7e1e0f..be47a40 100644 --- a/sima/plugins/mpd.py +++ b/sima/plugins/internal/mpdoptions.py @@ -8,7 +8,7 @@ # third parties components # local import -from ..lib.plugin import Plugin +from ...lib.plugin import Plugin class MpdOptions(Plugin): diff --git a/sima/plugins/randomfallback.py b/sima/plugins/internal/randomfallback.py similarity index 95% rename from sima/plugins/randomfallback.py rename to sima/plugins/internal/randomfallback.py index 4951c80..b8902ec 100644 --- a/sima/plugins/randomfallback.py +++ b/sima/plugins/internal/randomfallback.py @@ -9,8 +9,8 @@ import random # third parties componants # local import -from ..lib.plugin import Plugin -from ..lib.track import Track +from ...lib.plugin import Plugin +from ...lib.track import Track class RandomFallBack(Plugin): diff --git a/sima/utils/config.py b/sima/utils/config.py index 15c1a3d..5d62505 100644 --- a/sima/utils/config.py +++ b/sima/utils/config.py @@ -45,6 +45,8 @@ DEFAULT_CONF = { #'password': "", 'port': "6600"}, 'sima': { + 'internal': "Crop, History, MpdOptions, Lastfm, RandomFallBack", + 'contrib': "", 'user_db': "false", 'history_duration': "8", 'queue_length': "1", -- 2.39.5