]> kaliko git repositories - mpd-sima.git/blob - sima/plugins/core/uniq.py
969af6cf4c0d13d871469c19669b2bcfd020885e
[mpd-sima.git] / sima / plugins / core / uniq.py
1 # -*- coding: utf-8 -*-
2 # Copyright (c) 2013, 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     Deal with MPD options ‑ idle and repeat mode
22 """
23
24 # standard library import
25 from os import  getpid
26 from socket import getfqdn
27
28 # third parties components
29
30 # local import
31 from ...lib.plugin import Plugin
32
33
34 class Uniq(Plugin):
35     """
36     Publish presence on the MPD host message bus
37     """
38
39     def __init__(self, daemon):
40         Plugin.__init__(self, daemon)
41         self.capable = False
42         self.chan = 'mpd_sima:{0}.{1}'.format(getfqdn(), getpid())
43         self.channels = []
44         self.uniq = True
45         self.is_capable()
46         if not self.capable:
47             return
48         self.is_uniq()
49         self.sub_chan()
50
51     def is_capable(self):
52         if 'channels' in self.player.commands():
53             self.capable = True
54             return
55         self.log.warning('MPD does not provide client to client')
56
57     def get_channels(self):
58         return [chan for chan in self.player.channels() if
59                 chan.startswith('mpd_sima') and chan != self.chan]
60
61     def is_uniq(self):
62         channels = self.get_channels()
63         if channels:
64             self.log.warning('Another instance is queueing on this MPD host')
65             self.log.warning(' '.join(channels))
66             self.uniq = False
67
68     def sub_chan(self):
69         self.log.debug('Registering as {}'.format(self.chan))
70         self.player.subscribe(self.chan)
71
72     def callback_need_track(self):
73         if self.capable:
74             self.is_uniq()
75
76
77 # VIM MODLINE
78 # vim: ai ts=4 sw=4 sts=4 expandtab