]> kaliko git repositories - mpd-sima.git/blobdiff - sima/core.py
Some refactoring around Exceptions
[mpd-sima.git] / sima / core.py
index ed4e0e1115ef93a96b96048ef8b83780fc61ae21..83c5e910a54adae79fc860b199a6c9913792bbdd 100644 (file)
@@ -1,5 +1,5 @@
 # -*- coding: utf-8 -*-
-# Copyright (c) 2009-2015, 2020 kaliko <kaliko@azylum.org>
+# Copyright (c) 2009-2015, 2020, 2021 kaliko <kaliko@azylum.org>
 #
 #  This file is part of sima
 #
@@ -37,7 +37,7 @@ class Sima(Daemon):
     """
 
     def __init__(self, conf):
-        ## Set daemon
+        # Set daemon
         Daemon.__init__(self, conf.get('daemon', 'pidfile'))
         self.enabled = True
         self.config = conf
@@ -48,6 +48,7 @@ class Sima(Daemon):
         self._core_plugins = list()
         self.player = PlayerClient(conf)  # MPD client
         self.short_history = deque(maxlen=60)
+        self.changed = None
 
     def add_history(self):
         """Handle local, in memory, short history"""
@@ -80,7 +81,8 @@ class Sima(Daemon):
 
     @property
     def plugins(self):
-        return [plugin[1] for plugin in sorted(self._plugins, key=lambda pl: pl[0], reverse=True)]
+        return [plugin[1] for plugin in
+                sorted(self._plugins, key=lambda pl: pl[0], reverse=True)]
 
     def need_tracks(self):
         """Is the player in need for tracks"""
@@ -115,6 +117,8 @@ class Sima(Daemon):
         cycle : 5s 10s 1m 5m 20m 1h
         """
         sleepfor = [5, 10, 60, 300, 1200, 3600]
+        # reset change
+        self.changed = None
         while True:
             tmp = sleepfor.pop(0)
             sleepfor.append(tmp)
@@ -125,9 +129,6 @@ class Sima(Daemon):
             except PlayerError as err:
                 self.log.debug(err)
                 continue
-            except PlayerError as err:
-                #TODO: unhandled Player exceptions
-                self.log.warning('Unhandled player exception: %s', err)
             self.log.info('Got reconnected')
             break
         self.foreach_plugin('start')
@@ -171,17 +172,16 @@ class Sima(Daemon):
             except PlayerError as err:
                 self.log.warning('Player error: %s', err)
                 self.reconnect_player()
-                del self.changed
 
     def loop(self):
         """Dispatching callbacks to plugins
         """
         # hanging here until a monitored event is raised in the player
-        if getattr(self, 'changed', False): # first iteration exception
-            self.changed = self.player.monitor()
-        else:  # first iteration goes through else
+        if self.changed is None:  # first iteration goes through else
             self.changed = ['playlist', 'player', 'skipped']
-        self.log.debug('changed: %s', ', '.join(self.changed))
+        else:  # Wait for a change
+            self.changed = self.player.monitor()
+            self.log.debug('changed: %s', ', '.join(self.changed))
         if 'playlist' in self.changed:
             self.foreach_plugin('callback_playlist')
         if 'player' in self.changed or 'options' in self.changed: