X-Git-Url: https://git.kaliko.me/?a=blobdiff_plain;f=sima%2Fcore.py;h=3532c88e951e1f06e13c563debcc64909f5ed84f;hb=194de8ab2773bd8e456590e3876a0aca1fe4d564;hp=f535a33d486e6e11e577cfffac75877b3ff37144;hpb=57621c64288a742232b379b53bfe5fce34959535;p=mpd-sima.git diff --git a/sima/core.py b/sima/core.py index f535a33..3532c88 100644 --- a/sima/core.py +++ b/sima/core.py @@ -1,12 +1,25 @@ # -*- coding: utf-8 -*- +# Copyright (c) 2009, 2010, 2011, 2013, 2014 Jack Kaliko +# +# 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 . +# +# """Core Object dealing with plugins and player client """ -__version__ = '0.12.0pr1' -__author__ = 'kaliko jack' -__url__ = 'git://git.kaliko.me/sima.git' - -import sys import time from collections import deque @@ -16,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 @@ -31,11 +45,6 @@ class Sima(Daemon): 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.short_history = deque(maxlen=60) def __get_player(self): @@ -46,10 +55,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): @@ -59,6 +69,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,13 +84,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: @@ -97,20 +108,32 @@ class Sima(Daemon): time.sleep(tmp) try: self.player.connect() - except PlayerError: + except PlayerError as err: + self.log.debug(err) continue except PlayerUnHandledError as err: #TODO: unhandled Player exceptions self.log.warning('Unhandled player exception: %s' % err) self.log.info('Got reconnected') break + self.foreach_plugin('start') + + def hup_handler(self, signum, frame): + self.log.warning('Caught a sighup!') + # 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…') @@ -119,18 +142,26 @@ class Sima(Daemon): def run(self): """ """ + try: + self.log.info('Connecting MPD: {0}:{1}'.format(*self.player._mpd)) + self.player.connect() + except (PlayerError, PlayerUnHandledError) as err: + self.log.warning('Player: {}'.format(err)) + self.reconnect_player() + self.foreach_plugin('start') while 42: try: self.loop() 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