From b97bf3271d7967cfd901a9de7140d1aa7d2e71b6 Mon Sep 17 00:00:00 2001 From: boyska Date: Sat, 27 Mar 2021 13:20:02 +0100 Subject: [PATCH] MPD_ENFORCE_ALWAYS_PLAYING option refs #16 --- larigira/config.py | 1 + larigira/mpc.py | 41 ++++++++++++++++++++++++++++++++--------- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/larigira/config.py b/larigira/config.py index e9f9de5..18fcde9 100644 --- a/larigira/config.py +++ b/larigira/config.py @@ -27,6 +27,7 @@ def get_conf(prefix="LARIGIRA_"): conf["SECRET_KEY"] = "Please replace me!" conf["MPD_WAIT_START"] = True conf["MPD_WAIT_START_RETRYSECS"] = 5 + conf["MPD_ENFORCE_ALWAYS_PLAYING"] = False conf["CHECK_SECS"] = 20 # period for checking playlist length conf["EVENT_TICK_SECS"] = 30 # period for scheduling events conf["DEBUG"] = False diff --git a/larigira/mpc.py b/larigira/mpc.py index 1fb79bb..5ab5038 100644 --- a/larigira/mpc.py +++ b/larigira/mpc.py @@ -1,17 +1,19 @@ from __future__ import print_function + import logging import signal + +import mpd from pkg_resources import iter_entry_points import gevent from gevent.queue import Queue -import mpd +from .audiogen import audiogenerate +from .entrypoints_utils import get_avail_entrypoints from .event import Monitor from .eventutils import ParentedLet, Timer -from .audiogen import audiogenerate from .unused import UnusedCleaner -from .entrypoints_utils import get_avail_entrypoints def get_mpd_client(conf): @@ -51,7 +53,9 @@ class MPDWatcher(ParentedLet): FileNotFoundError, ) as exc: self.log.warning( - "Connection to MPD failed (%s: %s)", exc.__class__.__name__, exc + "Connection to MPD failed (%s: %s)", + exc.__class__.__name__, + exc, ) self.client = None first_after_connection = True @@ -83,9 +87,15 @@ class Player: mpd_client = mpd.MPDClient(use_unicode=True) try: mpd_client.connect(self.conf["MPD_HOST"], self.conf["MPD_PORT"]) - except (mpd.ConnectionError, ConnectionRefusedError, FileNotFoundError) as exc: + except ( + mpd.ConnectionError, + ConnectionRefusedError, + FileNotFoundError, + ) as exc: self.log.warning( - "Connection to MPD failed (%s: %s)", exc.__class__.__name__, exc + "Connection to MPD failed (%s: %s)", + exc.__class__.__name__, + exc, ) raise gevent.GreenletExit() return mpd_client @@ -146,7 +156,9 @@ class Player: try: ret = ef(songs=songs, context=ctx, conf=self.conf) except ImportError as exc: - self.log.warn("Filter %s skipped: %s" % (entrypoint.name, exc)) + self.log.warn( + "Filter %s skipped: %s" % (entrypoint.name, exc) + ) continue if ret is None: # bad behavior! continue @@ -159,7 +171,9 @@ class Player: return ret, reason else: if reason: - self.log.debug('filter %s says ok: %s', entrypoint.name, reason) + self.log.debug( + "filter %s says ok: %s", entrypoint.name, reason + ) return True, "Passed through %s" % ",".join(availfilters) def enqueue(self, songs): @@ -200,6 +214,11 @@ class Player: self.log.exception("Cannot insert song %s", uri) self.tmpcleaner.watch(uri.strip()) + def play(self): + """make sure that MPD is playing""" + mpd_client = self._get_mpd() + mpd_client.play() + class Controller(gevent.Greenlet): def __init__(self, conf): @@ -234,6 +253,8 @@ class Controller(gevent.Greenlet): kind == "mpc" and args[0] in ("player", "playlist", "connect") ): gevent.Greenlet.spawn(self.player.check_playlist) + if self.conf["MPD_ENFORCE_ALWAYS_PLAYING"]: + gevent.Greenlet.spawn(self.player.play) try: self.player.tmpcleaner.check_playlist() except: @@ -251,7 +272,9 @@ class Controller(gevent.Greenlet): self.log.exception( "Error while adding to queue; " "bad audiogen output?" ) - elif (kind == "signal" and args[0] == signal.SIGALRM) or kind == "refresh": + elif ( + kind == "signal" and args[0] == signal.SIGALRM + ) or kind == "refresh": # it's a tick! self.log.debug("Reload") self.monitor.q.put(dict(kind="forcetick"))