]> kaliko git repositories - mpd-sima.git/commitdiff
Plugins init call happen before player is connected
authorkaliko <kaliko@azylum.org>
Fri, 20 Jun 2014 17:41:41 +0000 (19:41 +0200)
committerkaliko <kaliko@azylum.org>
Fri, 20 Jun 2014 17:41:41 +0000 (19:41 +0200)
sima/client.py
sima/core.py
sima/launch.py
sima/lib/plugin.py
sima/plugins/core/uniq.py

index 8481f5cfa71d3e7fe35b9fecece2238ae24c7c0d..9eae523bd574e209d5e0c7ff8459e532b90d9370 100644 (file)
@@ -96,7 +96,7 @@ class PlayerClient(Player):
     TODO: handle exception in command not going through _client_wrapper() (ie.
           remove…)
     """
-    database = None  # sima database (history, blaclist)
+    database = None  # sima database (history, blacklist)
 
     def __init__(self, host="localhost", port="6600", password=None):
         super().__init__()
@@ -308,7 +308,7 @@ class PlayerClient(Player):
             self.log.warning('pending commands: {}'.format(self._client._pending))
 
     def remove(self, position=0):
-        self._client.delete(position)
+        self.delete(position)
 
     def add(self, track):
         """Overriding MPD's add method to accept add signature with a Track
@@ -321,7 +321,7 @@ class PlayerClient(Player):
 
     @property
     def state(self):
-        return str(self._client.status().get('state'))
+        return str(self.status().get('state'))
 
     @property
     def current(self):
index 7a17c5f8844923c3c590648afe4c344954ce5bcd..3532c88e951e1f06e13c563debcc64909f5ed84f 100644 (file)
@@ -45,11 +45,6 @@ class Sima(Daemon):
         self.log = getLogger('sima')
         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.warning('Player: {}'.format(err))
         self.short_history = deque(maxlen=60)
 
     def __get_player(self):
@@ -113,13 +108,15 @@ 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!')
@@ -145,6 +142,13 @@ 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()
index b03a41d65e78d85f675378b310bbd747e58d7174..03baa4fa5e9af85b68b9b66a91152aa17aa8e9b6 100644 (file)
@@ -113,6 +113,7 @@ def start(sopt, restart=False):
 
     #  Loading contrib plugins
     load_plugins(sima, 'contrib')
+
     # Run as a daemon
     if config.getboolean('daemon', 'daemon'):
         if restart:
index 755aadf93efbfd514c39daad003bd5b71cb23f16..3424ce3d4e6f4e3de3cd1a6bfb03ce84a0a06f79 100644 (file)
@@ -64,6 +64,13 @@ class Plugin:
         #    self.log.debug('Got config for {0}: {1}'.format(self,
         #                                                    self.plugin_conf))
 
+    def start(self):
+        """
+        Called when the daemon().run() is called.
+        ie. right after the player has connected successfully.
+        """
+        pass
+
     def callback_player(self):
         """
         Called on player changes, stopped, paused, skipped
index 969af6cf4c0d13d871469c19669b2bcfd020885e..b61afa616278d2aec52ebac5d348a3b80e4666e8 100644 (file)
@@ -1,5 +1,5 @@
 # -*- coding: utf-8 -*-
-# Copyright (c) 2013, 2014 Jack Kaliko <kaliko@azylum.org>
+# Copyright (c) 2014 Jack Kaliko <kaliko@azylum.org>
 #
 #  This file is part of sima
 #
@@ -18,7 +18,9 @@
 #
 #
 """
-    Deal with MPD options ‑ idle and repeat mode
+    Publish presence on the MPD host message bus
+
+    Notifies when concurrent instance run on the same host.
 """
 
 # standard library import
@@ -38,21 +40,20 @@ class Uniq(Plugin):
 
     def __init__(self, daemon):
         Plugin.__init__(self, daemon)
-        self.capable = False
         self.chan = 'mpd_sima:{0}.{1}'.format(getfqdn(), getpid())
         self.channels = []
         self.uniq = True
-        self.is_capable()
-        if not self.capable:
+
+    def start(self):
+        if not self.is_capable():
+            self.log.warning('MPD does not provide client to client')
             return
         self.is_uniq()
         self.sub_chan()
 
     def is_capable(self):
         if 'channels' in self.player.commands():
-            self.capable = True
-            return
-        self.log.warning('MPD does not provide client to client')
+            return True
 
     def get_channels(self):
         return [chan for chan in self.player.channels() if
@@ -70,7 +71,7 @@ class Uniq(Plugin):
         self.player.subscribe(self.chan)
 
     def callback_need_track(self):
-        if self.capable:
+        if self.is_capable():
             self.is_uniq()