From 0ad417cc793c1e20e3874fbd0c8e06221ade9906 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kim=20Silkeb=C3=A6kken?= Date: Thu, 31 Jan 2013 10:53:33 +0100 Subject: [PATCH] Refactor now_playing segment slightly --- powerline/segments/common.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/powerline/segments/common.py b/powerline/segments/common.py index 7ea1ef74..f44b07b3 100644 --- a/powerline/segments/common.py +++ b/powerline/segments/common.py @@ -263,8 +263,8 @@ class NowPlayingSegment(object): } def __call__(self, player='mpd', format=u'{state_symbol} {artist} - {title} ({total})', *args, **kwargs): - update_func = getattr(self, 'player_{0}'.format(player)) - self.now_playing = { + player_func = getattr(self, 'player_{0}'.format(player)) + stats = { 'state': None, 'state_symbol': self.STATE_SYMBOLS['fallback'], 'album': None, @@ -273,10 +273,11 @@ class NowPlayingSegment(object): 'elapsed': None, 'total': None, } - updated = update_func(*args, **kwargs) - if not updated: + func_stats = player_func(*args, **kwargs) + if not func_stats: return None - return format.format(**self.now_playing) + stats.update(func_stats) + return format.format(**stats) @staticmethod def _run_cmd(cmd): @@ -300,7 +301,7 @@ class NowPlayingSegment(object): status = client.status() client.close() client.disconnect() - self.now_playing.update({ + return { 'state': status.get('state'), 'state_symbol': self.STATE_SYMBOLS.get(status.get('state')), 'album': now_playing.get('album'), @@ -308,19 +309,18 @@ class NowPlayingSegment(object): 'title': now_playing.get('title'), 'elapsed': '{0:.0f}:{1:02.0f}'.format(*divmod(float(status.get('elapsed', 0)), 60)), 'total': '{0:.0f}:{1:02.0f}'.format(*divmod(float(now_playing['time']), 60)), - }) + } except ImportError: now_playing = self._run_cmd(['mpc', 'current', '-f', '%album%\n%artist%\n%title%\n%time%', '-h', str(host), '-p', str(port)]) if not now_playing: return now_playing = now_playing.split('\n') - self.now_playing.update({ + return { 'album': now_playing[0], 'artist': now_playing[1], 'title': now_playing[2], 'total': now_playing[3], - }) - return True + } def player_spotify(self): try: @@ -339,13 +339,12 @@ class NowPlayingSegment(object): except dbus.exceptions.DBusException: return state = {'Playing': 'play', 'Paused': 'pause'}.get(state, None) - self.now_playing.update({ + return { 'state': state, 'state_symbol': self.STATE_SYMBOLS.get(state), 'album': str(info['xesam:album']), 'artist': str(info['xesam:artist'][0]), 'title': str(info['xesam:title']), 'total': '{0:.0f}:{1:02.0f}'.format(*divmod(float(info['mpris:length'] / 1e6), 60)), - }) - return True + } now_playing = NowPlayingSegment()