X-Git-Url: http://git.kaliko.me/?a=blobdiff_plain;f=sima%2Fcore.py;h=264d97e144e108f45a3794c955c70f232a3156da;hb=23309f235879380a5ef7db3a51bda4d12e31902e;hp=ad21db6f16da571b46d7f8b7ac68253c04421f7c;hpb=e09e76d41b2f041de935e9e884009e3187005ab9;p=mpd-sima.git diff --git a/sima/core.py b/sima/core.py index ad21db6..264d97e 100644 --- a/sima/core.py +++ b/sima/core.py @@ -1,12 +1,25 @@ # -*- coding: utf-8 -*- +# Copyright (c) 2009, 2010, 2011, 2013, 2014, 2015 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.0.b' -__author__ = 'kaliko jack' -__url__ = 'git://git.kaliko.me/sima.git' - -import sys import time from collections import deque @@ -15,26 +28,27 @@ from logging import getLogger 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(object): +class Sima(Daemon): """Main class, plugin and player management """ - def __init__(self, conf, dbfile): + def __init__(self, conf): + ## Set daemon + Daemon.__init__(self, conf.get('daemon', 'pidfile')) self.enabled = True self.config = conf - self.sdb = SimaDB(db_path=dbfile) + 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._plugins = list() + self._core_plugins = list() + self.player = self.__get_player() # Player client self.short_history = deque(maxlen=60) - def _get_player(self): + def __get_player(self): """Instanciate the player""" host = self.config.get('MPD', 'host') port = self.config.get('MPD', 'port') @@ -42,19 +56,40 @@ class Sima(object): return PlayerClient(host, port, pswd) def add_history(self): + """Handle local, in memory, short history""" self.short_history.appendleft(self.player.current) def register_plugin(self, plugin_class): - """Registers plubin in Sima instance...""" - self.plugins.append(plugin_class(self)) + """Registers plugin in Sima instance...""" + plgn = plugin_class(self) + prio = int(plgn.priority) + self._plugins.append((prio, plgn)) + + def register_core_plugin(self, plugin_class): + """Registers core plugins""" + plgn = plugin_class(self) + prio = int(plgn.priority) + self._core_plugins.append((prio, plgn)) def foreach_plugin(self, method, *args, **kwds): """Plugin's callbacks dispatcher""" + for plugin in self.core_plugins: + getattr(plugin, method)(*args, **kwds) for plugin in self.plugins: #self.log.debug('dispatching {0} to {1}'.format(method, plugin)) getattr(plugin, method)(*args, **kwds) + @property + def core_plugins(self): + return [plugin[1] for plugin in + sorted(self._core_plugins, key=lambda pl: pl[0], reverse=True)] + + @property + def plugins(self): + 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""" if not self.enabled: self.log.debug('Queueing disabled!') return False @@ -69,16 +104,12 @@ class Sima(object): def queue(self): to_add = list() for plugin in self.plugins: - 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 anything!') - for plugin in self.plugins: - self.log.info('calling fb for {}'.format(plugin)) - pl_callback = getattr(plugin, 'callback_need_track_fb')() - if pl_callback: - to_add.extend(pl_callback) + self.log.info('running {}'.format(plugin)) + pl_candidates = getattr(plugin, 'callback_need_track')() + if pl_candidates: + to_add.extend(pl_candidates) + if to_add: + break for track in to_add: self.player.add(track) @@ -94,41 +125,60 @@ class Sima(object): 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…') self.log.info('bye...') - sys.exit(0) 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