From dfc2dc3d2a2ed5c13223beae97976004122fdd83 Mon Sep 17 00:00:00 2001 From: Vasiliy Horbachenko Date: Sun, 25 May 2014 12:58:51 +0300 Subject: rename mpd module to mpd_status to avoid conflict --- modules/mpd_status.py | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 modules/mpd_status.py (limited to 'modules/mpd_status.py') diff --git a/modules/mpd_status.py b/modules/mpd_status.py new file mode 100644 index 0000000..e94872c --- /dev/null +++ b/modules/mpd_status.py @@ -0,0 +1,112 @@ +from mpd import (MPDClient, CommandError) +from socket import error as SocketError +import time +import re + +""" +Mpd - module to show information from mpd to your's bar! Settings listed above. +DEPENDENCIES: python-mpd2 (NOT python2-mpd2): + # pip install python-mpd2 + +@author shadowprince +@version 1.0 +@license Eclipse Public License +""" + +# mpd settings +HOST = "localhost" +PORT = "6600" +PASSWORD = None + +# time to update +CACHED_TIME = 1 + +# position +POSITION = 1 + +# if text length will be greater - it'll shrink it +MAX_WIDTH = 120 + +# hide any indicator, if +HIDE_WHEN_PAUSED = False +HIDE_WHEN_STOPPED = True + +# state characters (or strings). Actual of them replaces {state} placeholder in STRFORMAT +STATE_CHARACTERS = { + "pause": "[pause]", + "play": "[play]", + "stop": "[stop]", +} + +""" format of result string +can contain: + {state} - current state from STATE_CHARACTERS + Track information: + {track}, {artist}, {title}, {time}, {album}, {pos} + In additional, information about next track also comes in, in analogue with current, but with next_ prefix, + like {next_title} +""" +STRFORMAT = "{state} №{pos}. {artist} - {title} [{time}] | {next_title}" + +class Py3status: + def __init__(self, *args, **kwargs): + self.text = "" + super(Py3status).__init__(*args, **kwargs) + + def currentTrack(self, i3status_output_json, i3status_config): + try: + c = MPDClient() + c.connect(host=HOST, port=PORT) + if PASSWORD: + c.password(PASSWORD) + + status = c.status() + song = int(status.get("song", 0)) + next_song = int(status.get("nextsong", 0)) + + if (status["state"] == "pause" and HIDE_WHEN_PAUSED) or (status["state"] == "stop" and HIDE_WHEN_STOPPED): + text = "" + else: + try: + song = c.playlistinfo()[song] + song["time"] = "{0:.2f}".format(int(song.get("time", 1)) / 60) + except IndexError: + song = {} + + try: + next_song = c.playlistinfo()[next_song] + except IndexError: + next_song = {} + + format_args = song + format_args["state"] = STATE_CHARACTERS.get(status.get("state", None)) + for k, v in next_song.items(): + format_args["next_{}".format(k)] = v + + text = STRFORMAT + for k, v in format_args.items(): + text = text.replace("{" + k + "}", v) + + for sub in re.findall(r"{\S+?}", text): + text = text.replace(sub, "") + except SocketError: + text = "Failed to connect to mpd!" + except CommandError: + text = "Failed to authenticate to mpd!" + c.disconnect() + + if len(text) > MAX_WIDTH: + text = text[-MAX_WIDTH-3:] + "..." + + if self.text != text: + transformed = True + self.text = text + else: + transformed = False + + return (POSITION, { + 'transformed': transformed, + 'full_text': self.text, + 'name': 'scratchpad-count', + 'cached_until': time.time() + CACHED_TIME, + }) -- cgit v1.3 From 7e88e29f9dc1bae8b7eef767fd6358143a185ae2 Mon Sep 17 00:00:00 2001 From: Ultrabug Date: Tue, 27 May 2014 12:29:49 +0200 Subject: PEP8 and normalization of mpd_status module --- modules/mpd_status.py | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) (limited to 'modules/mpd_status.py') diff --git a/modules/mpd_status.py b/modules/mpd_status.py index e94872c..28e99dc 100644 --- a/modules/mpd_status.py +++ b/modules/mpd_status.py @@ -1,15 +1,18 @@ +# -*- coding: utf-8 -*- + +import re + from mpd import (MPDClient, CommandError) from socket import error as SocketError -import time -import re +from time import time """ Mpd - module to show information from mpd to your's bar! Settings listed above. -DEPENDENCIES: python-mpd2 (NOT python2-mpd2): +Reequires + - python-mpd2 (NOT python2-mpd2) # pip install python-mpd2 @author shadowprince -@version 1.0 @license Eclipse Public License """ @@ -19,10 +22,10 @@ PORT = "6600" PASSWORD = None # time to update -CACHED_TIME = 1 +CACHE_TIMEOUT = 1 # position -POSITION = 1 +POSITION = 0 # if text length will be greater - it'll shrink it MAX_WIDTH = 120 @@ -39,7 +42,7 @@ STATE_CHARACTERS = { } """ format of result string -can contain: +can contain: {state} - current state from STATE_CHARACTERS Track information: {track}, {artist}, {title}, {time}, {album}, {pos} @@ -48,13 +51,13 @@ can contain: """ STRFORMAT = "{state} №{pos}. {artist} - {title} [{time}] | {next_title}" + class Py3status: - def __init__(self, *args, **kwargs): - self.text = "" - super(Py3status).__init__(*args, **kwargs) + def __init__(self): + self.text = '' def currentTrack(self, i3status_output_json, i3status_config): - try: + try: c = MPDClient() c.connect(host=HOST, port=PORT) if PASSWORD: @@ -104,9 +107,11 @@ class Py3status: else: transformed = False - return (POSITION, { - 'transformed': transformed, - 'full_text': self.text, - 'name': 'scratchpad-count', - 'cached_until': time.time() + CACHED_TIME, - }) + response = { + 'cached_until': time() + CACHE_TIMEOUT, + 'full_text': self.text, + 'name': 'scratchpad-count', + 'transformed': transformed + } + + return (POSITION, response) -- cgit v1.3