My collection of plugins for the Qtile window manager.
git clone https://mcol.xyz/code/qtools
Log | Files | Refs | README

commit 52e9e3c95e26c68c06d2e4f2e9d82d3d3629b6fe
parent 41b4434008de9c21fcd14c9dfc4e8df9d0e49ef2
Author: mcol <mcol@posteo.net>
Date:   Fri, 10 Jan 2020 18:39:52 +0000

Make mpc plugin use Notifier base class

Diffstat:
Mmpc/mpc.py | 64++++++++++++++++++++++++++++------------------------------------
1 file changed, 28 insertions(+), 36 deletions(-)

diff --git a/mpc/mpc.py b/mpc/mpc.py @@ -3,44 +3,40 @@ Qtile plugin to control Music Player Daemon using musicpd or mpd2 library 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 @@ class Client: 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 @@ class Client: 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'