]> kaliko git repositories - mpd-sima.git/blob - sima/plugins/core/uniq.py
1abdd62a8f492ee805f53f877afc4d34567f80ba
[mpd-sima.git] / sima / plugins / core / uniq.py
1 # -*- coding: utf-8 -*-
2 # Copyright (c) 2014 Jack Kaliko <kaliko@azylum.org>
3 #
4 #  This file is part of sima
5 #
6 #  sima is free software: you can redistribute it and/or modify
7 #  it under the terms of the GNU General Public License as published by
8 #  the Free Software Foundation, either version 3 of the License, or
9 #  (at your option) any later version.
10 #
11 #  sima is distributed in the hope that it will be useful,
12 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #  GNU General Public License for more details.
15 #
16 #  You should have received a copy of the GNU General Public License
17 #  along with sima.  If not, see <http://www.gnu.org/licenses/>.
18 #
19 #
20 """
21     Publish presence on the MPD host message bus
22
23     Notifies when concurrent instance run on the same host.
24 """
25
26 # standard library import
27 from os import  getpid
28 from socket import getfqdn
29
30 # third parties components
31
32 # local import
33 from ...lib.plugin import Plugin
34
35
36 class Uniq(Plugin):
37     """
38     Publish presence on the MPD host message bus
39     """
40
41     def __init__(self, daemon):
42         Plugin.__init__(self, daemon)
43         self.chan = 'mpd_sima:{0}.{1}'.format(getfqdn(), getpid())
44         self.channels = []
45         self.uniq = True
46
47     def start(self):
48         if not self.is_capable():
49             self.log.warning('MPD does not provide client to client')
50             return
51         self.is_uniq()
52         self.sub_chan()
53
54     def is_capable(self):
55         if {'channels', 'subscribe'}.issubset(set(self.player.commands())):
56             # Groove Basin compatibility
57             # For some reason Groove Basin have channels command but no
58             # subscribe command‽
59             return True
60
61     def get_channels(self):
62         return [chan for chan in self.player.channels() if
63                 chan.startswith('mpd_sima') and chan != self.chan]
64
65     def is_uniq(self):
66         channels = self.get_channels()
67         if channels:
68             self.log.warning('Another instance is queueing on this MPD host')
69             self.log.warning(' '.join(channels))
70             self.uniq = False
71
72     def sub_chan(self):
73         self.log.debug('Registering as {}'.format(self.chan))
74         self.player.subscribe(self.chan)
75
76     def callback_need_track(self):
77         if self.is_capable():
78             self.is_uniq()
79
80
81 # VIM MODLINE
82 # vim: ai ts=4 sw=4 sts=4 expandtab