From 380efde0890556760ff9160ca5200c5850293612 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sat, 8 Mar 2014 04:59:08 +0200 Subject: [PATCH] Handle MPD connection errors --- i3pystatus/mpd.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/i3pystatus/mpd.py b/i3pystatus/mpd.py index 82d8b73..4f2a6f8 100644 --- a/i3pystatus/mpd.py +++ b/i3pystatus/mpd.py @@ -41,8 +41,11 @@ class MPD(IntervalModule): host = "localhost" port = 6600 - s = socket.create_connection((host, port)) - s.recv(8192) + try: + s = socket.create_connection((host, port)) + s.recv(8192) + except Exception as e: + s = None format = "{title} {status}" format_sparse = None status = { @@ -54,7 +57,13 @@ class MPD(IntervalModule): vol = 100 def _mpd_command(self, sock, command): - sock.send((command + "\n").encode("utf-8")) + try: + sock.send((command + "\n").encode("utf-8")) + except Exception as e: + self.s = socket.create_connection((self.host, self.port)) + sock = self.s + sock.recv(8192) + sock.send((command + "\n").encode("utf-8")) reply = sock.recv(16384).decode("utf-8") replylines = reply.split("\n")[:-2] @@ -69,8 +78,12 @@ class MPD(IntervalModule): def run(self): fdict = {} - status = self._mpd_command(self.s, "status") - currentsong = self._mpd_command(self.s, "currentsong") + try: + status = self._mpd_command(self.s, "status") + currentsong = self._mpd_command(self.s, "currentsong") + except Exception as e: + self.output = {"full_text": "error connecting MPD"} + return fdict = { "pos": int(status.get("song", 0)) + 1,