1 # python-musicpd: Python MPD client library
2 # Copyright (C) 2012-2021 kaliko <kaliko@azylum.org>
3 # Copyright (C) 2019 Naglis Jonaitis <naglis@mailbox.org>
4 # Copyright (C) 2019 Bart Van Loon <bbb@bbbart.be>
5 # Copyright (C) 2008-2010 J. Alexander Treuman <jat@spatialrift.net>
7 # python-musicpd is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Lesser General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # python-musicpd is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Lesser General Public License for more details.
17 # You should have received a copy of the GNU Lesser General Public License
18 # along with python-musicpd. If not, see <http://www.gnu.org/licenses/>.
20 # pylint: disable=missing-docstring
25 from functools import wraps
27 HELLO_PREFIX = "OK MPD "
32 #: seconds before a tcp connection attempt times out
33 CONNECTION_TIMEOUT = 5
36 def iterator_wrapper(func):
37 """Decorator handling iterate option"""
39 def decorated_function(instance, *args, **kwargs):
40 generator = func(instance, *args, **kwargs)
41 if not instance.iterate:
42 return list(generator)
43 instance._iterating = True
50 instance._iterating = False
51 return iterator(generator)
52 return decorated_function
55 class MPDError(Exception):
59 class ConnectionError(MPDError):
63 class ProtocolError(MPDError):
67 class CommandError(MPDError):
71 class CommandListError(MPDError):
75 class PendingCommandError(MPDError):
79 class IteratingError(MPDError):
85 def __init__(self, tpl):
90 if len(self.tpl) == 0:
92 if len(self.tpl) == 1:
93 return '{0}:'.format(self.tpl[0])
94 return '{0[0]}:{0[1]}'.format(self.tpl)
97 return 'Range({0})'.format(self.tpl)
100 if not isinstance(self.tpl, tuple):
101 raise CommandError('Wrong type, provide a tuple')
102 if len(self.tpl) not in [0, 1, 2]:
103 raise CommandError('length not in [0, 1, 2]')
104 for index in self.tpl:
107 except (TypeError, ValueError):
108 raise CommandError('Not a tuple of int')
113 def __getattr__(self, attr):
117 raise ConnectionError("Not connected")
121 """MPDClient instance will look for ``MPD_HOST``/``MPD_PORT``/``XDG_RUNTIME_DIR`` environment
122 variables and set instance attribute ``host``, ``port`` and ``pwd``
123 accordingly. Regarding ``MPD_HOST`` format to expose password refer
124 MPD client manual :manpage:`mpc (1)`.
126 Then :py:obj:`musicpd.MPDClient.connect` will use ``host`` and ``port`` as defaults if not provided as args.
128 Cf. :py:obj:`musicpd.MPDClient.connect` for details.
130 >>> from os import environ
131 >>> environ['MPD_HOST'] = 'pass@mpdhost'
132 >>> cli = musicpd.MPDClient()
133 >>> cli.pwd == environ['MPD_HOST'].split('@')[0]
135 >>> cli.host == environ['MPD_HOST'].split('@')[1]
137 >>> cli.connect() # will use host/port as set in MPD_HOST/MPD_PORT
139 :ivar str host: host used with the current connection
140 :ivar str,int port: port used with the current connection
141 :ivar str pwd: password detected in ``MPD_HOST`` environment variable
143 .. warning:: Instance attribute host/port/pwd
145 While :py:attr:`musicpd.MPDClient().host` and
146 :py:attr:`musicpd.MPDClient().port` keep track of current connection
147 host and port, :py:attr:`musicpd.MPDClient().pwd` is set once with
148 password extracted from environment variable.
149 Calling :py:meth:`musicpd.MPDClient().password()` with a new password
150 won't update :py:attr:`musicpd.MPDClient().pwd` value.
152 Moreover, :py:attr:`musicpd.MPDClient().pwd` is only an helper attribute
153 exposing password extracted from ``MPD_HOST`` environment variable, it
154 will not be used as default value for the :py:meth:`password` method
162 "clearerror": self._fetch_nothing,
163 "currentsong": self._fetch_object,
164 "idle": self._fetch_list,
166 "status": self._fetch_object,
167 "stats": self._fetch_object,
168 # Playback Option Commands
169 "consume": self._fetch_nothing,
170 "crossfade": self._fetch_nothing,
171 "mixrampdb": self._fetch_nothing,
172 "mixrampdelay": self._fetch_nothing,
173 "random": self._fetch_nothing,
174 "repeat": self._fetch_nothing,
175 "setvol": self._fetch_nothing,
176 "single": self._fetch_nothing,
177 "replay_gain_mode": self._fetch_nothing,
178 "replay_gain_status": self._fetch_item,
179 "volume": self._fetch_nothing,
180 # Playback Control Commands
181 "next": self._fetch_nothing,
182 "pause": self._fetch_nothing,
183 "play": self._fetch_nothing,
184 "playid": self._fetch_nothing,
185 "previous": self._fetch_nothing,
186 "seek": self._fetch_nothing,
187 "seekid": self._fetch_nothing,
188 "seekcur": self._fetch_nothing,
189 "stop": self._fetch_nothing,
191 "add": self._fetch_nothing,
192 "addid": self._fetch_item,
193 "clear": self._fetch_nothing,
194 "delete": self._fetch_nothing,
195 "deleteid": self._fetch_nothing,
196 "move": self._fetch_nothing,
197 "moveid": self._fetch_nothing,
198 "playlist": self._fetch_playlist,
199 "playlistfind": self._fetch_songs,
200 "playlistid": self._fetch_songs,
201 "playlistinfo": self._fetch_songs,
202 "playlistsearch": self._fetch_songs,
203 "plchanges": self._fetch_songs,
204 "plchangesposid": self._fetch_changes,
205 "prio": self._fetch_nothing,
206 "prioid": self._fetch_nothing,
207 "rangeid": self._fetch_nothing,
208 "shuffle": self._fetch_nothing,
209 "swap": self._fetch_nothing,
210 "swapid": self._fetch_nothing,
211 "addtagid": self._fetch_nothing,
212 "cleartagid": self._fetch_nothing,
213 # Stored Playlist Commands
214 "listplaylist": self._fetch_list,
215 "listplaylistinfo": self._fetch_songs,
216 "listplaylists": self._fetch_playlists,
217 "load": self._fetch_nothing,
218 "playlistadd": self._fetch_nothing,
219 "playlistclear": self._fetch_nothing,
220 "playlistdelete": self._fetch_nothing,
221 "playlistmove": self._fetch_nothing,
222 "rename": self._fetch_nothing,
223 "rm": self._fetch_nothing,
224 "save": self._fetch_nothing,
226 "albumart": self._fetch_composite,
227 "count": self._fetch_object,
228 "getfingerprint": self._fetch_object,
229 "find": self._fetch_songs,
230 "findadd": self._fetch_nothing,
231 "list": self._fetch_list,
232 "listall": self._fetch_database,
233 "listallinfo": self._fetch_database,
234 "listfiles": self._fetch_database,
235 "lsinfo": self._fetch_database,
236 "readcomments": self._fetch_object,
237 "readpicture": self._fetch_composite,
238 "search": self._fetch_songs,
239 "searchadd": self._fetch_nothing,
240 "searchaddpl": self._fetch_nothing,
241 "update": self._fetch_item,
242 "rescan": self._fetch_item,
243 # Mounts and neighbors
244 "mount": self._fetch_nothing,
245 "unmount": self._fetch_nothing,
246 "listmounts": self._fetch_mounts,
247 "listneighbors": self._fetch_neighbors,
249 "sticker get": self._fetch_item,
250 "sticker set": self._fetch_nothing,
251 "sticker delete": self._fetch_nothing,
252 "sticker list": self._fetch_list,
253 "sticker find": self._fetch_songs,
254 # Connection Commands
257 "password": self._fetch_nothing,
258 "ping": self._fetch_nothing,
259 "binarylimit": self._fetch_nothing,
260 "tagtypes": self._fetch_list,
261 "tagtypes disable": self._fetch_nothing,
262 "tagtypes enable": self._fetch_nothing,
263 "tagtypes clear": self._fetch_nothing,
264 "tagtypes all": self._fetch_nothing,
266 "partition": self._fetch_nothing,
267 "listpartitions": self._fetch_list,
268 "newpartition": self._fetch_nothing,
269 "delpartition": self._fetch_nothing,
270 "moveoutput": self._fetch_nothing,
271 # Audio Output Commands
272 "disableoutput": self._fetch_nothing,
273 "enableoutput": self._fetch_nothing,
274 "toggleoutput": self._fetch_nothing,
275 "outputs": self._fetch_outputs,
276 "outputset": self._fetch_nothing,
277 # Reflection Commands
278 "config": self._fetch_object,
279 "commands": self._fetch_list,
280 "notcommands": self._fetch_list,
281 "urlhandlers": self._fetch_list,
282 "decoders": self._fetch_plugins,
284 "subscribe": self._fetch_nothing,
285 "unsubscribe": self._fetch_nothing,
286 "channels": self._fetch_list,
287 "readmessages": self._fetch_messages,
288 "sendmessage": self._fetch_nothing,
292 def _get_envvars(self):
294 Retrieve MPD env. var. to overrides "localhost:6600"
295 Use MPD_HOST/MPD_PORT if set
296 else use MPD_HOST=${XDG_RUNTIME_DIR:-/run/}/mpd/socket if file exists
298 self.host = 'localhost'
300 self.port = os.environ.get('MPD_PORT', '6600')
301 mpd_host_env = os.environ.get('MPD_HOST')
303 # If password is set:
304 # mpd_host_env = ['pass', 'host'] because MPD_HOST=pass@host
305 mpd_host_env = mpd_host_env.split('@')
306 mpd_host_env.reverse()
307 self.host = mpd_host_env[0]
308 if len(mpd_host_env) > 1 and mpd_host_env[1]:
309 self.pwd = mpd_host_env[1]
312 xdg_runtime_dir = os.environ.get('XDG_RUNTIME_DIR', '/run')
313 rundir = os.path.join(xdg_runtime_dir, 'mpd/socket')
314 if os.path.exists(rundir):
317 def __getattr__(self, attr):
318 if attr == 'send_noidle': # have send_noidle to cancel idle as well as noidle
320 if attr.startswith("send_"):
321 command = attr.replace("send_", "", 1)
323 elif attr.startswith("fetch_"):
324 command = attr.replace("fetch_", "", 1)
325 wrapper = self._fetch
328 wrapper = self._execute
329 if command not in self._commands:
330 command = command.replace("_", " ")
331 if command not in self._commands:
332 raise AttributeError("'%s' object has no attribute '%s'" %
333 (self.__class__.__name__, attr))
334 return lambda *args: wrapper(command, args)
336 def _send(self, command, args):
337 if self._command_list is not None:
338 raise CommandListError("Cannot use send_%s in a command list" %
339 command.replace(" ", "_"))
340 self._write_command(command, args)
341 retval = self._commands[command]
342 if retval is not None:
343 self._pending.append(command)
345 def _fetch(self, command, args=None):
346 if self._command_list is not None:
347 raise CommandListError("Cannot use fetch_%s in a command list" %
348 command.replace(" ", "_"))
350 raise IteratingError("Cannot use fetch_%s while iterating" %
351 command.replace(" ", "_"))
352 if not self._pending:
353 raise PendingCommandError("No pending commands to fetch")
354 if self._pending[0] != command:
355 raise PendingCommandError("'%s' is not the currently "
356 "pending command" % command)
358 retval = self._commands[command]
363 def _execute(self, command, args):
365 raise IteratingError("Cannot execute '%s' while iterating" %
368 raise PendingCommandError(
369 "Cannot execute '%s' with pending commands" % command)
370 retval = self._commands[command]
371 if self._command_list is not None:
372 if not callable(retval):
373 raise CommandListError(
374 "'%s' not allowed in command list" % command)
375 self._write_command(command, args)
376 self._command_list.append(retval)
378 self._write_command(command, args)
383 def _write_line(self, line):
384 self._wfile.write("%s\n" % line)
387 def _write_command(self, command, args=None):
392 if isinstance(arg, tuple):
393 parts.append('{0!s}'.format(Range(arg)))
395 parts.append('"%s"' % escape(str(arg)))
396 self._write_line(" ".join(parts))
398 def _read_binary(self, amount):
401 result = self._rbfile.read(amount)
404 raise ConnectionError("Connection lost while reading binary content")
406 amount -= len(result)
409 def _read_line(self, binary=False):
411 line = self._rbfile.readline().decode('utf-8')
413 line = self._rfile.readline()
414 if not line.endswith("\n"):
416 raise ConnectionError("Connection lost while reading line")
417 line = line.rstrip("\n")
418 if line.startswith(ERROR_PREFIX):
419 error = line[len(ERROR_PREFIX):].strip()
420 raise CommandError(error)
421 if self._command_list is not None:
425 raise ProtocolError("Got unexpected '%s'" % SUCCESS)
426 elif line == SUCCESS:
430 def _read_pair(self, separator, binary=False):
431 line = self._read_line(binary=binary)
434 pair = line.split(separator, 1)
436 raise ProtocolError("Could not parse pair: '%s'" % line)
439 def _read_pairs(self, separator=": ", binary=False):
440 pair = self._read_pair(separator, binary=binary)
443 pair = self._read_pair(separator, binary=binary)
445 def _read_list(self):
447 for key, value in self._read_pairs():
450 raise ProtocolError("Expected key '%s', got '%s'" %
455 def _read_playlist(self):
456 for _, value in self._read_pairs(":"):
459 def _read_objects(self, delimiters=None):
461 if delimiters is None:
463 for key, value in self._read_pairs():
466 if key in delimiters:
470 if not isinstance(obj[key], list):
471 obj[key] = [obj[key], value]
473 obj[key].append(value)
479 def _read_command_list(self):
481 for retval in self._command_list:
484 self._command_list = None
485 self._fetch_nothing()
487 def _fetch_nothing(self):
488 line = self._read_line()
490 raise ProtocolError("Got unexpected return value: '%s'" % line)
492 def _fetch_item(self):
493 pairs = list(self._read_pairs())
499 def _fetch_list(self):
500 return self._read_list()
503 def _fetch_playlist(self):
504 return self._read_playlist()
506 def _fetch_object(self):
507 objs = list(self._read_objects())
513 def _fetch_objects(self, delimiters):
514 return self._read_objects(delimiters)
516 def _fetch_changes(self):
517 return self._fetch_objects(["cpos"])
519 def _fetch_songs(self):
520 return self._fetch_objects(["file"])
522 def _fetch_playlists(self):
523 return self._fetch_objects(["playlist"])
525 def _fetch_database(self):
526 return self._fetch_objects(["file", "directory", "playlist"])
528 def _fetch_outputs(self):
529 return self._fetch_objects(["outputid"])
531 def _fetch_plugins(self):
532 return self._fetch_objects(["plugin"])
534 def _fetch_messages(self):
535 return self._fetch_objects(["channel"])
537 def _fetch_mounts(self):
538 return self._fetch_objects(["mount"])
540 def _fetch_neighbors(self):
541 return self._fetch_objects(["neighbor"])
543 def _fetch_composite(self):
545 for key, value in self._read_pairs(binary=True):
551 # If the song file was recognized, but there is no picture, the
552 # response is successful, but is otherwise empty.
554 amount = int(obj['binary'])
556 obj['data'] = self._read_binary(amount)
557 except IOError as err:
558 raise ConnectionError('Error reading binary content: %s' % err)
559 if len(obj['data']) != amount:
560 raise ConnectionError('Error reading binary content: '
561 'Expects %sB, got %s' % (amount, len(obj['data'])))
562 # Fetches trailing new line
563 self._read_line(binary=True)
564 # Fetches SUCCESS code
565 self._read_line(binary=True)
569 def _fetch_command_list(self):
570 return self._read_command_list()
573 line = self._rfile.readline()
574 if not line.endswith("\n"):
575 raise ConnectionError("Connection lost while reading MPD hello")
576 line = line.rstrip("\n")
577 if not line.startswith(HELLO_PREFIX):
578 raise ProtocolError("Got invalid MPD hello: '%s'" % line)
579 self.mpd_version = line[len(HELLO_PREFIX):].strip()
582 self.mpd_version = None
583 self._iterating = False
585 self._command_list = None
587 self._rfile = _NotConnected()
588 self._rbfile = _NotConnected()
589 self._wfile = _NotConnected()
591 def _connect_unix(self, path):
592 if not hasattr(socket, "AF_UNIX"):
593 raise ConnectionError(
594 "Unix domain sockets not supported on this platform")
595 sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
599 def _connect_tcp(self, host, port):
601 flags = socket.AI_ADDRCONFIG
602 except AttributeError:
605 for res in socket.getaddrinfo(host, port, socket.AF_UNSPEC,
606 socket.SOCK_STREAM, socket.IPPROTO_TCP,
608 af, socktype, proto, _, sa = res
611 sock = socket.socket(af, socktype, proto)
612 sock.settimeout(CONNECTION_TIMEOUT)
614 sock.settimeout(None)
616 except socket.error as socket_err:
621 raise ConnectionError(str(err))
623 raise ConnectionError("getaddrinfo returns an empty list")
626 # noidle's special case
627 if not self._pending or self._pending[0] != 'idle':
629 'cannot send noidle if send_idle was not called')
631 self._write_command("noidle")
632 return self._fetch_list()
634 def connect(self, host=None, port=None):
635 """Connects the MPD server
637 :param str host: hostname, IP or FQDN (defaults to `localhost` or socket, see below for details)
638 :param port: port number (defaults to 6600)
639 :type port: str or int
641 The connect method honors MPD_HOST/MPD_PORT environment variables.
643 .. note:: Default host/port
645 If host evaluate to :py:obj:`False`
646 * use ``MPD_HOST`` environment variable if set, extract password if present,
647 * else looks for a existing file in ``${XDG_RUNTIME_DIR:-/run/}/mpd/socket``
648 * else set host to ``localhost``
650 If port evaluate to :py:obj:`False`
651 * if ``MPD_PORT`` environment variable is set, use it for port
662 if self._sock is not None:
663 raise ConnectionError("Already connected")
664 if host.startswith("/"):
665 self._sock = self._connect_unix(host)
667 self._sock = self._connect_tcp(host, port)
668 self._rfile = self._sock.makefile("r", encoding='utf-8', errors='surrogateescape')
669 self._rbfile = self._sock.makefile("rb")
670 self._wfile = self._sock.makefile("w", encoding='utf-8')
677 def disconnect(self):
678 """Closes the MPD connection.
679 The client closes the actual socket, it does not use the
680 'close' request from MPD protocol (as suggested in documentation).
682 if hasattr(self._rfile, 'close'):
684 if hasattr(self._rbfile, 'close'):
686 if hasattr(self._wfile, 'close'):
688 if hasattr(self._sock, 'close'):
693 if self._sock is None:
694 raise ConnectionError("Not connected")
695 return self._sock.fileno()
697 def command_list_ok_begin(self):
698 if self._command_list is not None:
699 raise CommandListError("Already in command list")
701 raise IteratingError("Cannot begin command list while iterating")
703 raise PendingCommandError("Cannot begin command list "
704 "with pending commands")
705 self._write_command("command_list_ok_begin")
706 self._command_list = []
708 def command_list_end(self):
709 if self._command_list is None:
710 raise CommandListError("Not in command list")
712 raise IteratingError("Already iterating over a command list")
713 self._write_command("command_list_end")
714 return self._fetch_command_list()
718 return text.replace("\\", "\\\\").replace('"', '\\"')
720 # vim: set expandtab shiftwidth=4 softtabstop=4 textwidth=79: