Ugly formatting...

file attached
# -*- coding: iso-8859-1 -*-

# python modules
import os
import time
import string

# freevo modules
from plugins.idlebar import IdleBarPlugin
import plugin, config
import util.pymetar as pymetar
import mpdclient2

class PluginInterface(IdleBarPlugin):
	"""
	Display the song MPD is playing right now.
	If there's none nothing is shown here.
	"""

	def __init__(self, server='localhost', port=6600, password=None):
		IdleBarPlugin.__init__(self)
		self.plugin_name = 'idlebar.mpdnow'
		self.SERVER = server
		self.PORT = port
		self.PASSWORD = password
	
	def getsong(self):
		"""
		Ask MPD what song it's playing at the moment.
		Needs mpdclient2.py
		Returns a string (empty if nothing is playing)
		"""

		import mpdclient2
		try:
			m = mpdclient2.connect(host=self.SERVER, port=self.PORT)
			status=m.status()

			if status['state']=='play':
				song=m.currentsong()
				if hasattr(song,'artist') and hasattr(song,'title'):
					# ID3-Tags! Display Artist and Title 
					output=song['artist'].strip()+' - '+song['title'].strip()

				else:
					# No ID3 :( Using the filename instead, removing directories and file-ending
					try:
						file=song['file'].strip()
						file=file.split('/')
						file=file[-1].rsplit('.',1)
						output=file[0]

					except:
						# What could possibly go wrong?
						output=song['file'].strip()

			else:
				output=''
		except:
			output=''
		return output
	

	def draw(self, (type, object), x, osd):
		output = self.getsong()
		font  = osd.get_font('small0')

		try:
			# Splitting the Songname somewhere in the middle
			marker=len(output)/2
			while not output[marker].isspace():
				marker=marker-1
			line1=output[0:marker].strip()
			line2=output[marker:].strip()

		except:
			# Songname propably was too short (like 'Track23')
			line1=''
			line2=output.strip()

		width1 = font.stringsize(line1)
		width2 = font.stringsize(line2)
		# I wonder which one is the shortest line
		if width1>=width2: width=width1
		else: width=width2

		osd.write_text(line1, font, None, x + 15, osd.y + 30 - font.h, width, font.h, 'left', 'top')
		osd.write_text(line2, font, None, x + 15, osd.y + 55 - font.h, width, font.h, 'left', 'top')
		return width + 15
-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference 
Don't miss this year's exciting event. There's still time to save $100. 
Use priority code J8TL2D2. 
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
Freevo-devel mailing list
Freevo-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/freevo-devel

Reply via email to