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

Commit 52e9e3c95e26c68c06d2e4f2e9d82d3d3629b6fe
Parent: 41b4434008de9c21fcd14c9dfc4e8df9d0e49ef2
Author: mcol <mcol@posteo.net>
Date: 2020-01-10 18:39:52 +0000
Committer: mcol <mcol@posteo.net>
Committed: 2020-01-10 18:39:52 +0000

Make mpc plugin use Notifier base class

mpc/mpc.py Modified

@@ -3,44 +3,40 @@
 
 Example usage:
 
-    import qtools.mpc as mpc
-    mpc_client = mpc.Client()
+    import qtools.mpc
+    mpc = qtools.mpc.Client()
     keys.extend([EzKey(k, v) for k, v in {
-        '<XF86AudioPlay>':  lazy.function(mpc_local.toggle),
-        '<XF86AudioNext>':  lazy.function(mpc_local.next),
-        '<XF86AudioPrev>':  lazy.function(mpc_local.previous),
-        '<XF86AudioPlay>':  lazy.function(mpc_local.stop),
+        '<XF86AudioPlay>':  mpc.lazy_toggle,
+        '<XF86AudioNext>':  mpc.lazy_next,
+        '<XF86AudioPrev>':  mpc.lazy_previous,
+        '<XF86AudioPlay>':  mpc.lazy_stop,
     }.items()])
 
 """
 
+from functools import wraps
 
 try:
     from musicpd import ConnectionError, MPDClient
 except ImportError:
     from mpd import ConnectionError, MPDClient
+from qtools import Notifier
 
-import gi
-gi.require_version('Notify', '0.7')
-from gi.repository import Notify
 
-
-Notify.init('Music')
-
-
-def _wrap(func):
+def _client_func(func):
+    @wraps(func)
     def _inner(self, qtile):
         try:
             self.client.connect()
         except ConnectionError:
             pass
-        self.notifier.update('Music', func(self))
-        self.notifier.show()
+        self.update(func(self))
+        self.show()
         self.client.disconnect()
     return _inner
 
 
-class Client:
+class Client(Notifier):
     """
     The host and port are 127.0.0.1 and 6600 by default but can be set by passing these
     when initiating the client.
@@ -48,24 +44,20 @@
     The notification timeout can be changed by setting Client.timeout to milliseconds
     (int) or -1, which then uses the notification server's default timeout.
     """
-    def __init__(self, host='127.0.0.1', port='6600'):
-        self.client = MPDClient()
-        self.client.host = host
-        self.client.port = port
-        self.notifier = Notify.Notification.new('Music', 'body')
-        self._timeout = -1
-        self.notifier.set_timeout(self._timeout)
-
-    @property
-    def timeout(self):
-        return self._timeout
+    defaults = [
+        ('summary', 'Music', 'Notification summary.'),
+        ('host', '127.0.0.1', 'IP address of MPD server.'),
+        ('port', '6600', 'Port of MPD server.'),
+    ]
+    def __init__(self, **config):
+        Notifier.__init__(self, **config)
+        self.add_defaults(Client.defaults)
 
-    @timeout.setter
-    def timeout(self, timeout):
-        self.notifier.set_timeout(timeout)
-        self._timeout = timeout
+        self.client = MPDClient()
+        self.client.host = self.host
+        self.client.port = self.port
 
-    @_wrap
+    @_client_func
     def toggle(self):
         if self.client.status()['state'] == 'play':
             self.client.pause()
@@ -74,19 +66,19 @@
             self.client.play()
             return 'Playing'
 
-    @_wrap
+    @_client_func
     def next(self):
         self.client.next()
         current = self.client.currentsong()
         return f"{current['artist']} - {current['title']}"
 
-    @_wrap
+    @_client_func
     def previous(self):
         self.client.previous()
         current = self.client.currentsong()
         return f"{current['artist']} - {current['title']}"
 
-    @_wrap
+    @_client_func
     def stop(self):
         self.client.stop()
         return 'Stopped'