X-Git-Url: http://git.kaliko.me/?a=blobdiff_plain;f=sima%2Fcore.py;h=d7a61369a38971634174483fb156a5eb2b85c37f;hb=24993cd99b847733894f57fd004ed52b5390244a;hp=2d8b960b23e876351a9e234358028d0951f81f2b;hpb=c1bda032095902bdcd183c530a9c4de28f3c828a;p=mpd-sima.git diff --git a/sima/core.py b/sima/core.py index 2d8b960..d7a6136 100644 --- a/sima/core.py +++ b/sima/core.py @@ -1,11 +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.0.b' -__author__ = 'kaliko jack' -__url__ = 'git://git.kaliko.me/sima.git' - import sys import time @@ -15,24 +29,37 @@ 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 = PlayerClient() # Player client + 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=40) + sys.exit(1) + self.short_history = deque(maxlen=60) + + def __get_player(self): + """Instanciate the player""" + host = self.config.get('MPD', 'host') + port = self.config.get('MPD', 'port') + pswd = self.config.get('MPD', 'password', fallback=None) + return PlayerClient(host, port, pswd) def add_history(self): self.short_history.appendleft(self.player.current) @@ -65,6 +92,12 @@ class Sima(object): 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')() + if pl_callback: + to_add.extend(pl_callback) for track in to_add: self.player.add(track) @@ -88,6 +121,12 @@ class Sima(object): 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 """ @@ -98,7 +137,6 @@ class Sima(object): 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): """ @@ -115,6 +153,7 @@ class Sima(object): except PlayerError as err: self.log.warning('Player error: %s' % err) self.reconnect_player() + del(self.changed) def loop(self): """Dispatching callbacks to plugins