X-Git-Url: http://git.kaliko.me/?p=python-musicpd.git;a=blobdiff_plain;f=mpd.py;h=e16c8b8aa584ca7a5b5db0fa86faf002cc0afd5c;hp=d725bb23389a19790a3beec7b80450a91b5bbb7b;hb=6f1b787b4d4c946204df95f114a078a0b142b9c5;hpb=96c1cf2845f70ef969b4c4ba0d946c0c5c9d433b diff --git a/mpd.py b/mpd.py index d725bb2..e16c8b8 100644 --- a/mpd.py +++ b/mpd.py @@ -1,3 +1,4 @@ +# Python MPD client library # Copyright (C) 2008 J. Alexander Treuman # # This program is free software: you can redistribute it and/or modify @@ -25,6 +26,9 @@ NEXT = "list_OK" class MPDError(Exception): pass +class ConnectionError(MPDError): + pass + class ProtocolError(MPDError): pass @@ -140,7 +144,10 @@ class MPDClient(object): self._writeline(" ".join(parts)) def _readline(self): - line = self._sockfile.readline().rstrip("\n") + line = self._sockfile.readline() + if not line.endswith("\n"): + raise ConnectionError, "Connection lost while reading line" + line = line.rstrip("\n") if line.startswith(ERROR_PREFIX): error = line[len(ERROR_PREFIX):].strip() raise CommandError, error @@ -224,7 +231,7 @@ class MPDClient(object): def _getitem(self): items = list(self._readitems()) if len(items) != 1: - raise ProtocolError, "Expected 1 item, got %i" % len(items) + return return items[0][1] def _getlist(self): @@ -258,7 +265,9 @@ class MPDClient(object): return self._wrapiterator(self._readcommandlist()) def _hello(self): - line = self._sockfile.readline().rstrip("\n") + line = self._sockfile.readline() + if not line.endswith("\n"): + raise ConnectionError, "Connection lost while reading MPD hello" if not line.startswith(HELLO_PREFIX): raise ProtocolError, "Got invalid MPD hello: '%s'" % line self.mpd_version = line[len(HELLO_PREFIX):].strip()