#!/usr/bin/env python """A quick hack which started out as a mpd client to update the status bar, turning into a general purpose status bar updater thing, slowly. Motivated by an irrational hatred of seeing the wmii status script churn, spinning new processes left and right just to update the time. TODO: Make the mpd support optional. Make the whole thing slightly 'plugin-anble' Maybe. Using: #status & /bin/kill `cat ~/.wmii-4/.pystatus.pid` pystatus & """ import time import os try: from mpdclient import MpdController, MpdNoResponseError except ImportError: print 'install http://www.musicpd.org/py-libmpdclient.shtml' raise SystemExit WMIIDIR = os.environ['HOME'] + '/.wmii-4' PIDFILE = WMIIDIR+'/.pystatus.pid' mpd_state = { "1" : '.', # stopped "2" : '>', # playing "3" : '|', # paused } def wmii_status_write(notice): """(string) """ os.system('echo "%s" | wmiir write /rbar/status' % notice) def format_datetime(): return time.strftime(' | %-I:%M %p %A %B %-d', time.localtime()) class Status: def __init__(self): self.mpc = MpdController(port=6600) self.last_status_bar = '' def format_load_average(self): return ' | %.2f %.2f %.2f' % os.getloadavg() def format_mpd_status(self): try: status = self.mpc.getStatus() except MpdNoResponseError: try: # one second chance self.mpd.__init__() except: raise SystemExit status = self.mpc.getStatus() song = self.mpc.getCurrentSong() song_num = status.song playlist_length = status.playlistLength state = status.state return '%s %s - %s %s (%s/%s)' % (mpd_state[str(state)], song.title.strip(), song.artist.strip(), song.album.strip(), song_num, playlist_length) def update_bar(self): """go into a loop... and keep on doing it. """ open(PIDFILE,'w').write("%s\n" % os.getpid()) while 1: current = self.format_mpd_status() + \ self.format_load_average() + \ format_datetime() if current <> self.last_status_bar: wmii_status_write(current) self.last_status_bar = current time.sleep(1) if __name__ == '__main__': status = Status() # below is only here for testing - no consequence in # leaving in or out for 'real' use wmii_status_write(status.format_mpd_status() + format_datetime()) # do it, do it forever. status.update_bar()