[modules/mpd] update to latest API

This commit is contained in:
tobi-wan-kenobi 2020-04-29 20:29:51 +02:00
parent dc7577069f
commit 2cc463eb1a
2 changed files with 40 additions and 42 deletions

View file

@ -45,16 +45,16 @@ from collections import defaultdict
import string import string
import os import os
import bumblebee.util import core.module
import bumblebee.input import core.widget
import bumblebee.output import core.input
import bumblebee.engine import core.decorators
from bumblebee.output import scrollable import util.cli
class Module(bumblebee.engine.Module): class Module(core.module.Module):
def __init__(self, engine, config): def __init__(self, config, theme):
super(Module, self).__init__(engine, config, []) super().__init__(config, theme, [])
self._layout = self.parameter('layout', 'mpd.prev mpd.main mpd.next mpd.shuffle mpd.repeat') self._layout = self.parameter('layout', 'mpd.prev mpd.main mpd.next mpd.shuffle mpd.repeat')
@ -73,36 +73,36 @@ class Module(bumblebee.engine.Module):
widget_list = [] widget_list = []
widget_map = {} widget_map = {}
for widget_name in self._layout.split(): for widget_name in self._layout.split():
widget = bumblebee.output.Widget(name=widget_name) widget = core.widget.Widget(name=widget_name, module=self)
widget_list.append(widget) widget_list.append(widget)
if widget_name == 'mpd.prev': if widget_name == 'mpd.prev':
widget_map[widget] = {'button': bumblebee.input.LEFT_MOUSE, 'cmd': 'mpc prev' + self._hostcmd} widget_map[widget] = {'button': core.input.LEFT_MOUSE, 'cmd': 'mpc prev' + self._hostcmd}
elif widget_name == 'mpd.main': elif widget_name == 'mpd.main':
widget_map[widget] = {'button': bumblebee.input.LEFT_MOUSE, 'cmd': 'mpc toggle' + self._hostcmd} widget_map[widget] = {'button': core.input.LEFT_MOUSE, 'cmd': 'mpc toggle' + self._hostcmd}
widget.full_text(self.description) widget.full_text(self.description)
elif widget_name == 'mpd.next': elif widget_name == 'mpd.next':
widget_map[widget] = {'button': bumblebee.input.LEFT_MOUSE, 'cmd': 'mpc next' + self._hostcmd} widget_map[widget] = {'button': core.input.LEFT_MOUSE, 'cmd': 'mpc next' + self._hostcmd}
elif widget_name == 'mpd.shuffle': elif widget_name == 'mpd.shuffle':
widget_map[widget] = {'button': bumblebee.input.LEFT_MOUSE, 'cmd': 'mpc random' + self._hostcmd} widget_map[widget] = {'button': core.input.LEFT_MOUSE, 'cmd': 'mpc random' + self._hostcmd}
elif widget_name == 'mpd.repeat': elif widget_name == 'mpd.repeat':
widget_map[widget] = {'button': bumblebee.input.LEFT_MOUSE, 'cmd': 'mpc repeat' + self._hostcmd} widget_map[widget] = {'button': core.input.LEFT_MOUSE, 'cmd': 'mpc repeat' + self._hostcmd}
else: else:
raise KeyError('The mpd module does not support a {widget_name!r} widget'.format(widget_name=widget_name)) raise KeyError('The mpd module does not support a {widget_name!r} widget'.format(widget_name=widget_name))
self.widgets(widget_list) self.widgets(widget_list)
# Register input callbacks # Register input callbacks
for widget, callback_options in widget_map.items(): for widget, callback_options in widget_map.items():
engine.input.register_callback(widget, **callback_options) core.input.register(widget, **callback_options)
def hidden(self): def hidden(self):
return self._status is None return self._status is None
@scrollable @core.decorators.scrollable
def description(self, widget): def description(self, widget):
return string.Formatter().vformat(self._fmt, (), self._tags) return string.Formatter().vformat(self._fmt, (), self._tags)
def update(self, widgets): def update(self):
self._load_song() self._load_song()
def state(self, widget): def state(self, widget):
@ -118,30 +118,28 @@ class Module(bumblebee.engine.Module):
def _load_song(self): def _load_song(self):
info = '' info = ''
try: tags = ['name',
tags = ['name', 'artist',
'artist', 'album',
'album', 'albumartist',
'albumartist', 'comment',
'comment', 'composer',
'composer', 'date',
'date', 'originaldate',
'originaldate', 'disc',
'disc', 'genre',
'genre', 'performer',
'performer', 'title',
'title', 'track',
'track', 'time',
'time', 'file',
'file', 'id',
'id', 'prio',
'prio', 'mtime',
'mtime', 'mdate']
'mdate'] joinedtags = '\n'.join(['tag {0} %{0}%'.format(tag) for tag in tags])
joinedtags = '\n'.join(['tag {0} %{0}%'.format(tag) for tag in tags]) info = util.cli.execute('mpc -f "{}"{}'.format(joinedtags, self._hostcmd), ignore_errors=True)
info = bumblebee.util.execute('mpc -f ' + """ + joinedtags + """ + self._hostcmd)
except RuntimeError:
pass
self._tags = defaultdict(lambda: '') self._tags = defaultdict(lambda: '')
self._status = None self._status = None
for line in info.split('\n'): for line in info.split('\n'):

View file

@ -49,7 +49,7 @@ class Module(core.module.Module):
widget_list = [] widget_list = []
widget_map = {} widget_map = {}
for widget_name in self._layout.split(): for widget_name in self._layout.split():
widget = core.widget.Widget(name=widget_name) widget = core.widget.Widget(name=widget_name, module=self)
widget_list.append(widget) widget_list.append(widget)
self._cmd = 'cmus-remote' self._cmd = 'cmus-remote'
if self._server is not None: if self._server is not None: