bumblebee-status/modules/contrib/mpd.py

180 lines
6.3 KiB
Python
Raw Normal View History

2020-04-29 20:23:37 +02:00
# pylint: disable=C0111,R0903
# -*- coding: utf-8 -*-
"""Displays information about the current song in mpd.
Requires the following executable:
* mpc
Parameters:
* mpd.format: Format string for the song information.
Supported tags (see `man mpc` for additional information)
* {name}
* {artist}
* {album}
* {albumartist}
* {comment}
* {composer}
* {date}
* {originaldate}
* {disc}
* {genre}
* {performer}
* {title}
* {track}
* {time}
* {file}
* {id}
* {prio}
* {mtime}
* {mdate}
Additional tags:
* {position} - position of currently playing song
not to be confused with %position% mpc tag
* {duration} - duration of currently playing song
* {file1} - song file name without path prefix
if {file} = '/foo/bar.baz', then {file1} = 'bar.baz'
* {file2} - song file name without path prefix and extension suffix
if {file} = '/foo/bar.baz', then {file2} = 'bar'
* mpd.host: MPD host to connect to. (mpc behaviour by default)
* mpd.layout: Space-separated list of widgets to add. Possible widgets are the buttons/toggles mpd.prev, mpd.next, mpd.shuffle and mpd.repeat, and the main display with play/pause function mpd.main.
"""
from collections import defaultdict
import string
import os
2020-04-29 20:29:51 +02:00
import core.module
import core.widget
import core.input
import core.decorators
2020-04-29 20:23:37 +02:00
2020-04-29 20:29:51 +02:00
import util.cli
2020-04-29 20:23:37 +02:00
2020-04-29 20:29:51 +02:00
class Module(core.module.Module):
def __init__(self, config, theme):
super().__init__(config, theme, [])
2020-04-29 20:23:37 +02:00
2020-04-29 20:23:53 +02:00
self._layout = self.parameter('layout', 'mpd.prev mpd.main mpd.next mpd.shuffle mpd.repeat')
2020-04-29 20:23:37 +02:00
2020-04-29 20:23:53 +02:00
self._fmt = self.parameter('format', '{artist} - {title} {position}/{duration}')
2020-04-29 20:23:37 +02:00
self._status = None
self._shuffle = False
self._repeat = False
self._tags = defaultdict(lambda: '')
2020-04-29 20:23:53 +02:00
if not self.parameter('host'):
self._hostcmd = ''
2020-04-29 20:23:37 +02:00
else:
2020-04-29 20:23:53 +02:00
self._hostcmd = ' -h ' + self.parameter('host')
2020-04-29 20:23:37 +02:00
# Create widgets
widget_list = []
widget_map = {}
for widget_name in self._layout.split():
2020-04-29 20:29:51 +02:00
widget = core.widget.Widget(name=widget_name, module=self)
2020-04-29 20:23:37 +02:00
widget_list.append(widget)
2020-04-29 20:23:53 +02:00
if widget_name == 'mpd.prev':
2020-04-29 20:29:51 +02:00
widget_map[widget] = {'button': core.input.LEFT_MOUSE, 'cmd': 'mpc prev' + self._hostcmd}
2020-04-29 20:23:53 +02:00
elif widget_name == 'mpd.main':
2020-04-29 20:29:51 +02:00
widget_map[widget] = {'button': core.input.LEFT_MOUSE, 'cmd': 'mpc toggle' + self._hostcmd}
2020-04-29 20:23:37 +02:00
widget.full_text(self.description)
2020-04-29 20:23:53 +02:00
elif widget_name == 'mpd.next':
2020-04-29 20:29:51 +02:00
widget_map[widget] = {'button': core.input.LEFT_MOUSE, 'cmd': 'mpc next' + self._hostcmd}
2020-04-29 20:23:53 +02:00
elif widget_name == 'mpd.shuffle':
2020-04-29 20:29:51 +02:00
widget_map[widget] = {'button': core.input.LEFT_MOUSE, 'cmd': 'mpc random' + self._hostcmd}
2020-04-29 20:23:53 +02:00
elif widget_name == 'mpd.repeat':
2020-04-29 20:29:51 +02:00
widget_map[widget] = {'button': core.input.LEFT_MOUSE, 'cmd': 'mpc repeat' + self._hostcmd}
2020-04-29 20:23:37 +02:00
else:
2020-04-29 20:23:53 +02:00
raise KeyError('The mpd module does not support a {widget_name!r} widget'.format(widget_name=widget_name))
2020-04-29 20:23:37 +02:00
self.widgets(widget_list)
# Register input callbacks
for widget, callback_options in widget_map.items():
2020-04-29 20:29:51 +02:00
core.input.register(widget, **callback_options)
2020-04-29 20:23:37 +02:00
def hidden(self):
return self._status is None
2020-04-29 20:29:51 +02:00
@core.decorators.scrollable
2020-04-29 20:23:37 +02:00
def description(self, widget):
return string.Formatter().vformat(self._fmt, (), self._tags)
2020-04-29 20:29:51 +02:00
def update(self):
2020-04-29 20:23:37 +02:00
self._load_song()
def state(self, widget):
2020-04-29 20:23:53 +02:00
if widget.name == 'mpd.shuffle':
return 'shuffle-on' if self._shuffle else 'shuffle-off'
if widget.name == 'mpd.repeat':
return 'repeat-on' if self._repeat else 'repeat-off'
if widget.name == 'mpd.prev':
return 'prev'
if widget.name == 'mpd.next':
return 'next'
2020-04-29 20:23:37 +02:00
return self._status
def _load_song(self):
2020-04-29 20:23:53 +02:00
info = ''
2020-04-29 20:29:51 +02:00
tags = ['name',
'artist',
'album',
'albumartist',
'comment',
'composer',
'date',
'originaldate',
'disc',
'genre',
'performer',
'title',
'track',
'time',
'file',
'id',
'prio',
'mtime',
'mdate']
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)
2020-04-29 20:23:37 +02:00
self._tags = defaultdict(lambda: '')
self._status = None
2020-04-29 20:23:53 +02:00
for line in info.split('\n'):
if line.startswith('[playing]'):
self._status = 'playing'
elif line.startswith('[paused]'):
self._status = 'paused'
2020-04-29 20:23:37 +02:00
2020-04-29 20:23:53 +02:00
if line.startswith('['):
2020-04-29 20:23:37 +02:00
timer = line.split()[2]
2020-04-29 20:23:53 +02:00
position = timer.split('/')[0]
dur = timer.split('/')[1]
duration = dur.split(' ')[0]
2020-04-29 20:23:37 +02:00
self._tags.update({'position': position})
self._tags.update({'duration': duration})
2020-04-29 20:23:53 +02:00
if line.startswith('volume'):
value = line.split(' ', 2)[1:]
2020-04-29 20:23:37 +02:00
for option in value:
2020-04-29 20:23:53 +02:00
if option.startswith('repeat: on'):
2020-04-29 20:23:37 +02:00
self._repeat = True
2020-04-29 20:23:53 +02:00
elif option.startswith('repeat: off'):
2020-04-29 20:23:37 +02:00
self._repeat = False
2020-04-29 20:23:53 +02:00
elif option.startswith('random: on'):
2020-04-29 20:23:37 +02:00
self._shuffle = True
2020-04-29 20:23:53 +02:00
elif option.startswith('random: off'):
2020-04-29 20:23:37 +02:00
self._shuffle = False
2020-04-29 20:23:53 +02:00
if line.startswith('tag'):
key, value = line.split(' ', 2)[1:]
2020-04-29 20:23:37 +02:00
self._tags.update({key: value})
2020-04-29 20:23:53 +02:00
if key == 'file':
self._tags.update({'file1': os.path.basename(value)})
2020-04-29 20:23:37 +02:00
self._tags.update(
2020-04-29 20:23:53 +02:00
{'file2':
2020-04-29 20:23:37 +02:00
os.path.splitext(os.path.basename(value))[0]})
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4