diff --git a/api/config/plugins.py b/api/config/plugins.py index f390c4366..e6ab67915 100644 --- a/api/config/plugins.py +++ b/api/config/plugins.py @@ -34,6 +34,7 @@ def get_plugin_config( source=False, registry=_plugins, conf={}, + settings={}, description=None, version=None, label=None, @@ -42,8 +43,12 @@ def get_plugin_config( "name": name, "label": label or name, "logger": logger, + # conf is for dynamic settings "conf": conf, + # settings is for settings hardcoded in .env + "settings": settings, "user": True if source else user, + # source plugins are plugins that provide audio content "source": source, "description": description, "version": version, @@ -52,6 +57,24 @@ def get_plugin_config( return conf +def load_settings(name, settings): + from django.conf import settings as django_settings + + mapping = { + "boolean": django_settings.ENV.bool, + "text": django_settings.ENV, + } + values = {} + prefix = "FUNKWHALE_PLUGIN_{}".format(name.upper()) + for s in settings: + key = "_".join([prefix, s["name"].upper()]) + value = mapping[s["type"]](key, default=s.get("default", None)) + values[s["name"]] = value + + logger.debug("Plugin %s running with settings %s", name, values) + return values + + def get_session(): from funkwhale_api.common import session diff --git a/api/config/settings/common.py b/api/config/settings/common.py index b52d46312..06194348d 100644 --- a/api/config/settings/common.py +++ b/api/config/settings/common.py @@ -18,7 +18,7 @@ ROOT_DIR = environ.Path(__file__) - 3 # (/a/b/myfile.py - 3 = /) APPS_DIR = ROOT_DIR.path("funkwhale_api") env = environ.Env() - +ENV = env LOGLEVEL = env("LOGLEVEL", default="info").upper() """ Default logging level for the Funkwhale processes""" # pylint: disable=W0105 diff --git a/api/funkwhale_api/common/apps.py b/api/funkwhale_api/common/apps.py index afd834a5a..05fc275fe 100644 --- a/api/funkwhale_api/common/apps.py +++ b/api/funkwhale_api/common/apps.py @@ -17,3 +17,5 @@ class CommonConfig(AppConfig): mutations.registry.autodiscover(app_names) utils.monkey_patch_request_build_absolute_uri() plugins.startup.autodiscover([p + ".funkwhale_ready" for p in settings.PLUGINS]) + for p in plugins._plugins.values(): + p["settings"] = plugins.load_settings(p["name"], p["settings"]) diff --git a/api/funkwhale_api/contrib/scrobbler/funkwhale_startup.py b/api/funkwhale_api/contrib/scrobbler/funkwhale_startup.py index 517a1eadb..7dcdd4b4c 100644 --- a/api/funkwhale_api/contrib/scrobbler/funkwhale_startup.py +++ b/api/funkwhale_api/contrib/scrobbler/funkwhale_startup.py @@ -1,3 +1,13 @@ +""" +A plugin that enables scrobbling to ListenBrainz and Last.fm. + +If you're scrobbling to last.fm, you will need to create an `API account `_ +and add two variables two your .env file: + +- ``FUNKWHALE_PLUGIN_SCROBBLER_LASTFM_API_KEY=apikey`` +- ``FUNKWHALE_PLUGIN_SCROBBLER_LASTFM_API_SECRET=apisecret`` + +""" from config import plugins PLUGIN = plugins.get_plugin_config( @@ -24,4 +34,8 @@ PLUGIN = plugins.get_plugin_config( {"name": "username", "type": "text", "label": "Your scrobbler username"}, {"name": "password", "type": "password", "label": "Your scrobbler password"}, ], + # settings=[ + # {"name": "lastfm_api_key", "type": "text"}, + # {"name": "lastfm_api_secret", "type": "text"}, + # ] )