An (outdated) collection of plugins for Qtile.
git clone https://github.com/m-col/qtools
Files | Refs | Readme

-rw-r--r-- qtools/mpc/mpc.py


      1 """
      2 Qtile plugin to control Music Player Daemon using musicpd or mpd2 library
      3 
      4 Example usage:
      5 
      6     import qtools.mpc
      7     mpc = qtools.mpc.Client()
      8     keys.extend([EzKey(k, v) for k, v in {
      9         '<XF86AudioPlay>':  mpc.lazy_toggle,
     10         '<XF86AudioNext>':  mpc.lazy_next,
     11         '<XF86AudioPrev>':  mpc.lazy_previous,
     12         '<XF86AudioPlay>':  mpc.lazy_stop,
     13     }.items()])
     14 
     15 """
     16 # pylint: disable=no-member,redefined-builtin
     17 
     18 from functools import wraps
     19 
     20 try:
     21     from musicpd import ConnectionError, MPDClient
     22 except ImportError:
     23     from mpd import ConnectionError, MPDClient
     24 from qtools import Notifier
     25 
     26 
     27 bodies = {
     28     'pause': 'Paused',
     29     'play': 'Playing',
     30     'stop': 'Stopped',
     31 }
     32 
     33 
     34 def _client_func(func):
     35     @wraps(func)
     36     def _inner(self, qtile=None):
     37         try:
     38             self.client.connect()
     39         except ConnectionError:
     40             pass
     41         self.show(func(self))
     42         self.client.disconnect()
     43     return _inner
     44 
     45 
     46 class Client(Notifier):
     47     """
     48     The host and port are 127.0.0.1 and 6600 by default but can be set by passing these
     49     when initiating the client.
     50 
     51     The notification timeout can be changed by setting Client.timeout to milliseconds
     52     (int) or -1, which then uses the notification server's default timeout.
     53     """
     54     defaults = [
     55         ('summary', 'Music', 'Notification summary.'),
     56         ('host', '127.0.0.1', 'IP address of MPD server.'),
     57         ('port', '6600', 'Port of MPD server.'),
     58     ]
     59 
     60     def __init__(self, **config):
     61         Notifier.__init__(self, **config)
     62         self.add_defaults(Client.defaults)
     63 
     64         self.client = MPDClient()
     65         self.client.host = self.host
     66         self.client.port = self.port
     67 
     68     @_client_func
     69     def toggle(self):
     70         if self.client.status()['state'] == 'play':
     71             self.client.pause()
     72         else:
     73             self.client.play()
     74         return bodies.get(self.client.status()['state'])
     75 
     76     @_client_func
     77     def next(self):
     78         self.client.next()
     79         current = self.client.currentsong()
     80         return f"{current['artist']} - {current['title']}"
     81 
     82     @_client_func
     83     def previous(self):
     84         self.client.previous()
     85         current = self.client.currentsong()
     86         return f"{current['artist']} - {current['title']}"
     87 
     88     @_client_func
     89     def stop(self):
     90         self.client.stop()
     91         return 'Stopped'
     92