1 # Python MPD client library
2 # Copyright (C) 2008 J. Alexander Treuman <jat@spatialrift.net>
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 HELLO_PREFIX = "OK MPD "
26 class MPDError(Exception):
29 class ConnectionError(MPDError):
32 class ProtocolError(MPDError):
35 class CommandError(MPDError):
38 class CommandListError(MPDError):
42 class _NotConnected(object):
43 def __getattr__(self, attr):
47 raise ConnectionError("Not connected")
49 class MPDClient(object):
55 "clearerror": self._getnone,
56 "currentsong": self._getobject,
57 "status": self._getobject,
58 "stats": self._getobject,
59 # Playback Option Commands
60 "crossfade": self._getnone,
61 "random": self._getnone,
62 "repeat": self._getnone,
63 "setvol": self._getnone,
64 "volume": self._getnone,
65 # Playback Control Commands
66 "next": self._getnone,
67 "pause": self._getnone,
68 "play": self._getnone,
69 "playid": self._getnone,
70 "previous": self._getnone,
71 "seek": self._getnone,
72 "seekid": self._getnone,
73 "stop": self._getnone,
76 "addid": self._getitem,
77 "clear": self._getnone,
78 "delete": self._getnone,
79 "deleteid": self._getnone,
80 "move": self._getnone,
81 "moveid": self._getnone,
82 "playlist": self._getplaylist,
83 "playlistfind": self._getsongs,
84 "playlistid": self._getsongs,
85 "playlistinfo": self._getsongs,
86 "playlistsearch": self._getsongs,
87 "plchanges": self._getsongs,
88 "plchangesposid": self._getchanges,
89 "shuffle": self._getnone,
90 "swap": self._getnone,
91 "swapid": self._getnone,
92 # Stored Playlist Commands
93 "listplaylist": self._getlist,
94 "listplaylistinfo": self._getsongs,
95 "load": self._getnone,
96 "playlistadd": self._getnone,
97 "playlistclear": self._getnone,
98 "playlistdelete": self._getnone,
99 "playlistmove": self._getnone,
100 "rename": self._getnone,
102 "save": self._getnone,
104 "count": self._getobject,
105 "find": self._getsongs,
106 "list": self._getlist,
107 "listall": self._getdatabase,
108 "listallinfo": self._getdatabase,
109 "lsinfo": self._getdatabase,
110 "search": self._getsongs,
111 "update": self._getitem,
112 # Connection Commands
115 "password": self._getnone,
116 "ping": self._getnone,
117 # Audio Output Commands
118 "disableoutput": self._getnone,
119 "enableoutput": self._getnone,
120 "outputs": self._getoutputs,
121 # Reflection Commands
122 "commands": self._getlist,
123 "notcommands": self._getlist,
124 "tagtypes": self._getlist,
125 "urlhandlers": self._getlist,
128 def __getattr__(self, attr):
130 retval = self._commands[attr]
132 raise AttributeError("'%s' object has no attribute '%s'" %
133 (self.__class__.__name__, attr))
134 return lambda *args: self._docommand(attr, args, retval)
136 def _docommand(self, command, args, retval):
137 if self._commandlist is not None and not callable(retval):
138 raise CommandListError("%s not allowed in command list" % command)
139 self._writecommand(command, args)
140 if self._commandlist is None:
144 self._commandlist.append(retval)
146 def _writeline(self, line):
147 self._wfile.write("%s\n" % line)
150 def _writecommand(self, command, args=[]):
153 parts.append('"%s"' % escape(str(arg)))
154 self._writeline(" ".join(parts))
157 line = self._rfile.readline()
158 if not line.endswith("\n"):
159 raise ConnectionError("Connection lost while reading line")
160 line = line.rstrip("\n")
161 if line.startswith(ERROR_PREFIX):
162 error = line[len(ERROR_PREFIX):].strip()
163 raise CommandError(error)
164 if self._commandlist is not None:
168 raise ProtocolError("Got unexpected '%s'" % SUCCESS)
169 elif line == SUCCESS:
173 def _readitem(self, separator):
174 line = self._readline()
177 item = line.split(separator, 1)
179 raise ProtocolError("Could not parse item: '%s'" % line)
182 def _readitems(self, separator=": "):
183 item = self._readitem(separator)
186 item = self._readitem(separator)
191 for key, value in self._readitems():
194 raise ProtocolError("Expected key '%s', got '%s'" %
200 def _readplaylist(self):
201 for key, value in self._readitems(":"):
205 def _readobjects(self, delimiters=[]):
207 for key, value in self._readitems():
210 if key in delimiters:
213 elif obj.has_key(key):
214 if not isinstance(obj[key], list):
215 obj[key] = [obj[key], value]
217 obj[key].append(value)
224 def _readcommandlist(self):
225 for retval in self._commandlist:
227 self._commandlist = None
231 def _wrapiterator(self, iterator):
233 return list(iterator)
237 line = self._readline()
239 raise ProtocolError("Got unexpected return value: '%s'" % line)
242 items = list(self._readitems())
248 return self._wrapiterator(self._readlist())
250 def _getplaylist(self):
251 return self._wrapiterator(self._readplaylist())
253 def _getobject(self):
254 objs = list(self._readobjects())
259 def _getobjects(self, delimiters):
260 return self._wrapiterator(self._readobjects(delimiters))
263 return self._getobjects(["file"])
265 def _getdatabase(self):
266 return self._getobjects(["file", "directory", "playlist"])
268 def _getoutputs(self):
269 return self._getobjects(["outputid"])
271 def _getchanges(self):
272 return self._getobjects(["cpos"])
274 def _getcommandlist(self):
275 return self._wrapiterator(self._readcommandlist())
278 line = self._rfile.readline()
279 if not line.endswith("\n"):
280 raise ConnectionError("Connection lost while reading MPD hello")
281 line = line.rstrip("\n")
282 if not line.startswith(HELLO_PREFIX):
283 raise ProtocolError("Got invalid MPD hello: '%s'" % line)
284 self.mpd_version = line[len(HELLO_PREFIX):].strip()
287 self.mpd_version = None
288 self._commandlist = None
290 self._rfile = _NotConnected()
291 self._wfile = _NotConnected()
293 def connect(self, host, port):
295 raise ConnectionError("Already connected")
296 msg = "getaddrinfo returns an empty list"
298 flags = socket.AI_ADDRCONFIG
299 except AttributeError:
301 for res in socket.getaddrinfo(host, port, socket.AF_UNSPEC,
302 socket.SOCK_STREAM, socket.IPPROTO_TCP,
304 af, socktype, proto, canonname, sa = res
306 self._sock = socket.socket(af, socktype, proto)
307 self._sock.connect(sa)
308 except socket.error, msg:
315 raise socket.error(msg)
316 self._rfile = self._sock.makefile("rb")
317 self._wfile = self._sock.makefile("wb")
324 def disconnect(self):
330 def command_list_ok_begin(self):
331 if self._commandlist is not None:
332 raise CommandListError("Already in command list")
333 self._writecommand("command_list_ok_begin")
334 self._commandlist = []
336 def command_list_end(self):
337 if self._commandlist is None:
338 raise CommandListError("Not in command list")
339 self._writecommand("command_list_end")
340 return self._getcommandlist()
344 return text.replace("\\", "\\\\").replace('"', '\\"')
347 # vim: set expandtab shiftwidth=4 softtabstop=4 textwidth=79: