]> kaliko git repositories - mpd-sima.git/blobdiff - sima/launch.py
Add priority to plugins
[mpd-sima.git] / sima / launch.py
index dbd15256569a76ab1fee96e495d04c1aa033f210..c2dad9e823df571361414e6a2e3bd7b9c490266c 100644 (file)
@@ -1,4 +1,22 @@
 # -*- coding: utf-8 -*-
+# Copyright (c) 2013, 2014, 2015 Jack Kaliko <kaliko@azylum.org>
+#
+#  This file is part of sima
+#
+#  sima is free software: you can redistribute it and/or modify
+#  it under the terms of the GNU General Public License as published by
+#  the Free Software Foundation, either version 3 of the License, or
+#  (at your option) any later version.
+#
+#  sima is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#  GNU General Public License for more details.
+#
+#  You should have received a copy of the GNU General Public License
+#  along with sima.  If not, see <http://www.gnu.org/licenses/>.
+#
+#
 """Sima
 """
 
@@ -16,6 +34,7 @@ from os.path import isfile
 # local import
 from . import core, info
 from .lib.logger import set_logger
+from .lib.meta import Meta
 from .lib.simadb import SimaDB
 from .utils.config import ConfMan
 from .utils.startopt import StartOpt
@@ -23,6 +42,7 @@ from .utils.utils import exception_log, SigHup
  # core plugins
 from .plugins.core.history import History
 from .plugins.core.mpdoptions import MpdOptions
+from .plugins.core.uniq import Uniq
 ##
 
 
@@ -31,23 +51,28 @@ def load_plugins(sima, source):
         sima:   sima.core.Sima instance
         source: ['internal', 'contrib']
     """
-    if not sima.config.get('sima', source ):
+    if not sima.config.get('sima', source):
         return
     logger = logging.getLogger('sima')
+    # TODO: Sanity check for "sima.config.get('sima', source)" ?
     for plugin in sima.config.get('sima', source).split(','):
         plugin = plugin.strip(' \n')
         module = 'sima.plugins.{0}.{1}'.format(source, plugin.lower())
         try:
             mod_obj = __import__(module, fromlist=[plugin])
         except ImportError as err:
-            logger.error('Failed to load plugin\'s module: {0} ({1})'.format(module, err))
+            logger.error('Failed to load plugin\'s module: ' +
+                         '{0} ({1})'.format(module, err))
             sima.shutdown()
+            sys.exit(1)
         try:
             plugin_obj = getattr(mod_obj, plugin)
         except AttributeError as err:
             logger.error('Failed to load plugin {0} ({1})'.format(plugin, err))
             sima.shutdown()
-        logger.info('Loading {0} plugin: {name} ({doc})'.format(source, **plugin_obj.info()))
+            sys.exit(1)
+        logger.info('Loading {0} plugin: {name} ({doc})'.format(
+            source, **plugin_obj.info()))
         sima.register_plugin(plugin_obj)
 
 
@@ -57,15 +82,13 @@ def start(sopt, restart=False):
     # set logger
     verbosity = sopt.options.get('verbosity', 'warning')
     logfile = sopt.options.get('logfile', None)
-    cli_loglevel = getattr(logging, verbosity.upper())
-    set_logger(level=verbosity, logfile=logfile)
-    logger = logging.getLogger('sima')
-    logger.setLevel(cli_loglevel)
+    set_logger(verbosity, logfile)
     # loads configuration
-    config = ConfMan(logger, sopt.options).config
-    logger.setLevel(getattr(logging,
-                    config.get('log', 'verbosity').upper()))  # pylint: disable=E1103
-
+    config = ConfMan(sopt.options).config
+    logfile = config.get('log', 'logfile')
+    verbosity = config.get('log', 'verbosity')
+    set_logger(verbosity, logfile)
+    logger = logging.getLogger('sima')
     logger.debug('Command line say: {0}'.format(sopt.options))
     # Create Database
     db_file = config.get('sima', 'db_file')
@@ -82,14 +105,23 @@ def start(sopt, restart=False):
     sima = core.Sima(config)
 
     # required core plugins
-    sima.register_plugin(History)
-    sima.register_plugin(MpdOptions)
+    core_plugins = [History, MpdOptions, Uniq]
+    for cplgn in core_plugins:
+        logger.debug('Register core {name} ({doc})'.format(**cplgn.info()))
+        sima.register_core_plugin(cplgn)
+    logger.debug('core loaded, prioriy: {}'.format(' > '.join(map(str, sima.core_plugins))))
 
     #  Loading internal plugins
     load_plugins(sima, 'internal')
 
     #  Loading contrib plugins
     load_plugins(sima, 'contrib')
+    logger.info('plugins loaded, prioriy: {}'.format(' > '.join(map(str, sima.plugins))))
+    #  Set use of MusicBrainzIdentifier
+    if not config.getboolean('sima', 'musicbrainzid'):
+        logger.info('Disabling MusicBrainzIdentifier')
+        Meta.use_mbid = False
+
     # Run as a daemon
     if config.getboolean('daemon', 'daemon'):
         if restart:
@@ -113,13 +145,14 @@ def run(sopt, restart=False):
     # pylint: disable=broad-except
     try:
         start(sopt, restart)
-    except SigHup as err:  # SigHup inherit from Exception
+    except SigHup:  # SigHup inherit from Exception
         run(sopt, True)
     except Exception:  # Unhandled exception
         exception_log()
 
 # Script starts here
 def main():
+    """Entry point"""
     nfo = dict({'version': info.__version__,
                  'prog': 'sima'})
     # StartOpt gathers options from command line call (in StartOpt().options)