X-Git-Url: https://git.kaliko.me/?a=blobdiff_plain;f=sima%2Fcore.py;h=7a17c5f8844923c3c590648afe4c344954ce5bcd;hb=4f1b17d6b72946b60f79b8c3f5fa5557710b9aac;hp=d7a61369a38971634174483fb156a5eb2b85c37f;hpb=24993cd99b847733894f57fd004ed52b5390244a;p=mpd-sima.git diff --git a/sima/core.py b/sima/core.py index d7a6136..7a17c5f 100644 --- a/sima/core.py +++ b/sima/core.py @@ -20,7 +20,6 @@ """Core Object dealing with plugins and player client """ -import sys import time from collections import deque @@ -47,11 +46,10 @@ class Sima(Daemon): self.plugins = list() self.player = self.__get_player() # Player client try: + self.log.info('Connecting MPD: {0}:{1}'.format(*self.player._mpd)) self.player.connect() except (PlayerError, PlayerUnHandledError) as err: - self.log.error('Fails to connect player: {}'.format(err)) - self.shutdown() - sys.exit(1) + self.log.warning('Player: {}'.format(err)) self.short_history = deque(maxlen=60) def __get_player(self): @@ -62,10 +60,11 @@ 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): - """Registers plubin in Sima instance...""" + """Registers plugin in Sima instance...""" self.plugins.append(plugin_class(self)) def foreach_plugin(self, method, *args, **kwds): @@ -75,6 +74,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 @@ -89,13 +89,13 @@ 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) for track in to_add: @@ -123,16 +123,20 @@ class Sima(Daemon): def hup_handler(self, signum, frame): self.log.warning('Caught a sighup!') - self.player.disconnect() + # Cleaning pending command + self.player.clean() self.foreach_plugin('shutdown') + self.player.disconnect() raise SigHup('SIGHUP caught!') def shutdown(self): """General shutdown method """ self.log.warning('Starting shutdown.') - self.player.disconnect() + # Cleaning pending command + self.player.clean() self.foreach_plugin('shutdown') + self.player.disconnect() self.log.info('The way is shut, it was made by those who are dead. ' 'And the dead keep it…') @@ -147,13 +151,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) + del self.changed def loop(self): """Dispatching callbacks to plugins