1 # SPDX-FileCopyrightText: 2012-2023 kaliko <kaliko@azylum.org>
2 # SPDX-FileCopyrightText: 2021 Wonko der Verständige <wonko@hanstool.org>
3 # SPDX-FileCopyrightText: 2019 Naglis Jonaitis <naglis@mailbox.org>
4 # SPDX-FileCopyrightText: 2019 Bart Van Loon <bbb@bbbart.be>
5 # SPDX-FileCopyrightText: 2008-2010 J. Alexander Treuman <jat@spatialrift.net>
6 # SPDX-License-Identifier: LGPL-3.0-or-later
7 """python-musicpd: Python Music Player Daemon client library"""
13 from functools import wraps
15 HELLO_PREFIX = "OK MPD "
20 #: Seconds before a connection attempt times out
21 #: (overriden by MPD_TIMEOUT env. var.)
22 CONNECTION_TIMEOUT = 30
23 #: Socket timeout in second (Default is None for no timeout)
27 def iterator_wrapper(func):
28 """Decorator handling iterate option"""
30 def decorated_function(instance, *args, **kwargs):
31 generator = func(instance, *args, **kwargs)
32 if not instance.iterate:
33 return list(generator)
34 instance._iterating = True
41 instance._iterating = False
42 return iterator(generator)
43 return decorated_function
46 class MPDError(Exception):
50 class ConnectionError(MPDError):
54 class ProtocolError(MPDError):
58 class CommandError(MPDError):
62 class CommandListError(MPDError):
66 class PendingCommandError(MPDError):
70 class IteratingError(MPDError):
76 def __init__(self, tpl):
83 return f'{self.lower}:{self.upper}'
86 return f'Range({self.tpl})'
88 def _check_element(self, item):
89 if item is None or item == '':
93 except (TypeError, ValueError) as err:
94 raise CommandError(f'Not an integer: "{item}"') from err
98 if not isinstance(self.tpl, tuple):
99 raise CommandError('Wrong type, provide a tuple')
100 if len(self.tpl) == 0:
102 if len(self.tpl) == 1:
103 self.lower = self._check_element(self.tpl[0])
105 if len(self.tpl) != 2:
106 raise CommandError('Range wrong size (0, 1 or 2 allowed)')
107 self.lower = self._check_element(self.tpl[0])
108 self.upper = self._check_element(self.tpl[1])
109 if self.lower == '' and self.upper != '':
110 raise CommandError(f'Integer expected to start the range: {self.tpl}')
111 if self.upper.isdigit() and self.lower.isdigit():
112 if int(self.lower) > int(self.upper):
113 raise CommandError(f'Wrong range: {self.lower} > {self.upper}')
118 def __getattr__(self, attr):
121 def _dummy(self, *args):
122 raise ConnectionError("Not connected")
126 """MPDClient instance will look for ``MPD_HOST``/``MPD_PORT``/``XDG_RUNTIME_DIR`` environment
127 variables and set instance attribute ``host``, ``port`` and ``pwd``
128 accordingly. Regarding ``MPD_HOST`` format to expose password refer
129 MPD client manual :manpage:`mpc (1)`.
131 Then :py:obj:`musicpd.MPDClient.connect` will use ``host`` and ``port`` as defaults if not provided as args.
133 Cf. :py:obj:`musicpd.MPDClient.connect` for details.
135 >>> from os import environ
136 >>> environ['MPD_HOST'] = 'pass@mpdhost'
137 >>> cli = musicpd.MPDClient()
138 >>> cli.pwd == environ['MPD_HOST'].split('@')[0]
140 >>> cli.host == environ['MPD_HOST'].split('@')[1]
142 >>> cli.connect() # will use host/port as set in MPD_HOST/MPD_PORT
144 :ivar str host: host used with the current connection
145 :ivar str,int port: port used with the current connection
146 :ivar str pwd: password detected in ``MPD_HOST`` environment variable
148 .. warning:: Instance attribute host/port/pwd
150 While :py:attr:`musicpd.MPDClient().host` and
151 :py:attr:`musicpd.MPDClient().port` keep track of current connection
152 host and port, :py:attr:`musicpd.MPDClient().pwd` is set once with
153 password extracted from environment variable.
154 Calling :py:meth:`musicpd.MPDClient().password()` with a new password
155 won't update :py:attr:`musicpd.MPDClient().pwd` value.
157 Moreover, :py:attr:`musicpd.MPDClient().pwd` is only an helper attribute
158 exposing password extracted from ``MPD_HOST`` environment variable, it
159 will not be used as default value for the :py:meth:`password` method
164 #: Socket timeout value in seconds
165 self._socket_timeout = SOCKET_TIMEOUT
166 #: Current connection timeout value, defaults to
167 #: :py:obj:`CONNECTION_TIMEOUT` or env. var. ``MPD_TIMEOUT`` if provided
168 self.mpd_timeout = None
169 self.mpd_version = ''
170 """Protocol version as exposed by the server as a :py:obj:`str`
172 .. note:: This is the version of the protocol spoken, not the real version of the daemon."""
176 "clearerror": self._fetch_nothing,
177 "currentsong": self._fetch_object,
178 "idle": self._fetch_list,
180 "status": self._fetch_object,
181 "stats": self._fetch_object,
182 # Playback Option Commands
183 "consume": self._fetch_nothing,
184 "crossfade": self._fetch_nothing,
185 "mixrampdb": self._fetch_nothing,
186 "mixrampdelay": self._fetch_nothing,
187 "random": self._fetch_nothing,
188 "repeat": self._fetch_nothing,
189 "setvol": self._fetch_nothing,
190 "getvol": self._fetch_object,
191 "single": self._fetch_nothing,
192 "replay_gain_mode": self._fetch_nothing,
193 "replay_gain_status": self._fetch_item,
194 "volume": self._fetch_nothing,
195 # Playback Control Commands
196 "next": self._fetch_nothing,
197 "pause": self._fetch_nothing,
198 "play": self._fetch_nothing,
199 "playid": self._fetch_nothing,
200 "previous": self._fetch_nothing,
201 "seek": self._fetch_nothing,
202 "seekid": self._fetch_nothing,
203 "seekcur": self._fetch_nothing,
204 "stop": self._fetch_nothing,
206 "add": self._fetch_nothing,
207 "addid": self._fetch_item,
208 "clear": self._fetch_nothing,
209 "delete": self._fetch_nothing,
210 "deleteid": self._fetch_nothing,
211 "move": self._fetch_nothing,
212 "moveid": self._fetch_nothing,
213 "playlist": self._fetch_playlist,
214 "playlistfind": self._fetch_songs,
215 "playlistid": self._fetch_songs,
216 "playlistinfo": self._fetch_songs,
217 "playlistsearch": self._fetch_songs,
218 "plchanges": self._fetch_songs,
219 "plchangesposid": self._fetch_changes,
220 "prio": self._fetch_nothing,
221 "prioid": self._fetch_nothing,
222 "rangeid": self._fetch_nothing,
223 "shuffle": self._fetch_nothing,
224 "swap": self._fetch_nothing,
225 "swapid": self._fetch_nothing,
226 "addtagid": self._fetch_nothing,
227 "cleartagid": self._fetch_nothing,
228 # Stored Playlist Commands
229 "listplaylist": self._fetch_list,
230 "listplaylistinfo": self._fetch_songs,
231 "listplaylists": self._fetch_playlists,
232 "load": self._fetch_nothing,
233 "playlistadd": self._fetch_nothing,
234 "playlistclear": self._fetch_nothing,
235 "playlistdelete": self._fetch_nothing,
236 "playlistmove": self._fetch_nothing,
237 "rename": self._fetch_nothing,
238 "rm": self._fetch_nothing,
239 "save": self._fetch_nothing,
241 "albumart": self._fetch_composite,
242 "count": self._fetch_object,
243 "getfingerprint": self._fetch_object,
244 "find": self._fetch_songs,
245 "findadd": self._fetch_nothing,
246 "list": self._fetch_list,
247 "listall": self._fetch_database,
248 "listallinfo": self._fetch_database,
249 "listfiles": self._fetch_database,
250 "lsinfo": self._fetch_database,
251 "readcomments": self._fetch_object,
252 "readpicture": self._fetch_composite,
253 "search": self._fetch_songs,
254 "searchadd": self._fetch_nothing,
255 "searchaddpl": self._fetch_nothing,
256 "update": self._fetch_item,
257 "rescan": self._fetch_item,
258 # Mounts and neighbors
259 "mount": self._fetch_nothing,
260 "unmount": self._fetch_nothing,
261 "listmounts": self._fetch_mounts,
262 "listneighbors": self._fetch_neighbors,
264 "sticker get": self._fetch_item,
265 "sticker set": self._fetch_nothing,
266 "sticker delete": self._fetch_nothing,
267 "sticker list": self._fetch_list,
268 "sticker find": self._fetch_songs,
269 # Connection Commands
272 "password": self._fetch_nothing,
273 "ping": self._fetch_nothing,
274 "binarylimit": self._fetch_nothing,
275 "tagtypes": self._fetch_list,
276 "tagtypes disable": self._fetch_nothing,
277 "tagtypes enable": self._fetch_nothing,
278 "tagtypes clear": self._fetch_nothing,
279 "tagtypes all": self._fetch_nothing,
281 "partition": self._fetch_nothing,
282 "listpartitions": self._fetch_list,
283 "newpartition": self._fetch_nothing,
284 "delpartition": self._fetch_nothing,
285 "moveoutput": self._fetch_nothing,
286 # Audio Output Commands
287 "disableoutput": self._fetch_nothing,
288 "enableoutput": self._fetch_nothing,
289 "toggleoutput": self._fetch_nothing,
290 "outputs": self._fetch_outputs,
291 "outputset": self._fetch_nothing,
292 # Reflection Commands
293 "config": self._fetch_object,
294 "commands": self._fetch_list,
295 "notcommands": self._fetch_list,
296 "urlhandlers": self._fetch_list,
297 "decoders": self._fetch_plugins,
299 "subscribe": self._fetch_nothing,
300 "unsubscribe": self._fetch_nothing,
301 "channels": self._fetch_list,
302 "readmessages": self._fetch_messages,
303 "sendmessage": self._fetch_nothing,
305 #: host used with the current connection (:py:obj:`str`)
307 #: password detected in :envvar:`MPD_HOST` environment variable (:py:obj:`str`)
309 #: port used with the current connection (:py:obj:`int`, :py:obj:`str`)
313 def _get_envvars(self):
315 Retrieve MPD env. var. to overrides default "localhost:6600"
318 self.host = 'localhost'
319 self.port = os.getenv('MPD_PORT', '6600')
320 _host = os.getenv('MPD_HOST', '')
322 # If password is set: MPD_HOST=pass@host
324 mpd_host_env = _host.split('@', 1)
326 # A password is actually set
327 self.pwd = mpd_host_env[0]
329 self.host = mpd_host_env[1]
330 elif mpd_host_env[1]:
331 # No password set but leading @ is an abstract socket
332 self.host = '@'+mpd_host_env[1]
334 # MPD_HOST is a plain host
338 xdg_runtime_dir = os.getenv('XDG_RUNTIME_DIR', '/run')
339 rundir = os.path.join(xdg_runtime_dir, 'mpd/socket')
340 if os.path.exists(rundir):
342 _mpd_timeout = os.getenv('MPD_TIMEOUT', '')
343 if _mpd_timeout.isdigit():
344 self.mpd_timeout = int(_mpd_timeout)
345 else: # Use CONNECTION_TIMEOUT as default even if MPD_TIMEOUT carries gargage
346 self.mpd_timeout = CONNECTION_TIMEOUT
348 def __getattr__(self, attr):
349 if attr == 'send_noidle': # have send_noidle to cancel idle as well as noidle
351 if attr.startswith("send_"):
352 command = attr.replace("send_", "", 1)
354 elif attr.startswith("fetch_"):
355 command = attr.replace("fetch_", "", 1)
356 wrapper = self._fetch
359 wrapper = self._execute
360 if command not in self._commands:
361 command = command.replace("_", " ")
362 if command not in self._commands:
363 cls = self.__class__.__name__
364 raise AttributeError(f"'{cls}' object has no attribute '{attr}'")
365 return lambda *args: wrapper(command, args)
367 def _send(self, command, args):
368 if self._command_list is not None:
369 raise CommandListError("Cannot use send_%s in a command list" %
370 command.replace(" ", "_"))
371 self._write_command(command, args)
372 retval = self._commands[command]
373 if retval is not None:
374 self._pending.append(command)
376 def _fetch(self, command, args=None): # pylint: disable=unused-argument
377 cmd_fmt = command.replace(" ", "_")
378 if self._command_list is not None:
379 raise CommandListError(f"Cannot use fetch_{cmd_fmt} in a command list")
381 raise IteratingError(f"Cannot use fetch_{cmd_fmt} while iterating")
382 if not self._pending:
383 raise PendingCommandError("No pending commands to fetch")
384 if self._pending[0] != command:
385 raise PendingCommandError(f"'{command}' is not the currently pending command")
387 retval = self._commands[command]
392 def _execute(self, command, args): # pylint: disable=unused-argument
394 raise IteratingError(f"Cannot execute '{command}' while iterating")
396 raise PendingCommandError(f"Cannot execute '{command}' with pending commands")
397 retval = self._commands[command]
398 if self._command_list is not None:
399 if not callable(retval):
400 raise CommandListError(f"'{command}' not allowed in command list")
401 self._write_command(command, args)
402 self._command_list.append(retval)
404 self._write_command(command, args)
410 def _write_line(self, line):
411 self._wfile.write(f"{line!s}\n")
414 def _write_command(self, command, args=None):
419 if isinstance(arg, tuple):
420 parts.append('{0!s}'.format(Range(arg)))
422 parts.append('"%s"' % escape(str(arg)))
423 if '\n' in ' '.join(parts):
424 raise CommandError('new line found in the command!')
425 self._write_line(" ".join(parts))
427 def _read_binary(self, amount):
430 result = self._rbfile.read(amount)
433 raise ConnectionError("Connection lost while reading binary content")
435 amount -= len(result)
438 def _read_line(self, binary=False):
440 line = self._rbfile.readline().decode('utf-8')
442 line = self._rfile.readline()
443 if not line.endswith("\n"):
445 raise ConnectionError("Connection lost while reading line")
446 line = line.rstrip("\n")
447 if line.startswith(ERROR_PREFIX):
448 error = line[len(ERROR_PREFIX):].strip()
449 raise CommandError(error)
450 if self._command_list is not None:
454 raise ProtocolError(f"Got unexpected '{SUCCESS}'")
455 elif line == SUCCESS:
459 def _read_pair(self, separator, binary=False):
460 line = self._read_line(binary=binary)
463 pair = line.split(separator, 1)
465 raise ProtocolError(f"Could not parse pair: '{line}'")
468 def _read_pairs(self, separator=": ", binary=False):
469 pair = self._read_pair(separator, binary=binary)
472 pair = self._read_pair(separator, binary=binary)
474 def _read_list(self):
476 for key, value in self._read_pairs():
479 raise ProtocolError(f"Expected key '{seen}', got '{key}'")
483 def _read_playlist(self):
484 for _, value in self._read_pairs(":"):
487 def _read_objects(self, delimiters=None):
489 if delimiters is None:
491 for key, value in self._read_pairs():
494 if key in delimiters:
498 if not isinstance(obj[key], list):
499 obj[key] = [obj[key], value]
501 obj[key].append(value)
507 def _read_command_list(self):
509 for retval in self._command_list:
512 self._command_list = None
513 self._fetch_nothing()
515 def _fetch_nothing(self):
516 line = self._read_line()
518 raise ProtocolError(f"Got unexpected return value: '{line}'")
520 def _fetch_item(self):
521 pairs = list(self._read_pairs())
527 def _fetch_list(self):
528 return self._read_list()
531 def _fetch_playlist(self):
532 return self._read_playlist()
534 def _fetch_object(self):
535 objs = list(self._read_objects())
541 def _fetch_objects(self, delimiters):
542 return self._read_objects(delimiters)
544 def _fetch_changes(self):
545 return self._fetch_objects(["cpos"])
547 def _fetch_songs(self):
548 return self._fetch_objects(["file"])
550 def _fetch_playlists(self):
551 return self._fetch_objects(["playlist"])
553 def _fetch_database(self):
554 return self._fetch_objects(["file", "directory", "playlist"])
556 def _fetch_outputs(self):
557 return self._fetch_objects(["outputid"])
559 def _fetch_plugins(self):
560 return self._fetch_objects(["plugin"])
562 def _fetch_messages(self):
563 return self._fetch_objects(["channel"])
565 def _fetch_mounts(self):
566 return self._fetch_objects(["mount"])
568 def _fetch_neighbors(self):
569 return self._fetch_objects(["neighbor"])
571 def _fetch_composite(self):
573 for key, value in self._read_pairs(binary=True):
579 # If the song file was recognized, but there is no picture, the
580 # response is successful, but is otherwise empty.
582 amount = int(obj['binary'])
584 obj['data'] = self._read_binary(amount)
585 except IOError as err:
586 raise ConnectionError(f'Error reading binary content: {err}') from err
587 data_bytes = len(obj['data'])
588 if data_bytes != amount: # can we ever get there?
589 raise ConnectionError('Error reading binary content: '
590 f'Expects {amount}B, got {data_bytes}')
591 # Fetches trailing new line
592 self._read_line(binary=True)
593 # Fetches SUCCESS code
594 self._read_line(binary=True)
598 def _fetch_command_list(self):
599 return self._read_command_list()
602 line = self._rfile.readline()
603 if not line.endswith("\n"):
604 raise ConnectionError("Connection lost while reading MPD hello")
605 line = line.rstrip("\n")
606 if not line.startswith(HELLO_PREFIX):
607 raise ProtocolError(f"Got invalid MPD hello: '{line}'")
608 self.mpd_version = line[len(HELLO_PREFIX):].strip()
611 self.mpd_version = ''
612 self._iterating = False
614 self._command_list = None
616 self._rfile = _NotConnected()
617 self._rbfile = _NotConnected()
618 self._wfile = _NotConnected()
620 def _connect_unix(self, path):
621 if not hasattr(socket, "AF_UNIX"):
622 raise ConnectionError("Unix domain sockets not supported on this platform")
624 if path.startswith('@'):
626 sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
627 sock.settimeout(self.mpd_timeout)
629 sock.settimeout(self.socket_timeout)
632 def _connect_tcp(self, host, port):
634 flags = socket.AI_ADDRCONFIG
635 except AttributeError:
638 for res in socket.getaddrinfo(host, port, socket.AF_UNSPEC,
639 socket.SOCK_STREAM, socket.IPPROTO_TCP,
641 af, socktype, proto, _, sa = res
644 sock = socket.socket(af, socktype, proto)
645 sock.settimeout(self.mpd_timeout)
647 sock.settimeout(self.socket_timeout)
649 except socket.error as socket_err:
654 raise ConnectionError(str(err))
655 raise ConnectionError("getaddrinfo returns an empty list")
658 # noidle's special case
659 if not self._pending or self._pending[0] != 'idle':
660 raise CommandError('cannot send noidle if send_idle was not called')
662 self._write_command("noidle")
663 return self._fetch_list()
665 def connect(self, host=None, port=None):
666 """Connects the MPD server
668 :param str host: hostname, IP or FQDN (defaults to `localhost` or socket, see below for details)
669 :param port: port number (defaults to 6600)
670 :type port: str or int
672 The connect method honors MPD_HOST/MPD_PORT environment variables.
674 The underlying socket also honors MPD_TIMEOUT environment variable
675 and defaults to :py:obj:`musicpd.CONNECTION_TIMEOUT` (connect command only).
677 If you want to have a timeout for each command once you got connected,
678 set its value in :py:obj:`MPDClient.socket_timeout` (in second) or at
679 module level in :py:obj:`musicpd.SOCKET_TIMEOUT`.
681 .. note:: Default host/port
683 If host evaluate to :py:obj:`False`
684 * use ``MPD_HOST`` environment variable if set, extract password if present,
685 * else looks for an existing file in ``${XDG_RUNTIME_DIR:-/run/}/mpd/socket``
686 * else set host to ``localhost``
688 If port evaluate to :py:obj:`False`
689 * if ``MPD_PORT`` environment variable is set, use it for port
700 if self._sock is not None:
701 raise ConnectionError("Already connected")
702 if host[0] in ['/', '@']:
703 self._sock = self._connect_unix(host)
705 self._sock = self._connect_tcp(host, port)
706 self._rfile = self._sock.makefile("r", encoding='utf-8', errors='surrogateescape')
707 self._rbfile = self._sock.makefile("rb")
708 self._wfile = self._sock.makefile("w", encoding='utf-8')
716 def socket_timeout(self):
717 """Socket timeout in second (defaults to :py:obj:`SOCKET_TIMEOUT`).
718 Use None to disable socket timout."""
719 return self._socket_timeout
721 @socket_timeout.setter
722 def socket_timeout(self, timeout):
723 self._socket_timeout = timeout
724 if getattr(self._sock, 'settimeout', False):
725 self._sock.settimeout(self._socket_timeout)
727 def disconnect(self):
728 """Closes the MPD connection.
729 The client closes the actual socket, it does not use the
730 'close' request from MPD protocol (as suggested in documentation).
732 if hasattr(self._rfile, 'close'):
734 if hasattr(self._rbfile, 'close'):
736 if hasattr(self._wfile, 'close'):
738 if hasattr(self._sock, 'close'):
746 def __exit__(self, exception_type, exception_value, exception_traceback):
750 """Return the socket’s file descriptor (a small integer).
751 This is useful with :py:obj:`select.select`.
753 if self._sock is None:
754 raise ConnectionError("Not connected")
755 return self._sock.fileno()
757 def command_list_ok_begin(self):
758 if self._command_list is not None:
759 raise CommandListError("Already in command list")
761 raise IteratingError("Cannot begin command list while iterating")
763 raise PendingCommandError("Cannot begin command list with pending commands")
764 self._write_command("command_list_ok_begin")
765 self._command_list = []
767 def command_list_end(self):
768 if self._command_list is None:
769 raise CommandListError("Not in command list")
771 raise IteratingError("Already iterating over a command list")
772 self._write_command("command_list_end")
773 return self._fetch_command_list()
777 return text.replace("\\", "\\\\").replace('"', '\\"')
779 # vim: set expandtab shiftwidth=4 softtabstop=4 textwidth=79: