X-Git-Url: https://git.kaliko.me/?a=blobdiff_plain;f=mpd.py;h=e16c8b8aa584ca7a5b5db0fa86faf002cc0afd5c;hb=ffe18c757c7adabf5e37aab5647898537bfe87d9;hp=f73c570b7ed505be6e33c7f1c545de25923af485;hpb=c7302b734b5dc912cc71226bbd2fd0e178d857ac;p=python-musicpd.git diff --git a/mpd.py b/mpd.py index f73c570..e16c8b8 100644 --- a/mpd.py +++ b/mpd.py @@ -1,4 +1,18 @@ -#! /usr/bin/env python +# Python MPD client library +# Copyright (C) 2008 J. Alexander Treuman +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . import socket @@ -12,6 +26,9 @@ NEXT = "list_OK" class MPDError(Exception): pass +class ConnectionError(MPDError): + pass + class ProtocolError(MPDError): pass @@ -127,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 @@ -211,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): @@ -245,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()