1 # python-musicpd: Python MPD client library
2 # Copyright (C) 2012-2021 kaliko <kaliko@azylum.org>
3 # Copyright (C) 2021 Wonko der Verständige <wonko@hanstool.org>
4 # Copyright (C) 2019 Naglis Jonaitis <naglis@mailbox.org>
5 # Copyright (C) 2019 Bart Van Loon <bbb@bbbart.be>
6 # Copyright (C) 2008-2010 J. Alexander Treuman <jat@spatialrift.net>
8 # python-musicpd is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU Lesser General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # python-musicpd is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU Lesser General Public License for more details.
18 # You should have received a copy of the GNU Lesser General Public License
19 # along with python-musicpd. If not, see <http://www.gnu.org/licenses/>.
24 from functools import wraps
26 HELLO_PREFIX = "OK MPD "
31 #: Seconds before a connection attempt times out
32 #: (overriden by MPD_TIMEOUT env. var.)
33 CONNECTION_TIMEOUT = 30
34 #: Socket timeout in second (Default is None for no timeout)
38 def iterator_wrapper(func):
39 """Decorator handling iterate option"""
41 def decorated_function(instance, *args, **kwargs):
42 generator = func(instance, *args, **kwargs)
43 if not instance.iterate:
44 return list(generator)
45 instance._iterating = True
52 instance._iterating = False
53 return iterator(generator)
54 return decorated_function
57 class MPDError(Exception):
61 class ConnectionError(MPDError):
65 class ProtocolError(MPDError):
69 class CommandError(MPDError):
73 class CommandListError(MPDError):
77 class PendingCommandError(MPDError):
81 class IteratingError(MPDError):
87 def __init__(self, tpl):
92 if len(self.tpl) == 0:
94 if len(self.tpl) == 1:
95 return '{0}:'.format(self.tpl[0])
96 return '{0[0]}:{0[1]}'.format(self.tpl)
99 return 'Range({0})'.format(self.tpl)
102 if not isinstance(self.tpl, tuple):
103 raise CommandError('Wrong type, provide a tuple')
104 if len(self.tpl) not in [0, 1, 2]:
105 raise CommandError('length not in [0, 1, 2]')
106 for index in self.tpl:
109 except (TypeError, ValueError) as err:
110 raise CommandError('Not a tuple of int') from err
115 def __getattr__(self, attr):
118 def _dummy(self, *args):
119 raise ConnectionError("Not connected")
123 """MPDClient instance will look for ``MPD_HOST``/``MPD_PORT``/``XDG_RUNTIME_DIR`` environment
124 variables and set instance attribute ``host``, ``port`` and ``pwd``
125 accordingly. Regarding ``MPD_HOST`` format to expose password refer
126 MPD client manual :manpage:`mpc (1)`.
128 Then :py:obj:`musicpd.MPDClient.connect` will use ``host`` and ``port`` as defaults if not provided as args.
130 Cf. :py:obj:`musicpd.MPDClient.connect` for details.
132 >>> from os import environ
133 >>> environ['MPD_HOST'] = 'pass@mpdhost'
134 >>> cli = musicpd.MPDClient()
135 >>> cli.pwd == environ['MPD_HOST'].split('@')[0]
137 >>> cli.host == environ['MPD_HOST'].split('@')[1]
139 >>> cli.connect() # will use host/port as set in MPD_HOST/MPD_PORT
141 :ivar str host: host used with the current connection
142 :ivar str,int port: port used with the current connection
143 :ivar str pwd: password detected in ``MPD_HOST`` environment variable
145 .. warning:: Instance attribute host/port/pwd
147 While :py:attr:`musicpd.MPDClient().host` and
148 :py:attr:`musicpd.MPDClient().port` keep track of current connection
149 host and port, :py:attr:`musicpd.MPDClient().pwd` is set once with
150 password extracted from environment variable.
151 Calling :py:meth:`musicpd.MPDClient().password()` with a new password
152 won't update :py:attr:`musicpd.MPDClient().pwd` value.
154 Moreover, :py:attr:`musicpd.MPDClient().pwd` is only an helper attribute
155 exposing password extracted from ``MPD_HOST`` environment variable, it
156 will not be used as default value for the :py:meth:`password` method
161 #: Socket timeout value in seconds
162 self._socket_timeout = SOCKET_TIMEOUT
163 #: Current connection timeout value, defaults to
164 #: :py:obj:`CONNECTION_TIMEOUT` or env. var. ``MPD_TIMEOUT`` if provided
165 self.mpd_timeout = None
169 "clearerror": self._fetch_nothing,
170 "currentsong": self._fetch_object,
171 "idle": self._fetch_list,
173 "status": self._fetch_object,
174 "stats": self._fetch_object,
175 # Playback Option Commands
176 "consume": self._fetch_nothing,
177 "crossfade": self._fetch_nothing,
178 "mixrampdb": self._fetch_nothing,
179 "mixrampdelay": self._fetch_nothing,
180 "random": self._fetch_nothing,
181 "repeat": self._fetch_nothing,
182 "setvol": self._fetch_nothing,
183 "getvol": self._fetch_object,
184 "single": self._fetch_nothing,
185 "replay_gain_mode": self._fetch_nothing,
186 "replay_gain_status": self._fetch_item,
187 "volume": self._fetch_nothing,
188 # Playback Control Commands
189 "next": self._fetch_nothing,
190 "pause": self._fetch_nothing,
191 "play": self._fetch_nothing,
192 "playid": self._fetch_nothing,
193 "previous": self._fetch_nothing,
194 "seek": self._fetch_nothing,
195 "seekid": self._fetch_nothing,
196 "seekcur": self._fetch_nothing,
197 "stop": self._fetch_nothing,
199 "add": self._fetch_nothing,
200 "addid": self._fetch_item,
201 "clear": self._fetch_nothing,
202 "delete": self._fetch_nothing,
203 "deleteid": self._fetch_nothing,
204 "move": self._fetch_nothing,
205 "moveid": self._fetch_nothing,
206 "playlist": self._fetch_playlist,
207 "playlistfind": self._fetch_songs,
208 "playlistid": self._fetch_songs,
209 "playlistinfo": self._fetch_songs,
210 "playlistsearch": self._fetch_songs,
211 "plchanges": self._fetch_songs,
212 "plchangesposid": self._fetch_changes,
213 "prio": self._fetch_nothing,
214 "prioid": self._fetch_nothing,
215 "rangeid": self._fetch_nothing,
216 "shuffle": self._fetch_nothing,
217 "swap": self._fetch_nothing,
218 "swapid": self._fetch_nothing,
219 "addtagid": self._fetch_nothing,
220 "cleartagid": self._fetch_nothing,
221 # Stored Playlist Commands
222 "listplaylist": self._fetch_list,
223 "listplaylistinfo": self._fetch_songs,
224 "listplaylists": self._fetch_playlists,
225 "load": self._fetch_nothing,
226 "playlistadd": self._fetch_nothing,
227 "playlistclear": self._fetch_nothing,
228 "playlistdelete": self._fetch_nothing,
229 "playlistmove": self._fetch_nothing,
230 "rename": self._fetch_nothing,
231 "rm": self._fetch_nothing,
232 "save": self._fetch_nothing,
234 "albumart": self._fetch_composite,
235 "count": self._fetch_object,
236 "getfingerprint": self._fetch_object,
237 "find": self._fetch_songs,
238 "findadd": self._fetch_nothing,
239 "list": self._fetch_list,
240 "listall": self._fetch_database,
241 "listallinfo": self._fetch_database,
242 "listfiles": self._fetch_database,
243 "lsinfo": self._fetch_database,
244 "readcomments": self._fetch_object,
245 "readpicture": self._fetch_composite,
246 "search": self._fetch_songs,
247 "searchadd": self._fetch_nothing,
248 "searchaddpl": self._fetch_nothing,
249 "update": self._fetch_item,
250 "rescan": self._fetch_item,
251 # Mounts and neighbors
252 "mount": self._fetch_nothing,
253 "unmount": self._fetch_nothing,
254 "listmounts": self._fetch_mounts,
255 "listneighbors": self._fetch_neighbors,
257 "sticker get": self._fetch_item,
258 "sticker set": self._fetch_nothing,
259 "sticker delete": self._fetch_nothing,
260 "sticker list": self._fetch_list,
261 "sticker find": self._fetch_songs,
262 # Connection Commands
265 "password": self._fetch_nothing,
266 "ping": self._fetch_nothing,
267 "binarylimit": self._fetch_nothing,
268 "tagtypes": self._fetch_list,
269 "tagtypes disable": self._fetch_nothing,
270 "tagtypes enable": self._fetch_nothing,
271 "tagtypes clear": self._fetch_nothing,
272 "tagtypes all": self._fetch_nothing,
274 "partition": self._fetch_nothing,
275 "listpartitions": self._fetch_list,
276 "newpartition": self._fetch_nothing,
277 "delpartition": self._fetch_nothing,
278 "moveoutput": self._fetch_nothing,
279 # Audio Output Commands
280 "disableoutput": self._fetch_nothing,
281 "enableoutput": self._fetch_nothing,
282 "toggleoutput": self._fetch_nothing,
283 "outputs": self._fetch_outputs,
284 "outputset": self._fetch_nothing,
285 # Reflection Commands
286 "config": self._fetch_object,
287 "commands": self._fetch_list,
288 "notcommands": self._fetch_list,
289 "urlhandlers": self._fetch_list,
290 "decoders": self._fetch_plugins,
292 "subscribe": self._fetch_nothing,
293 "unsubscribe": self._fetch_nothing,
294 "channels": self._fetch_list,
295 "readmessages": self._fetch_messages,
296 "sendmessage": self._fetch_nothing,
300 def _get_envvars(self):
302 Retrieve MPD env. var. to overrides "localhost:6600"
303 Use MPD_HOST/MPD_PORT if set
304 else use MPD_HOST=${XDG_RUNTIME_DIR:-/run/}/mpd/socket if file exists
306 self.host = 'localhost'
308 self.port = os.getenv('MPD_PORT', '6600')
309 if os.getenv('MPD_HOST'):
310 # If password is set: MPD_HOST=pass@host
311 if '@' in os.getenv('MPD_HOST'):
312 mpd_host_env = os.getenv('MPD_HOST').split('@', 1)
314 # A password is actually set
315 self.pwd = mpd_host_env[0]
317 self.host = mpd_host_env[1]
318 elif mpd_host_env[1]:
319 # No password set but leading @ is an abstract socket
320 self.host = '@'+mpd_host_env[1]
322 # MPD_HOST is a plain host
323 self.host = os.getenv('MPD_HOST')
326 xdg_runtime_dir = os.getenv('XDG_RUNTIME_DIR', '/run')
327 rundir = os.path.join(xdg_runtime_dir, 'mpd/socket')
328 if os.path.exists(rundir):
330 self.mpd_timeout = os.getenv('MPD_TIMEOUT')
331 if self.mpd_timeout and self.mpd_timeout.isdigit():
332 self.mpd_timeout = int(self.mpd_timeout)
333 else: # Use CONNECTION_TIMEOUT as default even if MPD_TIMEOUT carries gargage
334 self.mpd_timeout = CONNECTION_TIMEOUT
336 def __getattr__(self, attr):
337 if attr == 'send_noidle': # have send_noidle to cancel idle as well as noidle
339 if attr.startswith("send_"):
340 command = attr.replace("send_", "", 1)
342 elif attr.startswith("fetch_"):
343 command = attr.replace("fetch_", "", 1)
344 wrapper = self._fetch
347 wrapper = self._execute
348 if command not in self._commands:
349 command = command.replace("_", " ")
350 if command not in self._commands:
351 cls = self.__class__.__name__
352 raise AttributeError(f"'{cls}' object has no attribute '{attr}'")
353 return lambda *args: wrapper(command, args)
355 def _send(self, command, args):
356 if self._command_list is not None:
357 raise CommandListError("Cannot use send_%s in a command list" %
358 command.replace(" ", "_"))
359 self._write_command(command, args)
360 retval = self._commands[command]
361 if retval is not None:
362 self._pending.append(command)
364 def _fetch(self, command, args=None): # pylint: disable=unused-argument
365 cmd_fmt = command.replace(" ", "_")
366 if self._command_list is not None:
367 raise CommandListError(f"Cannot use fetch_{cmd_fmt} in a command list")
369 raise IteratingError(f"Cannot use fetch_{cmd_fmt} while iterating")
370 if not self._pending:
371 raise PendingCommandError("No pending commands to fetch")
372 if self._pending[0] != command:
373 raise PendingCommandError(f"'{command}' is not the currently pending command")
375 retval = self._commands[command]
380 def _execute(self, command, args): # pylint: disable=unused-argument
382 raise IteratingError(f"Cannot execute '{command}' while iterating")
384 raise PendingCommandError(f"Cannot execute '{command}' with pending commands")
385 retval = self._commands[command]
386 if self._command_list is not None:
387 if not callable(retval):
388 raise CommandListError(f"'{command}' not allowed in command list")
389 self._write_command(command, args)
390 self._command_list.append(retval)
392 self._write_command(command, args)
398 def _write_line(self, line):
399 self._wfile.write(f"{line!s}\n")
402 def _write_command(self, command, args=None):
407 if isinstance(arg, tuple):
408 parts.append('{0!s}'.format(Range(arg)))
410 parts.append('"%s"' % escape(str(arg)))
411 if '\n' in ' '.join(parts):
412 raise CommandError('new line found in the command!')
413 self._write_line(" ".join(parts))
415 def _read_binary(self, amount):
418 result = self._rbfile.read(amount)
421 raise ConnectionError("Connection lost while reading binary content")
423 amount -= len(result)
426 def _read_line(self, binary=False):
428 line = self._rbfile.readline().decode('utf-8')
430 line = self._rfile.readline()
431 if not line.endswith("\n"):
433 raise ConnectionError("Connection lost while reading line")
434 line = line.rstrip("\n")
435 if line.startswith(ERROR_PREFIX):
436 error = line[len(ERROR_PREFIX):].strip()
437 raise CommandError(error)
438 if self._command_list is not None:
442 raise ProtocolError(f"Got unexpected '{SUCCESS}'")
443 elif line == SUCCESS:
447 def _read_pair(self, separator, binary=False):
448 line = self._read_line(binary=binary)
451 pair = line.split(separator, 1)
453 raise ProtocolError(f"Could not parse pair: '{line}'")
456 def _read_pairs(self, separator=": ", binary=False):
457 pair = self._read_pair(separator, binary=binary)
460 pair = self._read_pair(separator, binary=binary)
462 def _read_list(self):
464 for key, value in self._read_pairs():
467 raise ProtocolError(f"Expected key '{seen}', got '{key}'")
471 def _read_playlist(self):
472 for _, value in self._read_pairs(":"):
475 def _read_objects(self, delimiters=None):
477 if delimiters is None:
479 for key, value in self._read_pairs():
482 if key in delimiters:
486 if not isinstance(obj[key], list):
487 obj[key] = [obj[key], value]
489 obj[key].append(value)
495 def _read_command_list(self):
497 for retval in self._command_list:
500 self._command_list = None
501 self._fetch_nothing()
503 def _fetch_nothing(self):
504 line = self._read_line()
506 raise ProtocolError(f"Got unexpected return value: '{line}'")
508 def _fetch_item(self):
509 pairs = list(self._read_pairs())
515 def _fetch_list(self):
516 return self._read_list()
519 def _fetch_playlist(self):
520 return self._read_playlist()
522 def _fetch_object(self):
523 objs = list(self._read_objects())
529 def _fetch_objects(self, delimiters):
530 return self._read_objects(delimiters)
532 def _fetch_changes(self):
533 return self._fetch_objects(["cpos"])
535 def _fetch_songs(self):
536 return self._fetch_objects(["file"])
538 def _fetch_playlists(self):
539 return self._fetch_objects(["playlist"])
541 def _fetch_database(self):
542 return self._fetch_objects(["file", "directory", "playlist"])
544 def _fetch_outputs(self):
545 return self._fetch_objects(["outputid"])
547 def _fetch_plugins(self):
548 return self._fetch_objects(["plugin"])
550 def _fetch_messages(self):
551 return self._fetch_objects(["channel"])
553 def _fetch_mounts(self):
554 return self._fetch_objects(["mount"])
556 def _fetch_neighbors(self):
557 return self._fetch_objects(["neighbor"])
559 def _fetch_composite(self):
561 for key, value in self._read_pairs(binary=True):
567 # If the song file was recognized, but there is no picture, the
568 # response is successful, but is otherwise empty.
570 amount = int(obj['binary'])
572 obj['data'] = self._read_binary(amount)
573 except IOError as err:
574 raise ConnectionError(f'Error reading binary content: {err}') from err
575 data_bytes = len(obj['data'])
576 if data_bytes != amount: # can we ever get there?
577 raise ConnectionError('Error reading binary content: '
578 f'Expects {amount}B, got {data_bytes}')
579 # Fetches trailing new line
580 self._read_line(binary=True)
581 # Fetches SUCCESS code
582 self._read_line(binary=True)
586 def _fetch_command_list(self):
587 return self._read_command_list()
590 line = self._rfile.readline()
591 if not line.endswith("\n"):
592 raise ConnectionError("Connection lost while reading MPD hello")
593 line = line.rstrip("\n")
594 if not line.startswith(HELLO_PREFIX):
595 raise ProtocolError(f"Got invalid MPD hello: '{line}'")
596 self.mpd_version = line[len(HELLO_PREFIX):].strip()
599 self.mpd_version = None
600 self._iterating = False
602 self._command_list = None
604 self._rfile = _NotConnected()
605 self._rbfile = _NotConnected()
606 self._wfile = _NotConnected()
608 def _connect_unix(self, path):
609 if not hasattr(socket, "AF_UNIX"):
610 raise ConnectionError("Unix domain sockets not supported on this platform")
612 if path.startswith('@'):
614 sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
615 sock.settimeout(self.mpd_timeout)
617 sock.settimeout(self.socket_timeout)
620 def _connect_tcp(self, host, port):
622 flags = socket.AI_ADDRCONFIG
623 except AttributeError:
626 for res in socket.getaddrinfo(host, port, socket.AF_UNSPEC,
627 socket.SOCK_STREAM, socket.IPPROTO_TCP,
629 af, socktype, proto, _, sa = res
632 sock = socket.socket(af, socktype, proto)
633 sock.settimeout(self.mpd_timeout)
635 sock.settimeout(self.socket_timeout)
637 except socket.error as socket_err:
642 raise ConnectionError(str(err))
643 raise ConnectionError("getaddrinfo returns an empty list")
646 # noidle's special case
647 if not self._pending or self._pending[0] != 'idle':
648 raise CommandError('cannot send noidle if send_idle was not called')
650 self._write_command("noidle")
651 return self._fetch_list()
653 def connect(self, host=None, port=None):
654 """Connects the MPD server
656 :param str host: hostname, IP or FQDN (defaults to `localhost` or socket, see below for details)
657 :param port: port number (defaults to 6600)
658 :type port: str or int
660 The connect method honors MPD_HOST/MPD_PORT environment variables.
662 The underlying socket also honors MPD_TIMEOUT environment variable
663 and defaults to :py:obj:`musicpd.CONNECTION_TIMEOUT` (connect command only).
665 If you want to have a timeout for each command once you got connected,
666 set its value in :py:obj:`MPDClient.socket_timeout` (in second) or at
667 module level in :py:obj:`musicpd.SOCKET_TIMEOUT`.
669 .. note:: Default host/port
671 If host evaluate to :py:obj:`False`
672 * use ``MPD_HOST`` environment variable if set, extract password if present,
673 * else looks for an existing file in ``${XDG_RUNTIME_DIR:-/run/}/mpd/socket``
674 * else set host to ``localhost``
676 If port evaluate to :py:obj:`False`
677 * if ``MPD_PORT`` environment variable is set, use it for port
688 if self._sock is not None:
689 raise ConnectionError("Already connected")
690 if host[0] in ['/', '@']:
691 self._sock = self._connect_unix(host)
693 self._sock = self._connect_tcp(host, port)
694 self._rfile = self._sock.makefile("r", encoding='utf-8', errors='surrogateescape')
695 self._rbfile = self._sock.makefile("rb")
696 self._wfile = self._sock.makefile("w", encoding='utf-8')
704 def socket_timeout(self):
705 """Socket timeout in second (defaults to :py:obj:`SOCKET_TIMEOUT`).
706 Use None to disable socket timout."""
707 return self._socket_timeout
709 @socket_timeout.setter
710 def socket_timeout(self, timeout):
711 self._socket_timeout = timeout
712 if getattr(self._sock, 'settimeout', False):
713 self._sock.settimeout(self._socket_timeout)
715 def disconnect(self):
716 """Closes the MPD connection.
717 The client closes the actual socket, it does not use the
718 'close' request from MPD protocol (as suggested in documentation).
720 if hasattr(self._rfile, 'close'):
722 if hasattr(self._rbfile, 'close'):
724 if hasattr(self._wfile, 'close'):
726 if hasattr(self._sock, 'close'):
734 def __exit__(self, exception_type, exception_value, exception_traceback):
738 """Return the socket’s file descriptor (a small integer).
739 This is useful with :py:obj:`select.select`.
741 if self._sock is None:
742 raise ConnectionError("Not connected")
743 return self._sock.fileno()
745 def command_list_ok_begin(self):
746 if self._command_list is not None:
747 raise CommandListError("Already in command list")
749 raise IteratingError("Cannot begin command list while iterating")
751 raise PendingCommandError("Cannot begin command list with pending commands")
752 self._write_command("command_list_ok_begin")
753 self._command_list = []
755 def command_list_end(self):
756 if self._command_list is None:
757 raise CommandListError("Not in command list")
759 raise IteratingError("Already iterating over a command list")
760 self._write_command("command_list_end")
761 return self._fetch_command_list()
765 return text.replace("\\", "\\\\").replace('"', '\\"')
767 # vim: set expandtab shiftwidth=4 softtabstop=4 textwidth=79: