]> kaliko git repositories - mpd-sima.git/blobdiff - sima/core.py
Fixed blacklisting in track mode
[mpd-sima.git] / sima / core.py
index 87501e83b7d8bc60a616c252229f401d3bfc571e..634db4b1cc06c83e651e99f88a9c0c23fd949afa 100644 (file)
@@ -1,13 +1,25 @@
 # -*- coding: utf-8 -*-
+# Copyright (c) 2009, 2010, 2011, 2013, 2014 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/>.
+#
+#
 """Core Object dealing with plugins and player client
 """
 
-__version__ = '0.12.0.b'
-__author__ = 'kaliko jack'
-__url__ = 'git://git.kaliko.me/sima.git'
-
-import random
-import sys
 import time
 
 from collections import deque
@@ -17,6 +29,7 @@ from .client import PlayerClient
 from .client import PlayerError, PlayerUnHandledError
 from .lib.simadb import SimaDB
 from .lib.daemon import Daemon
+from .utils.utils import SigHup
 
 class Sima(Daemon):
     """Main class, plugin and player management
@@ -28,14 +41,14 @@ class Sima(Daemon):
         self.enabled = True
         self.config = conf
         self.sdb = SimaDB(db_path=conf.get('sima', 'db_file'))
+        PlayerClient.database = self.sdb
         self.log = getLogger('sima')
         self.plugins = list()
         self.player = self.__get_player()  # Player client
         try:
             self.player.connect()
         except (PlayerError, PlayerUnHandledError) as err:
-            self.log.error('Fails to connect player: {}'.format(err))
-            self.shutdown()
+            self.log.warning('Player: {}'.format(err))
         self.short_history = deque(maxlen=60)
 
     def __get_player(self):
@@ -46,6 +59,7 @@ class Sima(Daemon):
         return PlayerClient(host, port, pswd)
 
     def add_history(self):
+        """Handle local short history"""
         self.short_history.appendleft(self.player.current)
 
     def register_plugin(self, plugin_class):
@@ -59,6 +73,7 @@ class Sima(Daemon):
             getattr(plugin, method)(*args, **kwds)
 
     def need_tracks(self):
+        """Is the player in need for tracks"""
         if not self.enabled:
             self.log.debug('Queueing disabled!')
             return False
@@ -73,16 +88,15 @@ class Sima(Daemon):
     def queue(self):
         to_add = list()
         for plugin in self.plugins:
-            pl_callback =  getattr(plugin, 'callback_need_track')()
+            pl_callback = getattr(plugin, 'callback_need_track')()
             if pl_callback:
                 to_add.extend(pl_callback)
         if not to_add:
             self.log.warning('Queue plugins returned nothing!')
             for plugin in self.plugins:
-                pl_callback =  getattr(plugin, 'callback_need_track_fb')()
+                pl_callback = getattr(plugin, 'callback_need_track_fb')()
                 if pl_callback:
                     to_add.extend(pl_callback)
-        random.shuffle(to_add)
         for track in to_add:
             self.player.add(track)
 
@@ -106,6 +120,12 @@ class Sima(Daemon):
             self.log.info('Got reconnected')
             break
 
+    def hup_handler(self, signum, frame):
+        self.log.warning('Caught a sighup!')
+        self.player.disconnect()
+        self.foreach_plugin('shutdown')
+        raise SigHup('SIGHUP caught!')
+
     def shutdown(self):
         """General shutdown method
         """
@@ -116,7 +136,6 @@ class Sima(Daemon):
         self.log.info('The way is shut, it was made by those who are dead. '
                       'And the dead keep it…')
         self.log.info('bye...')
-        sys.exit(0)
 
     def run(self):
         """
@@ -127,12 +146,13 @@ class Sima(Daemon):
             except PlayerUnHandledError as err:
                 #TODO: unhandled Player exceptions
                 self.log.warning('Unhandled player exception: {}'.format(err))
-                del(self.player)
+                del self.player
                 self.player = PlayerClient()
                 time.sleep(10)
             except PlayerError as err:
                 self.log.warning('Player error: %s' % err)
                 self.reconnect_player()
+                del self.changed
 
     def loop(self):
         """Dispatching callbacks to plugins