Skip to content

Commit

Permalink
Merge remote branch 'dieter/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
cortesi committed Nov 27, 2010
2 parents e473047 + a0acfef commit b985bf2
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
1 change: 1 addition & 0 deletions libqtile/widget/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from textbox import TextBox
from spacer import Spacer
from clock import Clock
from mpdwidget import Mpd
from sep import Sep
from prompt import Prompt
from systray import Systray
Expand Down
87 changes: 87 additions & 0 deletions libqtile/widget/mpdwidget.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# -*- coding: utf-8 -*-
# depends on python-mpd


# TODO: check if UI hangs in case of network issues and such
# TODO: python-mpd supports idle proto, can widgets be push instead of pull?
# TODO: a teardown hook so I can client.disconnect() ?
# TODO: some kind of templating to make shown info configurable
# TODO: best practice to handle failures? just write to stderr?

from .. import hook, bar, manager
import base
from mpd import MPDClient, CommandError
from socket import error as SocketError

class Mpd(base._TextBox):
"""
An mpd widget
"""
defaults = manager.Defaults(
("font", "Arial", "Mpd widget font"),
("fontsize", None, "Mpd widget pixel size. Calculated if None."),
("padding", None, "Mpd widget padding. Calculated if None."),
("background", "000000", "Background colour"),
("foreground", "ffffff", "Foreground colour")
)
def __init__(self, width=bar.CALCULATED, host='localhost', port=6600, password=False, **config):
"""
- host: host to connect to
- port: port to connect to
- password: password to use
- width: A fixed width, or bar.CALCULATED to calculate the width
automatically (which is recommended).
"""
self.host = host
self.port = port
self.password = password
base._TextBox.__init__(self, " ", width, **config)
self.client = MPDClient()
CON_ID = {'host': self.host, 'port': self.port}
if not self.mpdConnect(CON_ID):
print >> stderr, 'fail to connect MPD server.'
if self.password:
if not self.mpdAuth(self.password):
print >> stderr, 'Error trying to pass auth.'
client.disconnect()

def mpdConnect(self, con_id):
"""
Simple wrapper to connect MPD.
"""
try:
self.client.connect(**con_id)
except SocketError:
return False
return True

def mpdAuth(self, secret):
"""
Authenticate
"""
try:
self.client.password(secret)
except CommandError:
return False
return True

def _configure(self, qtile, bar):
#TODO: don't do this if connection failed
base._TextBox._configure(self, qtile, bar)
hook.subscribe.tick(self.update)

def update(self):
song = self.client.currentsong()
if song:
artist = ''
title = ''
if 'artist' in song:
artist = song['artist']
if 'title' in song:
title = song['title']
playing = "%s - %s" % (artist, title)
else:
playing = ' - '
if self.text != playing:
self.text = playing
self.bar.draw()

0 comments on commit b985bf2

Please sign in to comment.