2014-12-01 20:55:28 +01:00
|
|
|
#!/usr/bin/env python3
|
2012-04-14 13:26:27 +02:00
|
|
|
|
|
|
|
|
2019-09-22 18:41:02 +02:00
|
|
|
import concurrent.futures
|
2013-02-28 18:46:10 +01:00
|
|
|
import configparser
|
2023-01-08 19:09:19 +01:00
|
|
|
import dateutil.parser
|
2014-12-01 20:55:28 +01:00
|
|
|
import logging
|
2012-04-15 11:58:31 +02:00
|
|
|
import os
|
2013-02-25 17:32:33 +01:00
|
|
|
import queue
|
2015-01-31 13:02:50 +01:00
|
|
|
import re
|
2014-12-01 20:55:28 +01:00
|
|
|
import socket
|
2013-02-28 18:46:10 +01:00
|
|
|
import threading
|
2016-03-12 12:56:22 +01:00
|
|
|
|
2016-08-01 13:08:27 +02:00
|
|
|
from mcg.utils import SortOrder
|
2018-11-04 17:44:52 +01:00
|
|
|
from mcg.utils import Utils
|
2016-08-01 13:08:27 +02:00
|
|
|
|
2013-02-28 18:46:10 +01:00
|
|
|
|
2012-04-14 13:26:27 +02:00
|
|
|
|
2013-02-25 17:32:33 +01:00
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
class MPDException(Exception):
|
2015-01-31 13:02:50 +01:00
|
|
|
def __init__(self, error):
|
|
|
|
super(MPDException, self).__init__(self._parse_error(error))
|
2021-04-18 17:09:35 +02:00
|
|
|
self._error = error
|
2015-01-31 13:02:50 +01:00
|
|
|
|
|
|
|
|
|
|
|
def _parse_error(self, error):
|
2016-04-08 23:03:47 +02:00
|
|
|
if error:
|
|
|
|
parts = re.match("\[(\d+)@(\d+)\]\s\{(\w+)\}\s(.*)", error)
|
|
|
|
if parts:
|
2019-09-22 18:41:02 +02:00
|
|
|
self._error_number = int(parts.group(1))
|
2016-04-08 23:03:47 +02:00
|
|
|
self._command_number = int(parts.group(2))
|
|
|
|
self._command_name = parts.group(3)
|
|
|
|
return parts.group(4)
|
|
|
|
return error
|
2015-01-31 13:02:50 +01:00
|
|
|
|
|
|
|
|
|
|
|
def get_error(self):
|
|
|
|
return self._error
|
|
|
|
|
|
|
|
|
2021-04-18 17:09:35 +02:00
|
|
|
def get_error_number(self):
|
|
|
|
return self._error_number
|
|
|
|
|
|
|
|
|
2015-01-31 13:02:50 +01:00
|
|
|
def get_command_number(self):
|
|
|
|
return self._command_number
|
|
|
|
|
|
|
|
|
|
|
|
def get_command_name(self):
|
|
|
|
return self._command_name
|
2013-02-25 17:32:33 +01:00
|
|
|
|
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
class ConnectionException(MPDException):
|
|
|
|
pass
|
2013-02-25 17:32:33 +01:00
|
|
|
|
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
class ProtocolException(MPDException):
|
|
|
|
pass
|
2013-02-25 17:32:33 +01:00
|
|
|
|
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
class CommandException(MPDException):
|
|
|
|
pass
|
2013-02-25 17:32:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-09-22 18:41:02 +02:00
|
|
|
class Future(concurrent.futures.Future):
|
|
|
|
def __init__(self, signal):
|
|
|
|
concurrent.futures.Future.__init__(self)
|
|
|
|
self._signal = signal
|
|
|
|
|
|
|
|
|
|
|
|
def get_signal(self):
|
|
|
|
return self._signal
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
class Base():
|
|
|
|
def __init__(self):
|
|
|
|
self._callbacks = {}
|
2013-02-25 17:32:33 +01:00
|
|
|
|
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
def connect_signal(self, signal, callback):
|
|
|
|
"""Connect a callback function to a signal (event)."""
|
|
|
|
self._callbacks[signal] = callback
|
2013-02-25 17:32:33 +01:00
|
|
|
|
2012-06-22 15:33:48 +02:00
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
def disconnect_signal(self, signal):
|
|
|
|
"""Disconnect a callback function from a signal (event)."""
|
|
|
|
if self._has_callback(signal):
|
|
|
|
del self._callbacks[signal]
|
2012-04-15 11:58:31 +02:00
|
|
|
|
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
def _has_callback(self, signal):
|
|
|
|
"""Check if there is a registered callback function for a signal."""
|
|
|
|
return signal in self._callbacks
|
2012-04-14 13:26:27 +02:00
|
|
|
|
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
def _callback(self, signal, *data):
|
|
|
|
if signal in self._callbacks:
|
|
|
|
callback = self._callbacks[signal]
|
|
|
|
callback(*data)
|
2013-02-25 17:32:33 +01:00
|
|
|
|
2012-04-19 18:28:17 +02:00
|
|
|
|
2019-09-22 18:41:02 +02:00
|
|
|
def _callback_future(self, future):
|
|
|
|
self._callback(future.get_signal(), *future.result())
|
|
|
|
|
|
|
|
|
2012-04-19 18:28:17 +02:00
|
|
|
|
2012-04-21 16:31:14 +02:00
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
class Client(Base):
|
|
|
|
"""Client library for handling the connection to the Music Player Daemon.
|
2012-04-21 16:31:14 +02:00
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
This class implements an album-based MPD client. It offers a non-blocking
|
|
|
|
threaded worker model for use in graphical environments.
|
|
|
|
"""
|
|
|
|
# Protocol: greeting mark
|
|
|
|
PROTOCOL_GREETING = 'OK MPD '
|
|
|
|
# Protocol: completion mark
|
|
|
|
PROTOCOL_COMPLETION = 'OK'
|
|
|
|
# Protocol: error mark
|
|
|
|
PROTOCOL_ERROR = 'ACK '
|
2015-01-31 13:03:59 +01:00
|
|
|
# Protocol: error: permission
|
|
|
|
PROTOCOL_ERROR_PERMISSION = 4
|
2019-09-22 18:41:02 +02:00
|
|
|
# Protocol: error: no exists
|
|
|
|
PROTOCOL_ERROR_NOEXISTS = 50
|
2014-12-01 20:55:28 +01:00
|
|
|
# Signal: connection status
|
|
|
|
SIGNAL_CONNECTION = 'connection'
|
|
|
|
# Signal: status
|
|
|
|
SIGNAL_STATUS = 'status'
|
2017-12-24 22:29:43 +01:00
|
|
|
# Signal: stats
|
|
|
|
SIGNAL_STATS = 'stats'
|
2020-03-22 00:19:45 +01:00
|
|
|
# Signal: init loading of albums
|
|
|
|
SIGNAL_INIT_ALBUMS = 'init-albums'
|
|
|
|
# Signal: pulse loading of albums
|
|
|
|
SIGNAL_PULSE_ALBUMS = 'pulse-albums'
|
2014-12-01 20:55:28 +01:00
|
|
|
# Signal: load albums
|
|
|
|
SIGNAL_LOAD_ALBUMS = 'load-albums'
|
|
|
|
# Signal: load playlist
|
|
|
|
SIGNAL_LOAD_PLAYLIST = 'load-playlist'
|
2017-12-25 13:16:56 +01:00
|
|
|
# Signal: load audio output devices
|
|
|
|
SIGNAL_LOAD_OUTPUT_DEVICES = 'load-output-devices'
|
2019-09-22 18:41:02 +02:00
|
|
|
# Signal: load albumart
|
|
|
|
SIGNAL_LOAD_ALBUMART = 'albumart'
|
2018-11-04 19:09:18 +01:00
|
|
|
# Signal: custom (dummy) event to trigger callback
|
|
|
|
SIGNAL_CUSTOM = 'custom'
|
2014-12-01 20:55:28 +01:00
|
|
|
# Signal: error
|
|
|
|
SIGNAL_ERROR = 'error'
|
2018-11-04 12:17:09 +01:00
|
|
|
# Buffer size for reading from socket
|
|
|
|
SOCKET_BUFSIZE = 4096
|
|
|
|
|
2012-04-21 16:31:14 +02:00
|
|
|
|
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
def __init__(self):
|
|
|
|
"""Set class variables and instantiates the Client."""
|
|
|
|
Base.__init__(self)
|
|
|
|
self._logger = logging.getLogger(__name__)
|
|
|
|
self._sock = None
|
2018-11-04 12:17:09 +01:00
|
|
|
self._buffer = bytearray()
|
2014-12-01 20:55:28 +01:00
|
|
|
self._sock_write = None
|
|
|
|
self._stop = threading.Event()
|
|
|
|
self._actions = queue.Queue()
|
|
|
|
self._worker = None
|
|
|
|
self._idling = False
|
|
|
|
self._host = None
|
|
|
|
self._albums = {}
|
|
|
|
self._playlist = []
|
|
|
|
self._state = None
|
2012-06-19 02:36:23 +02:00
|
|
|
|
2012-04-15 16:54:22 +02:00
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
def get_logger(self):
|
|
|
|
return self._logger
|
2012-04-15 16:54:22 +02:00
|
|
|
|
2012-06-24 00:13:24 +02:00
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
# Client commands
|
2012-06-24 00:13:24 +02:00
|
|
|
|
2019-09-22 18:41:02 +02:00
|
|
|
def connect(self, host, port, password=None):
|
2014-12-01 20:55:28 +01:00
|
|
|
"""Connect to MPD with the given host, port and password or with
|
|
|
|
standard values.
|
|
|
|
"""
|
|
|
|
self._logger.info("connect")
|
|
|
|
self._host = host
|
|
|
|
self._add_action(self._connect, host, port, password)
|
|
|
|
self._stop.clear()
|
|
|
|
self._start_worker()
|
2012-06-20 03:14:28 +02:00
|
|
|
|
2013-03-02 04:59:32 +01:00
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
def is_connected(self):
|
|
|
|
"""Return the connection status."""
|
2014-12-01 21:19:49 +01:00
|
|
|
return self._worker is not None and self._worker.is_alive()
|
2012-06-20 03:14:28 +02:00
|
|
|
|
2013-02-25 17:32:33 +01:00
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
def disconnect(self):
|
|
|
|
"""Disconnect from the connected MPD."""
|
|
|
|
self._logger.info("disconnect")
|
|
|
|
self._stop.set()
|
|
|
|
self._add_action(self._disconnect)
|
2012-06-24 00:13:24 +02:00
|
|
|
|
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
def join(self):
|
|
|
|
self._actions.join()
|
2012-06-24 01:15:07 +02:00
|
|
|
|
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
def get_status(self):
|
|
|
|
"""Determine the current status."""
|
|
|
|
self._logger.info("get status")
|
2019-09-22 18:41:02 +02:00
|
|
|
self._add_action_signal(Client.SIGNAL_STATUS, self._get_status)
|
2013-10-16 15:30:28 +02:00
|
|
|
|
|
|
|
|
2017-12-24 22:29:43 +01:00
|
|
|
def get_stats(self):
|
|
|
|
"""Load statistics."""
|
|
|
|
self._logger.info("get stats")
|
2019-09-22 18:41:02 +02:00
|
|
|
self._add_action_signal(Client.SIGNAL_STATS, self._get_stats)
|
2017-12-24 22:29:43 +01:00
|
|
|
|
|
|
|
|
2017-12-25 13:16:56 +01:00
|
|
|
def get_output_devices(self):
|
|
|
|
"""Determine the list of audio output devices."""
|
|
|
|
self._logger.info("get output devices")
|
2019-09-22 18:41:02 +02:00
|
|
|
self._add_action_signal(Client.SIGNAL_LOAD_OUTPUT_DEVICES, self._get_output_devices)
|
2017-12-25 13:16:56 +01:00
|
|
|
|
|
|
|
|
|
|
|
def enable_output_device(self, device, enabled):
|
|
|
|
"""Enable/disable an audio output device."""
|
|
|
|
self._logger.info("enable output device")
|
|
|
|
self._add_action(self._enable_output_device, device, enabled)
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
def load_albums(self):
|
|
|
|
self._logger.info("load albums")
|
2019-09-22 18:41:02 +02:00
|
|
|
self._add_action_signal(Client.SIGNAL_LOAD_ALBUMS, self._load_albums)
|
2014-12-01 20:55:28 +01:00
|
|
|
|
2012-04-15 11:58:31 +02:00
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
def update(self):
|
|
|
|
self._logger.info("update")
|
|
|
|
self._add_action(self._update)
|
2012-04-19 18:28:17 +02:00
|
|
|
|
2012-04-15 11:58:31 +02:00
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
def load_playlist(self):
|
|
|
|
self._logger.info("load playlist")
|
2019-09-22 18:41:02 +02:00
|
|
|
self._add_action_signal(Client.SIGNAL_LOAD_PLAYLIST, self._load_playlist)
|
2014-12-01 20:55:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
def clear_playlist(self):
|
|
|
|
"""Clear the current playlist"""
|
|
|
|
self._logger.info("clear playlist")
|
|
|
|
self._add_action(self._clear_playlist)
|
|
|
|
|
|
|
|
|
2016-08-07 21:13:07 +02:00
|
|
|
def remove_album_from_playlist(self, album):
|
|
|
|
"""Remove the given album from the playlist."""
|
|
|
|
self._logger.info("remove album from playlist")
|
|
|
|
self._add_action(self._remove_album_from_playlist, album)
|
|
|
|
|
|
|
|
|
2017-08-26 20:43:41 +02:00
|
|
|
def remove_albums_from_playlist(self, albums):
|
|
|
|
"""Remove multiple albums from the playlist in one step."""
|
|
|
|
self._logger.info("remove multiple albums from playlist")
|
|
|
|
self._add_action(self._remove_albums_from_playlist, albums)
|
|
|
|
|
|
|
|
|
2016-08-07 21:20:44 +02:00
|
|
|
def play_album_from_playlist(self, album):
|
|
|
|
"""Play the given album from the playlist."""
|
|
|
|
self._logger.info("play album from playlist")
|
|
|
|
self._add_action(self._play_album_from_playlist, album)
|
|
|
|
|
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
def playpause(self):
|
|
|
|
"""Play or pauses the current state."""
|
|
|
|
self._logger.info("playpause")
|
|
|
|
self._add_action(self._playpause)
|
|
|
|
|
|
|
|
|
|
|
|
def play_album(self, album):
|
2018-09-01 19:21:33 +02:00
|
|
|
"""Add the given album to the queue and play it immediately."""
|
2014-12-01 20:55:28 +01:00
|
|
|
self._logger.info("play album")
|
|
|
|
self._add_action(self._play_album, album)
|
|
|
|
|
|
|
|
|
2018-09-01 19:21:33 +02:00
|
|
|
def queue_album(self, album):
|
|
|
|
"""Add the given album to the queue."""
|
|
|
|
self._logger.info("play album")
|
|
|
|
self._add_action(self._queue_album, album)
|
|
|
|
|
|
|
|
|
|
|
|
def queue_albums(self, albums):
|
|
|
|
"""Add the given albums to the queue."""
|
2017-08-27 11:51:22 +02:00
|
|
|
self._logger.info("play albums")
|
2018-09-01 19:21:33 +02:00
|
|
|
self._add_action(self._queue_albums, albums)
|
2017-08-27 11:51:22 +02:00
|
|
|
|
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
def seek(self, pos, time):
|
|
|
|
"""Seeks to a song at a position"""
|
|
|
|
self._logger.info("seek")
|
|
|
|
self._add_action(self._seek, pos, time)
|
|
|
|
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
self._logger.info("stop")
|
|
|
|
self._add_action(self._stop)
|
|
|
|
|
|
|
|
|
|
|
|
def set_volume(self, volume):
|
|
|
|
self._logger.info("set volume")
|
|
|
|
self._add_action(self._set_volume, volume)
|
|
|
|
|
|
|
|
|
2019-09-22 18:41:02 +02:00
|
|
|
def get_albumart(self, album):
|
|
|
|
self._logger.info("get albumart")
|
|
|
|
self._add_action_signal(Client.SIGNAL_LOAD_ALBUMART, self._get_albumart, album)
|
|
|
|
|
|
|
|
|
|
|
|
def get_albumart_now(self, album):
|
|
|
|
self._logger.info("get albumart now")
|
|
|
|
future = concurrent.futures.Future()
|
|
|
|
self._add_action_future(future, self._get_albumart, album)
|
|
|
|
(_, albumart) = future.result()
|
|
|
|
return albumart
|
|
|
|
|
|
|
|
|
2018-11-04 19:09:18 +01:00
|
|
|
def get_custom(self, name):
|
|
|
|
self._logger.info("get custom \"%s\"", name)
|
2019-09-22 18:41:02 +02:00
|
|
|
self._add_action_signal(Client.SIGNAL_CUSTOM, self._get_custom, name)
|
2018-11-04 19:09:18 +01:00
|
|
|
|
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
# Private methods
|
|
|
|
|
|
|
|
def _connect(self, host, port, password):
|
|
|
|
self._logger.info("connecting to host %r, port %r", host, port)
|
|
|
|
if self._sock is not None:
|
|
|
|
return
|
|
|
|
try:
|
|
|
|
self._sock = self._connect_socket(host, port)
|
|
|
|
self._sock_write = self._sock.makefile("w", encoding="utf-8")
|
|
|
|
self._greet()
|
|
|
|
self._logger.info("connected")
|
|
|
|
if password:
|
|
|
|
self._logger.info("setting password")
|
|
|
|
self._call("password", password)
|
|
|
|
self._set_connection_status(True)
|
|
|
|
except OSError as e:
|
|
|
|
raise ConnectionException("connection failed: {}".format(e))
|
|
|
|
|
|
|
|
|
|
|
|
def _connect_socket(self, host, port):
|
|
|
|
sock = None
|
|
|
|
error = None
|
|
|
|
for res in socket.getaddrinfo(host, port, socket.AF_UNSPEC, socket.SOCK_STREAM, socket.IPPROTO_TCP):
|
|
|
|
af, socktype, proto, canonname, sa = res
|
|
|
|
try:
|
|
|
|
sock = socket.socket(af, socktype, proto)
|
|
|
|
sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
|
|
|
|
sock.connect(sa)
|
|
|
|
return sock
|
|
|
|
except Exception as e:
|
|
|
|
error = e
|
|
|
|
if sock is not None:
|
|
|
|
sock.close()
|
|
|
|
break
|
|
|
|
if error is not None:
|
|
|
|
raise ConnectionException("connection failed: {}".format(error))
|
|
|
|
else:
|
|
|
|
raise ConnectionException("no suitable socket")
|
|
|
|
|
|
|
|
|
|
|
|
def _greet(self):
|
2018-11-04 12:17:09 +01:00
|
|
|
greeting = self._read_line()
|
2014-12-01 20:55:28 +01:00
|
|
|
self._logger.debug("greeting: %s", greeting.strip())
|
|
|
|
if not greeting.startswith(Client.PROTOCOL_GREETING):
|
|
|
|
self._disconnect_socket()
|
|
|
|
raise ProtocolException("invalid greeting: {}".format(greeting))
|
|
|
|
self._protocol_version = greeting[len(Client.PROTOCOL_GREETING):].strip()
|
|
|
|
self._logger.debug("protocol version: %s", self._protocol_version)
|
|
|
|
|
|
|
|
|
|
|
|
def _disconnect(self):
|
|
|
|
self._logger.info("disconnecting")
|
|
|
|
self._disconnect_socket()
|
|
|
|
|
|
|
|
|
|
|
|
def _disconnect_socket(self):
|
|
|
|
if self._sock_write is not None:
|
|
|
|
self._sock_write.close()
|
2014-12-01 21:19:49 +01:00
|
|
|
self._sock_write = None
|
2014-12-01 20:55:28 +01:00
|
|
|
if self._sock is not None:
|
|
|
|
self._sock.close()
|
2014-12-01 21:19:49 +01:00
|
|
|
self._sock = None
|
2014-12-01 20:55:28 +01:00
|
|
|
self._logger.info("disconnected")
|
|
|
|
self._set_connection_status(False)
|
|
|
|
|
|
|
|
|
|
|
|
def _idle(self):
|
|
|
|
"""React to idle events from MPD."""
|
|
|
|
self._logger.info("idle")
|
|
|
|
self._idling = True
|
|
|
|
subsystems = self._parse_dict(self._call("idle"))
|
|
|
|
self._idling = False
|
|
|
|
self._logger.info("idle subsystems: %r", subsystems)
|
|
|
|
if subsystems:
|
|
|
|
if subsystems['changed'] == 'player':
|
2017-04-23 12:08:10 +02:00
|
|
|
self.load_playlist()
|
2020-08-02 17:39:04 +02:00
|
|
|
self.get_status()
|
2014-12-01 20:55:28 +01:00
|
|
|
if subsystems['changed'] == 'mixer':
|
|
|
|
self.get_status()
|
|
|
|
if subsystems['changed'] == 'playlist':
|
|
|
|
self.load_playlist()
|
|
|
|
if subsystems['changed'] == 'database':
|
|
|
|
self.load_albums()
|
|
|
|
self.load_playlist()
|
|
|
|
self.get_status()
|
|
|
|
if subsystems['changed'] == 'update':
|
|
|
|
self.load_albums()
|
|
|
|
self.load_playlist()
|
|
|
|
self.get_status()
|
2017-12-25 13:16:56 +01:00
|
|
|
if subsystems['changed'] == 'output':
|
|
|
|
self.get_output_devices()
|
|
|
|
self.get_status()
|
2014-12-01 20:55:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
def _noidle(self):
|
|
|
|
if self._idling:
|
|
|
|
self._logger.debug("noidle")
|
|
|
|
self._write("noidle")
|
|
|
|
|
|
|
|
|
|
|
|
def _get_status(self):
|
|
|
|
"""Action: Perform the real status determination."""
|
|
|
|
self._logger.info("getting status")
|
|
|
|
status = self._parse_dict(self._call("status"))
|
|
|
|
self._logger.debug("status: %r", status)
|
|
|
|
|
|
|
|
# State
|
|
|
|
state = None
|
|
|
|
if 'state' in status:
|
|
|
|
state = status['state']
|
|
|
|
self._state = state
|
|
|
|
# Time
|
|
|
|
time = 0
|
|
|
|
if 'time' in status:
|
|
|
|
time = int(status['time'].split(':')[0])
|
|
|
|
# Volume
|
2022-09-11 12:24:56 +02:00
|
|
|
volume = -1
|
2014-12-01 20:55:28 +01:00
|
|
|
if 'volume' in status:
|
|
|
|
volume = int(status['volume'])
|
|
|
|
# Error
|
|
|
|
error = None
|
|
|
|
if 'error' in status:
|
|
|
|
error = status['error']
|
|
|
|
# Album
|
2017-12-24 15:47:12 +01:00
|
|
|
file = None
|
2014-12-01 20:55:28 +01:00
|
|
|
album = None
|
|
|
|
pos = 0
|
|
|
|
song = self._parse_dict(self._call("currentsong"))
|
|
|
|
if song:
|
2017-12-24 15:47:12 +01:00
|
|
|
# File
|
|
|
|
if 'file' in song:
|
|
|
|
file = song['file']
|
2017-06-04 14:11:09 +02:00
|
|
|
# Track
|
|
|
|
track = self._extract_playlist_track(song)
|
|
|
|
if track:
|
|
|
|
# Album
|
|
|
|
album = self._extract_album(song)
|
|
|
|
# Position
|
|
|
|
pos = track.get_pos()
|
|
|
|
for palbum in self._playlist:
|
|
|
|
if palbum == album and len(palbum.get_tracks()) >= pos:
|
|
|
|
album = palbum
|
|
|
|
break
|
|
|
|
pos = pos - len(palbum.get_tracks())
|
2017-12-24 15:47:12 +01:00
|
|
|
# Audio
|
|
|
|
audio = None
|
|
|
|
if 'audio' in status:
|
|
|
|
audio = status['audio']
|
|
|
|
# Bitrate
|
|
|
|
bitrate = None
|
|
|
|
if 'bitrate' in status:
|
|
|
|
bitrate = status['bitrate']
|
2019-09-22 18:41:02 +02:00
|
|
|
return (state, album, pos, time, volume, file, audio, bitrate, error)
|
2014-12-01 20:55:28 +01:00
|
|
|
|
|
|
|
|
2017-12-24 22:29:43 +01:00
|
|
|
def _get_stats(self):
|
|
|
|
"""Action: Perform the real statistics gathering."""
|
|
|
|
self._logger.info("getting statistics")
|
|
|
|
stats = self._parse_dict(self._call("stats"))
|
|
|
|
self._logger.debug("stats: %r", stats)
|
|
|
|
|
|
|
|
# Artists
|
|
|
|
artists = 0
|
|
|
|
if 'artists' in stats:
|
|
|
|
artists = int(stats['artists'])
|
|
|
|
# Albums
|
|
|
|
albums = 0
|
|
|
|
if 'albums' in stats:
|
|
|
|
albums = int(stats['albums'])
|
|
|
|
# Songs
|
|
|
|
songs = 0
|
|
|
|
if 'songs' in stats:
|
|
|
|
songs = int(stats['songs'])
|
|
|
|
# Database playtime
|
|
|
|
dbplaytime = 0
|
|
|
|
if 'db_playtime' in stats:
|
|
|
|
dbplaytime = stats['db_playtime']
|
|
|
|
# Playtime
|
|
|
|
playtime = 0
|
|
|
|
if 'playtime' in stats:
|
|
|
|
playtime = stats['playtime']
|
|
|
|
# Uptime
|
|
|
|
uptime = 0
|
|
|
|
if 'uptime' in stats:
|
|
|
|
uptime = stats['uptime']
|
2019-09-22 18:41:02 +02:00
|
|
|
return (artists, albums, songs, dbplaytime, playtime, uptime)
|
2017-12-24 22:29:43 +01:00
|
|
|
|
|
|
|
|
2017-12-25 13:16:56 +01:00
|
|
|
def _get_output_devices(self):
|
|
|
|
"""Action: Perform the real loading of output devices."""
|
|
|
|
devices = []
|
|
|
|
for output in self._parse_list(self._call('outputs'), ['outputid']):
|
|
|
|
device = OutputDevice(output['outputid'], output['outputname'])
|
|
|
|
device.set_enabled(int(output['outputenabled']) == 1)
|
|
|
|
devices.append(device)
|
2019-09-22 18:41:02 +02:00
|
|
|
return (devices, )
|
2017-12-25 13:16:56 +01:00
|
|
|
|
|
|
|
|
|
|
|
def _enable_output_device(self, device, enabled):
|
|
|
|
"""Action: Perform the real enabling/disabling of an output device."""
|
|
|
|
if enabled:
|
|
|
|
self._call('enableoutput ', device.get_id())
|
|
|
|
else:
|
|
|
|
self._call('disableoutput ', device.get_id())
|
|
|
|
|
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
def _load_albums(self):
|
|
|
|
"""Action: Perform the real update."""
|
2020-03-22 00:19:45 +01:00
|
|
|
self._callback(Client.SIGNAL_INIT_ALBUMS)
|
2014-12-01 20:55:28 +01:00
|
|
|
self._albums = {}
|
2014-12-02 20:10:20 +01:00
|
|
|
# Albums
|
2017-06-04 14:11:09 +02:00
|
|
|
for album in self._parse_list(self._call('list album'), ['album']):
|
2020-03-22 00:19:45 +01:00
|
|
|
self._callback(Client.SIGNAL_PULSE_ALBUMS)
|
|
|
|
|
2017-06-04 14:11:09 +02:00
|
|
|
# Album
|
|
|
|
album = self._extract_album(album)
|
2014-12-02 20:10:20 +01:00
|
|
|
self._logger.debug("album: %r", album)
|
|
|
|
# Tracks
|
2017-06-04 14:11:09 +02:00
|
|
|
for song in self._parse_list(self._call('find album ', album.get_title()), ['file']):
|
|
|
|
track = self._extract_track(song)
|
|
|
|
if track:
|
2014-12-02 20:10:20 +01:00
|
|
|
self._logger.debug("track: %r", track)
|
2014-12-01 20:55:28 +01:00
|
|
|
album.add_track(track)
|
2019-09-22 18:41:02 +02:00
|
|
|
return (self._albums, )
|
2014-12-01 20:55:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
def _update(self):
|
|
|
|
self._call('update')
|
|
|
|
|
|
|
|
|
|
|
|
def _load_playlist(self):
|
|
|
|
self._playlist = []
|
|
|
|
for song in self._parse_list(self._call('playlistinfo'), ['file', 'playlist']):
|
|
|
|
self._logger.debug("song: %r", song)
|
|
|
|
# Track
|
2017-06-04 14:11:09 +02:00
|
|
|
track = self._extract_playlist_track(song)
|
|
|
|
self._logger.debug("track: %r", track)
|
2014-12-01 20:55:28 +01:00
|
|
|
# Album
|
2017-06-04 14:11:09 +02:00
|
|
|
album = self._extract_album(song, lookup=False)
|
2018-11-04 17:44:52 +01:00
|
|
|
if len(self._playlist) == 0 or self._playlist[len(self._playlist)-1] != album:
|
2014-12-01 20:55:28 +01:00
|
|
|
self._playlist.append(album)
|
|
|
|
else:
|
|
|
|
album = self._playlist[len(self._playlist)-1]
|
|
|
|
self._logger.debug("album: %r", album)
|
|
|
|
if track:
|
|
|
|
album.add_track(track)
|
2019-09-22 18:41:02 +02:00
|
|
|
return (self._playlist, )
|
2014-12-01 20:55:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
def _clear_playlist(self):
|
|
|
|
"""Action: Perform the real clearing of the current playlist."""
|
|
|
|
self._call('clear')
|
|
|
|
|
|
|
|
|
2016-08-07 21:13:07 +02:00
|
|
|
def _remove_album_from_playlist(self, album):
|
|
|
|
self._call_list('command_list_begin')
|
|
|
|
for track in album.get_tracks():
|
|
|
|
self._call_list('deleteid', track.get_id())
|
2017-08-26 20:43:41 +02:00
|
|
|
self._call('command_list_end')
|
|
|
|
|
|
|
|
|
|
|
|
def _remove_albums_from_playlist(self, albums):
|
|
|
|
self._call_list('command_list_begin')
|
|
|
|
for album in albums:
|
|
|
|
for track in album.get_tracks():
|
|
|
|
self._call_list('deleteid', track.get_id())
|
2016-08-07 21:13:07 +02:00
|
|
|
self._call('command_list_end')
|
|
|
|
|
|
|
|
|
2016-08-07 21:20:44 +02:00
|
|
|
def _play_album_from_playlist(self, album):
|
|
|
|
if album.get_tracks():
|
|
|
|
self._call('playid', album.get_tracks()[0].get_id())
|
|
|
|
|
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
def _playpause(self):
|
|
|
|
"""Action: Perform the real play/pause command."""
|
|
|
|
#status = self._parse_dict(self._call('status'))
|
|
|
|
#if 'state' in status:
|
|
|
|
if self._state == 'play':
|
|
|
|
self._call('pause')
|
|
|
|
else:
|
|
|
|
self._call('play')
|
|
|
|
|
|
|
|
|
|
|
|
def _play_album(self, album):
|
2018-09-01 19:21:33 +02:00
|
|
|
track_ids = self._queue_album(album)
|
|
|
|
if track_ids:
|
|
|
|
self._logger.info("play track %d", track_ids[0])
|
|
|
|
self._call('playid', track_ids[0])
|
|
|
|
|
|
|
|
|
|
|
|
def _queue_album(self, album):
|
|
|
|
track_ids = []
|
2014-12-01 20:55:28 +01:00
|
|
|
if album in self._albums:
|
2018-09-01 19:21:33 +02:00
|
|
|
self._logger.info("add album %s", album)
|
2014-12-01 20:55:28 +01:00
|
|
|
for track in self._albums[album].get_tracks():
|
|
|
|
self._logger.info("addid: %r", track.get_file())
|
|
|
|
track_id = None
|
|
|
|
track_id_response = self._parse_dict(self._call('addid', track.get_file()))
|
|
|
|
if 'id' in track_id_response:
|
|
|
|
track_id = track_id_response['id']
|
|
|
|
self._logger.debug("track id: %r", track_id)
|
|
|
|
if track_id is not None:
|
|
|
|
track_ids.append(track_id)
|
2018-09-01 19:21:33 +02:00
|
|
|
return track_ids
|
2014-12-01 20:55:28 +01:00
|
|
|
|
|
|
|
|
2018-09-01 19:21:33 +02:00
|
|
|
def _queue_albums(self, albums):
|
2017-08-27 11:51:22 +02:00
|
|
|
track_ids = []
|
|
|
|
for album in albums:
|
2018-09-01 19:21:33 +02:00
|
|
|
track_ids.extend(self._queue_album(album))
|
2017-08-27 11:51:22 +02:00
|
|
|
|
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
def _seek(self, pos, time):
|
|
|
|
self._call('seek', pos, time)
|
|
|
|
|
|
|
|
|
|
|
|
def _stop(self):
|
|
|
|
self._call('stop')
|
|
|
|
|
|
|
|
|
|
|
|
def _set_volume(self, volume):
|
|
|
|
self._call('setvol', volume)
|
|
|
|
|
|
|
|
|
2019-09-22 18:41:02 +02:00
|
|
|
def _get_albumart(self, album):
|
|
|
|
if album in self._albums:
|
|
|
|
album = self._albums[album]
|
|
|
|
self._logger.debug("get albumart for album \"%s\"", album.get_title())
|
|
|
|
|
2023-01-21 12:59:01 +01:00
|
|
|
# Use "albumart" command
|
|
|
|
if album.get_tracks():
|
|
|
|
try:
|
|
|
|
return (album, self._read_binary('albumart', album.get_tracks()[0].get_file(), False))
|
|
|
|
except CommandException as e:
|
|
|
|
# The "albumart" command throws an exception if not found
|
|
|
|
if e.get_error_number() != Client.PROTOCOL_ERROR_NOEXISTS:
|
|
|
|
raise e
|
|
|
|
# If no albumart can be found, use "readpicture" command
|
|
|
|
for track in album.get_tracks():
|
|
|
|
data = self._read_binary('readpicture', track.get_file(), True)
|
|
|
|
if data:
|
|
|
|
return (album, data)
|
|
|
|
|
|
|
|
return (album, None)
|
2019-09-22 18:41:02 +02:00
|
|
|
|
|
|
|
|
2018-11-04 19:09:18 +01:00
|
|
|
def _get_custom(self, name):
|
2019-09-22 18:41:02 +02:00
|
|
|
return (name, )
|
2018-11-04 19:09:18 +01:00
|
|
|
|
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
def _start_worker(self):
|
|
|
|
"""Start the worker thread which waits for action to be performed."""
|
|
|
|
self._logger.debug("start worker")
|
|
|
|
self._worker = threading.Thread(target=self._run, name='mcg-worker', args=())
|
|
|
|
self._worker.setDaemon(True)
|
|
|
|
self._worker.start()
|
|
|
|
self._logger.debug("worker started")
|
|
|
|
|
|
|
|
|
|
|
|
def _run(self):
|
|
|
|
while not self._stop.is_set() or not self._actions.empty():
|
|
|
|
if self._sock is not None and self._actions.empty():
|
|
|
|
self._add_action(self._idle)
|
|
|
|
action = self._actions.get()
|
|
|
|
self._logger.debug("next action: %r", action)
|
|
|
|
self._work(action)
|
|
|
|
self._actions.task_done()
|
|
|
|
self._logger.debug("action done")
|
|
|
|
self._logger.debug("worker finished")
|
|
|
|
|
|
|
|
|
|
|
|
def _add_action(self, method, *args):
|
|
|
|
"""Add an action to the action list."""
|
|
|
|
self._logger.debug("add action %r (%r)", method.__name__, args)
|
2019-09-22 18:41:02 +02:00
|
|
|
future = concurrent.futures.Future()
|
|
|
|
action = (future, method, args)
|
|
|
|
self._actions.put(action)
|
|
|
|
self._noidle()
|
|
|
|
|
|
|
|
return future
|
|
|
|
|
|
|
|
|
|
|
|
def _add_action_signal(self, signal, method, *args):
|
|
|
|
"""Add an action to the action list that triggers a callback."""
|
|
|
|
self._logger.debug("add action signal %r: %r (%r)", signal, method.__name__, args)
|
|
|
|
future = Future(signal)
|
|
|
|
future.add_done_callback(self._callback_future)
|
|
|
|
self._add_action_future(future, method, *args)
|
|
|
|
|
|
|
|
return future
|
|
|
|
|
|
|
|
|
|
|
|
def _add_action_future(self, future, method, *args):
|
|
|
|
"""Add an action to the action list based on a futre."""
|
|
|
|
self._logger.debug("add action future %r (%r)", method.__name__, args)
|
|
|
|
action = (future, method, args)
|
2014-12-01 20:55:28 +01:00
|
|
|
self._actions.put(action)
|
|
|
|
self._noidle()
|
|
|
|
|
|
|
|
|
|
|
|
def _work(self, action):
|
2019-09-22 18:41:02 +02:00
|
|
|
(future, method, args) = action
|
2014-12-01 20:55:28 +01:00
|
|
|
self._logger.debug("work: %r", method.__name__)
|
|
|
|
try:
|
2019-09-22 18:41:02 +02:00
|
|
|
result = method(*args)
|
|
|
|
future.set_result(result)
|
2014-12-01 20:55:28 +01:00
|
|
|
except ConnectionException as e:
|
|
|
|
self._logger.exception(e)
|
2019-09-22 18:41:02 +02:00
|
|
|
future.set_exception(e)
|
2014-12-01 20:55:28 +01:00
|
|
|
self._callback(Client.SIGNAL_ERROR, e)
|
|
|
|
self._disconnect_socket()
|
|
|
|
except Exception as e:
|
|
|
|
self._logger.exception(e)
|
2019-09-22 18:41:02 +02:00
|
|
|
future.set_exception(e)
|
2014-12-01 20:55:28 +01:00
|
|
|
self._callback(Client.SIGNAL_ERROR, e)
|
|
|
|
|
|
|
|
|
|
|
|
def _call(self, command, *args):
|
|
|
|
try:
|
|
|
|
self._write(command, args)
|
|
|
|
return self._read()
|
|
|
|
except MPDException as e:
|
2021-04-18 17:09:35 +02:00
|
|
|
if command == 'idle' and e.get_error_number() == Client.PROTOCOL_ERROR_PERMISSION:
|
2015-01-31 13:03:59 +01:00
|
|
|
self.disconnect()
|
2014-12-01 20:55:28 +01:00
|
|
|
self._callback(Client.SIGNAL_ERROR, e)
|
|
|
|
|
|
|
|
|
2016-08-07 21:08:03 +02:00
|
|
|
def _call_list(self, command, *args):
|
|
|
|
try:
|
|
|
|
self._write(command, args)
|
|
|
|
except MPDException as e:
|
2021-04-18 17:09:35 +02:00
|
|
|
if command == 'idle' and e.get_error_number() == Client.PROTOCOL_ERROR_PERMISSION:
|
2016-08-07 21:08:03 +02:00
|
|
|
self.disconnect()
|
|
|
|
self._callback(Client.SIGNAL_ERROR, e)
|
|
|
|
|
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
def _write(self, command, args=None):
|
|
|
|
if args is not None and len(args) > 0:
|
2014-12-02 20:09:35 +01:00
|
|
|
line = '{} "{}"\n'.format(command, '" "'.join(str(x).replace('"', '\\\"') for x in args))
|
2014-12-01 20:55:28 +01:00
|
|
|
else:
|
|
|
|
line = '{}\n'.format(command)
|
|
|
|
self._logger.debug("write: %r", line)
|
|
|
|
self._sock_write.write(line)
|
|
|
|
self._sock_write.flush()
|
|
|
|
|
|
|
|
|
|
|
|
def _read(self):
|
|
|
|
self._logger.debug("reading response")
|
|
|
|
response = []
|
2018-11-04 12:17:09 +01:00
|
|
|
line = self._read_line()
|
2014-12-01 20:55:28 +01:00
|
|
|
while not line.startswith(Client.PROTOCOL_COMPLETION) and not line.startswith(Client.PROTOCOL_ERROR):
|
|
|
|
response.append(line.strip())
|
2018-11-04 12:17:09 +01:00
|
|
|
line = self._read_line()
|
2014-12-01 20:55:28 +01:00
|
|
|
if line.startswith(Client.PROTOCOL_COMPLETION):
|
|
|
|
self._logger.debug("response complete")
|
|
|
|
if line.startswith(Client.PROTOCOL_ERROR):
|
|
|
|
error = line[len(Client.PROTOCOL_ERROR):].strip()
|
|
|
|
self._logger.debug("command failed: %r", error)
|
|
|
|
raise CommandException(error)
|
|
|
|
self._logger.debug("response: %r", response)
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
2018-11-04 12:17:09 +01:00
|
|
|
def _read_line(self):
|
|
|
|
self._logger.debug("reading line")
|
|
|
|
|
|
|
|
# Read from the buffer
|
|
|
|
data = self._buffer_get_char(b'\x0A')
|
|
|
|
if not data.endswith(b'\x0A'):
|
|
|
|
# Read more from socket until next line break
|
|
|
|
while b'\x0A' not in data:
|
|
|
|
buf = self._sock.recv(Client.SOCKET_BUFSIZE)
|
|
|
|
if buf:
|
|
|
|
data += buf
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
|
|
|
|
# Get first line from data, add rest to buffer
|
|
|
|
if data:
|
|
|
|
lines = data.split(b'\x0A', 1)
|
|
|
|
data = lines[0]
|
|
|
|
self._buffer_set(lines[1])
|
|
|
|
if data:
|
|
|
|
return data.decode('utf-8')
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2023-01-21 12:59:01 +01:00
|
|
|
def _read_binary(self, command, filename, has_mimetype):
|
|
|
|
data = None
|
|
|
|
size = 1
|
|
|
|
offset = 0
|
|
|
|
index = 0
|
|
|
|
|
|
|
|
# Read data until size is reached
|
|
|
|
while offset < size:
|
|
|
|
self._write(command, args=[filename, offset])
|
|
|
|
|
|
|
|
# Read first line
|
|
|
|
line = self._read_line()
|
|
|
|
# Check first line for error
|
|
|
|
if line.startswith(Client.PROTOCOL_ERROR):
|
|
|
|
error = line[len(Client.PROTOCOL_ERROR):].strip()
|
|
|
|
self._logger.debug("command failed: %r", error)
|
|
|
|
raise CommandException(error)
|
|
|
|
# Check first line for completion
|
|
|
|
if line.startswith(Client.PROTOCOL_COMPLETION):
|
|
|
|
break
|
|
|
|
# First line is the file size
|
|
|
|
size = int(self._parse_dict([line])['size'])
|
|
|
|
self._logger.debug("size: %d", size)
|
|
|
|
# For some commands the second line is the mimetype
|
|
|
|
if has_mimetype:
|
|
|
|
mimetype = self._parse_dict([self._read_line()])['type']
|
|
|
|
# Next line is the count of bytes read
|
|
|
|
binary = int(self._parse_dict([self._read_line()])['binary'])
|
|
|
|
self._logger.debug("binary: %d", binary)
|
|
|
|
|
|
|
|
# Create new data array on the first iteration
|
|
|
|
if not data:
|
|
|
|
data = bytearray(size)
|
|
|
|
# Create a view for the current chunk of data
|
|
|
|
data_view = memoryview(data)[offset:offset+binary]
|
|
|
|
# Read actual bytes
|
|
|
|
self._read_bytes(data_view, binary)
|
|
|
|
offset += binary
|
|
|
|
# Read line break to complete previous repsonse
|
|
|
|
self._read_line()
|
|
|
|
# Read command completion
|
|
|
|
end = self._read_line()
|
|
|
|
if not end.startswith(Client.PROTOCOL_COMPLETION):
|
|
|
|
self._logger.debug("albumart not completed")
|
|
|
|
data = None
|
|
|
|
break
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
2018-11-04 12:17:09 +01:00
|
|
|
def _read_bytes(self, buf, nbytes):
|
|
|
|
self._logger.debug("reading bytes")
|
|
|
|
# Use already buffered data
|
|
|
|
buf_read = self._buffer_get_size(nbytes)
|
|
|
|
nbytes_read = len(buf_read)
|
|
|
|
buf[0:nbytes_read] = buf_read
|
|
|
|
# Read additional data from socket
|
|
|
|
nbytes = nbytes - nbytes_read
|
|
|
|
if nbytes > 0:
|
|
|
|
buf_view = memoryview(buf)[nbytes_read:]
|
|
|
|
nbytes_read += self._sock.recv_into(buf_view, nbytes)
|
|
|
|
return nbytes_read
|
|
|
|
|
|
|
|
|
|
|
|
def _buffer_get_char(self, char):
|
|
|
|
pos = self._buffer.find(char)
|
|
|
|
if pos < 0:
|
|
|
|
pos = len(self._buffer)-1
|
|
|
|
buf = self._buffer[0:pos+1]
|
|
|
|
self._buffer = self._buffer[pos+1:]
|
|
|
|
return buf
|
|
|
|
|
|
|
|
|
|
|
|
def _buffer_get_size(self, size):
|
|
|
|
buf = self._buffer[0:size]
|
|
|
|
self._logger.debug("get %d bytes from buffer", len(buf))
|
|
|
|
self._buffer = self._buffer[size:]
|
|
|
|
self._logger.debug("leaving %d in the buffer", len(self._buffer))
|
|
|
|
return buf
|
|
|
|
|
|
|
|
|
|
|
|
def _buffer_set(self, buf):
|
|
|
|
self._logger.debug("set %d %s as buffer", len(buf), type(buf))
|
|
|
|
self._buffer = buf
|
|
|
|
|
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
def _parse_dict(self, response):
|
|
|
|
dict = {}
|
2015-01-31 13:04:51 +01:00
|
|
|
if response:
|
|
|
|
for line in response:
|
|
|
|
key, value = self._split_line(line)
|
|
|
|
dict[key] = value
|
2014-12-01 20:55:28 +01:00
|
|
|
return dict
|
|
|
|
|
|
|
|
|
|
|
|
def _parse_list(self, response, delimiters):
|
|
|
|
entry = {}
|
2015-01-31 13:04:51 +01:00
|
|
|
if response:
|
|
|
|
for line in response:
|
|
|
|
key, value = self._split_line(line)
|
|
|
|
if entry and key in delimiters:
|
|
|
|
yield entry
|
|
|
|
entry = {}
|
|
|
|
#if key in entry.keys():
|
|
|
|
# if entry[key] is not list:
|
|
|
|
# entry[key] = [entry[key]]
|
|
|
|
# entry[key].append(value)
|
|
|
|
#else:
|
|
|
|
entry[key] = value
|
2014-12-01 20:55:28 +01:00
|
|
|
if entry:
|
|
|
|
yield entry
|
|
|
|
|
|
|
|
|
|
|
|
def _split_line(self, line):
|
2014-12-02 20:10:01 +01:00
|
|
|
parts = line.split(':')
|
|
|
|
return parts[0].lower(), ':'.join(parts[1:]).lstrip()
|
2014-12-01 20:55:28 +01:00
|
|
|
|
|
|
|
|
2017-06-04 14:11:09 +02:00
|
|
|
def _extract_album(self, song, lookup=True):
|
|
|
|
album = None
|
|
|
|
if 'album' not in song:
|
|
|
|
song['album'] = MCGAlbum.DEFAULT_ALBUM
|
2018-11-04 17:44:52 +01:00
|
|
|
id = Utils.generate_id(song['album'])
|
|
|
|
if lookup and id in self._albums.keys():
|
|
|
|
album = self._albums[id]
|
2017-06-04 14:11:09 +02:00
|
|
|
else:
|
2019-09-22 18:41:02 +02:00
|
|
|
album = MCGAlbum(song['album'], self._host)
|
2017-06-04 14:11:09 +02:00
|
|
|
if lookup:
|
2018-11-04 17:44:52 +01:00
|
|
|
self._albums[id] = album
|
2017-06-04 14:11:09 +02:00
|
|
|
return album
|
|
|
|
|
|
|
|
|
|
|
|
def _extract_track(self, song):
|
|
|
|
track = None
|
|
|
|
if 'artist' in song and 'title' in song and 'file' in song:
|
|
|
|
track = MCGTrack(song['artist'], song['title'], song['file'])
|
|
|
|
if 'track' in song:
|
|
|
|
track.set_track(song['track'])
|
|
|
|
if 'time' in song:
|
|
|
|
track.set_length(song['time'])
|
|
|
|
if 'date' in song:
|
|
|
|
track.set_date(song['date'])
|
2017-06-04 15:39:07 +02:00
|
|
|
if 'albumartist' in song:
|
|
|
|
track.set_albumartists(song['albumartist'])
|
2023-01-08 19:09:19 +01:00
|
|
|
if 'last-modified' in song:
|
|
|
|
track.set_last_modified(song['last-modified'])
|
2017-06-04 14:11:09 +02:00
|
|
|
return track
|
|
|
|
|
|
|
|
|
|
|
|
def _extract_playlist_track(self, song):
|
|
|
|
track = self._extract_track(song)
|
|
|
|
if track and 'id' in song and 'pos' in song:
|
|
|
|
track = MCGPlaylistTrack(track, song['id'], song['pos'])
|
|
|
|
return track
|
|
|
|
|
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
def _set_connection_status(self, status):
|
|
|
|
self._callback(Client.SIGNAL_CONNECTION, status)
|
2012-06-22 16:42:48 +02:00
|
|
|
|
|
|
|
|
2012-04-15 11:58:31 +02:00
|
|
|
|
2012-04-19 18:28:17 +02:00
|
|
|
|
2017-12-25 13:16:56 +01:00
|
|
|
class OutputDevice:
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, id, name):
|
|
|
|
self._id = id
|
|
|
|
self._name = name
|
|
|
|
self._enabled = None
|
|
|
|
|
|
|
|
|
|
|
|
def get_id(self):
|
|
|
|
return self._id
|
|
|
|
|
|
|
|
|
|
|
|
def get_name(self):
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
|
|
|
|
def set_enabled(self, enabled):
|
|
|
|
self._enabled = enabled
|
|
|
|
|
|
|
|
|
|
|
|
def is_enabled(self):
|
|
|
|
return self._enabled
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-04-19 18:28:17 +02:00
|
|
|
class MCGAlbum:
|
2014-12-01 20:55:28 +01:00
|
|
|
DEFAULT_ALBUM = 'Various'
|
2015-01-31 13:05:43 +01:00
|
|
|
_FILE_NAMES = ['cover', 'folder']
|
2014-12-01 20:55:28 +01:00
|
|
|
_FILE_EXTS = ['jpg', 'png', 'jpeg']
|
2015-01-31 13:07:20 +01:00
|
|
|
_FILTER_DELIMITER = ' '
|
2012-04-19 18:28:17 +02:00
|
|
|
|
|
|
|
|
2019-09-22 18:41:02 +02:00
|
|
|
def __init__(self, title, host):
|
2014-12-01 20:55:28 +01:00
|
|
|
self._artists = []
|
2017-06-04 15:39:07 +02:00
|
|
|
self._albumartists = []
|
2014-12-01 20:55:28 +01:00
|
|
|
self._pathes = []
|
|
|
|
if type(title) is list:
|
|
|
|
title = title[0]
|
|
|
|
self._title = title
|
|
|
|
self._dates = []
|
|
|
|
self._host = host
|
|
|
|
self._tracks = []
|
|
|
|
self._length = 0
|
2023-01-08 19:09:19 +01:00
|
|
|
self._last_modified = None
|
2018-11-04 17:44:52 +01:00
|
|
|
self._id = Utils.generate_id(title)
|
2012-04-19 18:28:17 +02:00
|
|
|
|
2013-05-30 03:44:44 +02:00
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
def __eq__(self, other):
|
2018-11-04 17:44:52 +01:00
|
|
|
return (other and self.get_id() == other.get_id())
|
|
|
|
|
|
|
|
|
|
|
|
def __hash__(self):
|
|
|
|
return hash(self._title)
|
|
|
|
|
|
|
|
|
|
|
|
def get_id(self):
|
|
|
|
return self._id
|
2013-05-30 03:44:44 +02:00
|
|
|
|
2012-04-19 18:28:17 +02:00
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
def get_artists(self):
|
2017-06-04 15:39:07 +02:00
|
|
|
if self._albumartists:
|
|
|
|
return [artist for artist in self._artists if artist not in self._albumartists]
|
|
|
|
return self._artists
|
|
|
|
|
|
|
|
|
|
|
|
def get_albumartists(self):
|
|
|
|
if self._albumartists:
|
|
|
|
return self._albumartists
|
2014-12-01 20:55:28 +01:00
|
|
|
return self._artists
|
2012-04-19 18:28:17 +02:00
|
|
|
|
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
def get_title(self):
|
|
|
|
return self._title
|
2012-04-19 18:28:17 +02:00
|
|
|
|
2013-05-30 01:36:09 +02:00
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
def get_dates(self):
|
|
|
|
return self._dates
|
2013-05-30 01:36:09 +02:00
|
|
|
|
2012-04-20 01:33:29 +02:00
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
def get_date(self):
|
|
|
|
if len(self._dates) == 0:
|
|
|
|
return None
|
|
|
|
return self._dates[0]
|
2012-04-20 01:33:29 +02:00
|
|
|
|
2012-04-19 18:28:17 +02:00
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
def get_path(self):
|
|
|
|
return self._path
|
2012-04-19 18:28:17 +02:00
|
|
|
|
2012-04-20 01:33:29 +02:00
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
def add_track(self, track):
|
|
|
|
self._tracks.append(track)
|
|
|
|
self._length = self._length + track.get_length()
|
|
|
|
for artist in track.get_artists():
|
|
|
|
if artist not in self._artists:
|
|
|
|
self._artists.append(artist)
|
2017-06-04 15:39:07 +02:00
|
|
|
for artist in track.get_albumartists():
|
|
|
|
if artist not in self._albumartists:
|
|
|
|
self._albumartists.append(artist)
|
2014-12-01 20:55:28 +01:00
|
|
|
if track.get_date() is not None and track.get_date() not in self._dates:
|
|
|
|
self._dates.append(track.get_date())
|
|
|
|
path = os.path.dirname(track.get_file())
|
|
|
|
if path not in self._pathes:
|
|
|
|
self._pathes.append(path)
|
2023-01-08 19:09:19 +01:00
|
|
|
if track.get_last_modified():
|
|
|
|
if not self._last_modified or track.get_last_modified() > self._last_modified:
|
|
|
|
self._last_modified = track.get_last_modified()
|
2012-04-20 01:33:29 +02:00
|
|
|
|
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
def get_tracks(self):
|
|
|
|
return self._tracks
|
2012-04-20 01:33:29 +02:00
|
|
|
|
2013-04-30 16:26:51 +02:00
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
def get_length(self):
|
|
|
|
return self._length
|
2013-04-30 16:26:51 +02:00
|
|
|
|
2012-04-19 18:28:17 +02:00
|
|
|
|
2023-01-08 19:09:19 +01:00
|
|
|
def get_last_modified(self):
|
|
|
|
return self._last_modified
|
|
|
|
|
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
def filter(self, filter_string):
|
2015-01-31 13:07:20 +01:00
|
|
|
if len(filter_string) == 0:
|
|
|
|
return True
|
|
|
|
keywords = filter_string.split(MCGAlbum._FILTER_DELIMITER)
|
|
|
|
for keyword in keywords:
|
|
|
|
if len(keyword) == 0:
|
|
|
|
continue
|
|
|
|
result = False
|
|
|
|
keyword = keyword.lower()
|
|
|
|
# Search in album data
|
|
|
|
for value in self._artists + [self._title] + self._dates:
|
|
|
|
if keyword in value.lower():
|
|
|
|
result = True
|
|
|
|
break
|
|
|
|
if result:
|
|
|
|
continue
|
|
|
|
# Search in track data
|
|
|
|
for track in self._tracks:
|
|
|
|
if keyword in track.get_title().lower() or keyword in track.get_file().lower():
|
|
|
|
result = True
|
|
|
|
break
|
|
|
|
if not result:
|
|
|
|
return False
|
|
|
|
return True
|
2012-06-30 14:48:53 +02:00
|
|
|
|
2012-06-30 22:58:45 +02:00
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
def compare(album1, album2, criterion=None):
|
|
|
|
if criterion == None:
|
2016-08-01 13:08:27 +02:00
|
|
|
criterion = SortOrder.TITLE
|
|
|
|
if criterion == SortOrder.ARTIST:
|
2014-12-01 20:55:28 +01:00
|
|
|
value_function = "get_artists"
|
2016-08-01 13:08:27 +02:00
|
|
|
elif criterion == SortOrder.TITLE:
|
2014-12-01 20:55:28 +01:00
|
|
|
value_function = "get_title"
|
2016-08-01 13:08:27 +02:00
|
|
|
elif criterion == SortOrder.YEAR:
|
2014-12-01 20:55:28 +01:00
|
|
|
value_function = "get_date"
|
2023-01-08 19:09:19 +01:00
|
|
|
elif criterion == SortOrder.MODIFIED:
|
|
|
|
value_function = "get_last_modified"
|
2012-06-30 22:58:45 +02:00
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
value1 = getattr(album1, value_function)()
|
|
|
|
value2 = getattr(album2, value_function)()
|
|
|
|
if value1 is None and value2 is None:
|
|
|
|
return 0
|
|
|
|
elif value1 is None:
|
|
|
|
return -1
|
|
|
|
elif value2 is None:
|
|
|
|
return 1
|
|
|
|
if value1 < value2:
|
|
|
|
return -1
|
|
|
|
elif value1 == value2:
|
|
|
|
return 0
|
|
|
|
else:
|
|
|
|
return 1
|
2012-06-30 22:58:45 +02:00
|
|
|
|
2013-02-25 17:32:33 +01:00
|
|
|
|
2012-04-20 01:33:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
class MCGTrack:
|
2017-06-04 14:11:09 +02:00
|
|
|
def __init__(self, artists, title, file):
|
2014-12-01 20:55:28 +01:00
|
|
|
if type(artists) is not list:
|
|
|
|
artists = [artists]
|
|
|
|
self._artists = artists
|
|
|
|
if type(title) is list:
|
|
|
|
title = title[0]
|
|
|
|
self._title = title
|
|
|
|
if type(file) is list:
|
|
|
|
file = file[0]
|
|
|
|
self._file = file
|
2012-04-20 01:33:29 +02:00
|
|
|
|
2017-06-04 15:39:07 +02:00
|
|
|
self._albumartists = []
|
2017-06-04 14:11:09 +02:00
|
|
|
self._track = None
|
|
|
|
self._length = 0
|
|
|
|
self._date = None
|
2023-01-08 19:09:19 +01:00
|
|
|
self._last_modified = None
|
2017-06-04 14:11:09 +02:00
|
|
|
|
2012-04-20 01:33:29 +02:00
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
def __eq__(self, other):
|
|
|
|
return self._file == other.get_file()
|
2013-05-30 01:36:09 +02:00
|
|
|
|
|
|
|
|
2018-11-04 17:44:52 +01:00
|
|
|
def __hash__(self):
|
|
|
|
return hash(self._file)
|
|
|
|
|
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
def get_artists(self):
|
2017-06-04 15:39:07 +02:00
|
|
|
if self._albumartists:
|
|
|
|
return [artist for artist in self._artists if artist not in self._albumartists]
|
|
|
|
return self._artists
|
|
|
|
|
|
|
|
|
|
|
|
def set_albumartists(self, artists):
|
|
|
|
if type(artists) is not list:
|
|
|
|
artists = [artists]
|
|
|
|
self._albumartists = artists
|
|
|
|
|
|
|
|
|
|
|
|
def get_albumartists(self):
|
|
|
|
if self._albumartists:
|
|
|
|
return self._albumartists
|
2014-12-01 20:55:28 +01:00
|
|
|
return self._artists
|
2013-02-27 19:19:27 +01:00
|
|
|
|
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
def get_title(self):
|
|
|
|
return self._title
|
2012-04-20 01:33:29 +02:00
|
|
|
|
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
def get_track(self):
|
|
|
|
return self._track
|
2012-04-20 01:33:29 +02:00
|
|
|
|
|
|
|
|
2017-06-04 14:11:09 +02:00
|
|
|
def set_track(self, track):
|
|
|
|
if type(track) is list:
|
|
|
|
track = track[0]
|
|
|
|
if type(track) is str and '/' in track:
|
|
|
|
track = track[0: track.index('/')]
|
|
|
|
if track is not None:
|
|
|
|
try:
|
|
|
|
track = int(track)
|
|
|
|
except ValueError:
|
|
|
|
track = 0
|
|
|
|
self._track = track
|
|
|
|
|
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
def get_length(self):
|
|
|
|
return self._length
|
2013-05-30 01:36:09 +02:00
|
|
|
|
|
|
|
|
2017-06-04 14:11:09 +02:00
|
|
|
def set_length(self, length):
|
|
|
|
self._length = int(length)
|
|
|
|
|
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
def get_date(self):
|
|
|
|
return self._date
|
2012-04-20 01:33:29 +02:00
|
|
|
|
|
|
|
|
2017-06-04 14:11:09 +02:00
|
|
|
def set_date(self, date):
|
|
|
|
if type(date) is list:
|
|
|
|
date = date[0]
|
|
|
|
self._date = date
|
|
|
|
|
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
def get_file(self):
|
|
|
|
return self._file
|
2012-04-20 01:33:29 +02:00
|
|
|
|
2013-02-25 17:32:33 +01:00
|
|
|
|
2023-01-08 19:09:19 +01:00
|
|
|
def set_last_modified(self, date_string):
|
|
|
|
if date_string:
|
|
|
|
try:
|
|
|
|
self._last_modified = dateutil.parser.isoparse(date_string)
|
|
|
|
except ValueError as e:
|
|
|
|
self._logger.debug("Invalid date format: %s", date_string)
|
|
|
|
|
|
|
|
|
|
|
|
def get_last_modified(self):
|
|
|
|
return self._last_modified
|
|
|
|
|
|
|
|
|
2013-02-25 17:32:33 +01:00
|
|
|
|
|
|
|
|
2016-04-16 19:14:34 +02:00
|
|
|
class MCGPlaylistTrack(MCGTrack):
|
2017-06-04 14:11:09 +02:00
|
|
|
def __init__(self, track, id, pos):
|
|
|
|
MCGTrack.__init__(
|
|
|
|
self,
|
|
|
|
track.get_artists(),
|
|
|
|
track.get_title(),
|
|
|
|
track.get_file()
|
|
|
|
)
|
2017-06-04 15:39:07 +02:00
|
|
|
self.set_albumartists(track.get_albumartists())
|
2017-06-04 14:11:09 +02:00
|
|
|
self.set_track(track.get_track())
|
|
|
|
self.set_length(track.get_length())
|
|
|
|
self.set_date(track.get_date())
|
2016-08-07 21:08:41 +02:00
|
|
|
self._id = int(id)
|
2016-04-16 19:14:34 +02:00
|
|
|
self._pos = int(pos)
|
|
|
|
|
|
|
|
|
2016-08-07 21:08:41 +02:00
|
|
|
def get_id(self):
|
|
|
|
return self._id
|
|
|
|
|
|
|
|
|
2016-04-16 19:14:34 +02:00
|
|
|
def get_pos(self):
|
|
|
|
return self._pos
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-02-25 17:32:33 +01:00
|
|
|
class MCGConfig(configparser.ConfigParser):
|
2014-12-01 20:55:28 +01:00
|
|
|
CONFIG_DIR = '~/.config/mcg/'
|
2013-02-25 17:32:33 +01:00
|
|
|
|
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
def __init__(self, filename):
|
|
|
|
configparser.ConfigParser.__init__(self)
|
|
|
|
self._filename = os.path.expanduser(os.path.join(MCGConfig.CONFIG_DIR, filename))
|
|
|
|
self._create_dir()
|
2013-02-25 17:32:33 +01:00
|
|
|
|
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
def load(self):
|
|
|
|
if os.path.isfile(self._filename):
|
|
|
|
self.read(self._filename)
|
2013-02-25 17:32:33 +01:00
|
|
|
|
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
def save(self):
|
|
|
|
with open(self._filename, 'w') as configfile:
|
|
|
|
self.write(configfile)
|
2013-02-25 17:32:33 +01:00
|
|
|
|
|
|
|
|
2014-12-01 20:55:28 +01:00
|
|
|
def _create_dir(self):
|
|
|
|
dirname = os.path.dirname(self._filename)
|
|
|
|
if not os.path.exists(dirname):
|
|
|
|
os.makedirs(dirname)
|
2013-02-25 17:32:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MCGCache():
|
2014-12-01 20:55:28 +01:00
|
|
|
DIRNAME = '~/.cache/mcg/'
|
|
|
|
SIZE_FILENAME = 'size'
|
|
|
|
_lock = threading.Lock()
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, host, size):
|
Don't assume that `size` file is valid
I don't know how my cache got into this state, but I had an empty size
file:
$ cat ~/.cache/mcg/127.0.0.1/size
$ stat ~/.cache/mcg/127.0.0.1/size
File: /home/jeremy/.cache/mcg/127.0.0.1/size
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 254,0 Inode: 18493061 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ jeremy) Gid: ( 100/ users)
Access: 2022-09-14 00:18:32.942885525 -0700
Modify: 2022-09-07 12:32:44.151734944 -0700
Change: 2022-09-07 12:32:44.151734944 -0700
Birth: 2022-08-25 10:01:01.729717504 -0700
This was causing mcg's Library view to crash like this:
(.mcg-wrapped:879276): Gtk-CRITICAL **: 00:19:15.727: gtk_window_add_accel_group: assertion 'GTK_IS_WINDOW (window)' failed
Exception in thread Thread-1 (_set_playlist):
Traceback (most recent call last):
File "/nix/store/c1vb2z3c64i0sd92iz7fv0lb720qcvhb-python3-3.10.6/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
self.run()
File "/nix/store/c1vb2z3c64i0sd92iz7fv0lb720qcvhb-python3-3.10.6/lib/python3.10/threading.py", line 953, in run
self._target(*self._args, **self._kwargs)
File "/nix/store/l935dwmk93sq2chr4xxiipv9amyfcg43-CoverGrid-3.1/share/mcg/mcg/playlistpanel.py", line 256, in _set_playlist
cache = client.MCGCache(host, size)
File "/nix/store/l935dwmk93sq2chr4xxiipv9amyfcg43-CoverGrid-3.1/share/mcg/mcg/client.py", line 1279, in __init__
self._read_size()
File "/nix/store/l935dwmk93sq2chr4xxiipv9amyfcg43-CoverGrid-3.1/share/mcg/mcg/client.py", line 1293, in _read_size
size = int(f.readline())
ValueError: invalid literal for int() with base 10: ''
Maybe mcg crashed while writing the `size` file at some point? I see
that it writes directly to the size file, which seems potentially risky:
it would probably be safer to write to a temp file and then (atomically)
move it. Still, it seems like a good practice to be resilient here.
After this change, here's what I see get printed by mcg:
(.mcg-wrapped:889856): Gtk-CRITICAL **: 00:37:00.045: gtk_window_add_accel_group: assertion 'GTK_IS_WINDOW (window)' failed
2022-09-14 00:37:00,076 WARNING: invalid cache file: /home/jeremy/.cache/mcg/127.0.0.1/size, deleting file
Traceback (most recent call last):
File "/nix/store/vzgcfs00nq543hjk8hrk81k1rs8aqpqw-CoverGrid-3.1/share/mcg/mcg/client.py", line 1295, in _read_size
size = int(f.readline())
ValueError: invalid literal for int() with base 10: ''
And then the problem goes away =)
2022-09-14 09:26:00 +02:00
|
|
|
self._logger = logging.getLogger(__name__)
|
2014-12-01 20:55:28 +01:00
|
|
|
self._host = host
|
|
|
|
self._size = size
|
|
|
|
self._dirname = os.path.expanduser(os.path.join(MCGCache.DIRNAME, host))
|
|
|
|
if not os.path.exists(self._dirname):
|
|
|
|
os.makedirs(self._dirname)
|
|
|
|
self._read_size()
|
|
|
|
|
|
|
|
|
|
|
|
def create_filename(self, album):
|
2018-11-04 17:44:52 +01:00
|
|
|
return os.path.join(self._dirname, '-'.join([album.get_id()]))
|
2014-12-01 20:55:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
def _read_size(self):
|
|
|
|
size = 100
|
|
|
|
MCGCache._lock.acquire()
|
|
|
|
# Read old size
|
|
|
|
filename = os.path.join(self._dirname, MCGCache.SIZE_FILENAME)
|
|
|
|
if os.path.exists(filename):
|
|
|
|
with open(filename, 'r') as f:
|
Don't assume that `size` file is valid
I don't know how my cache got into this state, but I had an empty size
file:
$ cat ~/.cache/mcg/127.0.0.1/size
$ stat ~/.cache/mcg/127.0.0.1/size
File: /home/jeremy/.cache/mcg/127.0.0.1/size
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 254,0 Inode: 18493061 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ jeremy) Gid: ( 100/ users)
Access: 2022-09-14 00:18:32.942885525 -0700
Modify: 2022-09-07 12:32:44.151734944 -0700
Change: 2022-09-07 12:32:44.151734944 -0700
Birth: 2022-08-25 10:01:01.729717504 -0700
This was causing mcg's Library view to crash like this:
(.mcg-wrapped:879276): Gtk-CRITICAL **: 00:19:15.727: gtk_window_add_accel_group: assertion 'GTK_IS_WINDOW (window)' failed
Exception in thread Thread-1 (_set_playlist):
Traceback (most recent call last):
File "/nix/store/c1vb2z3c64i0sd92iz7fv0lb720qcvhb-python3-3.10.6/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
self.run()
File "/nix/store/c1vb2z3c64i0sd92iz7fv0lb720qcvhb-python3-3.10.6/lib/python3.10/threading.py", line 953, in run
self._target(*self._args, **self._kwargs)
File "/nix/store/l935dwmk93sq2chr4xxiipv9amyfcg43-CoverGrid-3.1/share/mcg/mcg/playlistpanel.py", line 256, in _set_playlist
cache = client.MCGCache(host, size)
File "/nix/store/l935dwmk93sq2chr4xxiipv9amyfcg43-CoverGrid-3.1/share/mcg/mcg/client.py", line 1279, in __init__
self._read_size()
File "/nix/store/l935dwmk93sq2chr4xxiipv9amyfcg43-CoverGrid-3.1/share/mcg/mcg/client.py", line 1293, in _read_size
size = int(f.readline())
ValueError: invalid literal for int() with base 10: ''
Maybe mcg crashed while writing the `size` file at some point? I see
that it writes directly to the size file, which seems potentially risky:
it would probably be safer to write to a temp file and then (atomically)
move it. Still, it seems like a good practice to be resilient here.
After this change, here's what I see get printed by mcg:
(.mcg-wrapped:889856): Gtk-CRITICAL **: 00:37:00.045: gtk_window_add_accel_group: assertion 'GTK_IS_WINDOW (window)' failed
2022-09-14 00:37:00,076 WARNING: invalid cache file: /home/jeremy/.cache/mcg/127.0.0.1/size, deleting file
Traceback (most recent call last):
File "/nix/store/vzgcfs00nq543hjk8hrk81k1rs8aqpqw-CoverGrid-3.1/share/mcg/mcg/client.py", line 1295, in _read_size
size = int(f.readline())
ValueError: invalid literal for int() with base 10: ''
And then the problem goes away =)
2022-09-14 09:26:00 +02:00
|
|
|
try:
|
|
|
|
size = int(f.readline())
|
|
|
|
except:
|
|
|
|
self._logger.warning("invalid cache file: %s, deleting file", filename, exc_info=True)
|
|
|
|
size = None
|
2014-12-01 20:55:28 +01:00
|
|
|
# Clear cache if size has changed
|
|
|
|
if size != self._size:
|
|
|
|
self._clear()
|
|
|
|
# Write new size
|
|
|
|
with open(filename, 'w') as f:
|
|
|
|
f.write(str(self._size))
|
|
|
|
MCGCache._lock.release()
|
|
|
|
|
|
|
|
|
|
|
|
def _clear(self):
|
|
|
|
for filename in os.listdir(self._dirname):
|
|
|
|
path = os.path.join(self._dirname, filename)
|
|
|
|
if os.path.isfile(path):
|
|
|
|
try:
|
|
|
|
os.unlink(path)
|
|
|
|
except Exception as e:
|
|
|
|
print("clear:", e)
|