Merge branch 'lastfm-api' into 'develop'

Plugins can now register .env settings

See merge request funkwhale/funkwhale!1215
This commit is contained in:
Agate 2020-08-25 10:01:27 +02:00
commit 00b6fb512f
4 changed files with 40 additions and 1 deletions

View File

@ -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

View File

@ -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

View File

@ -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"])

View File

@ -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 <https://www.last.fm/api/account/create>`_
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"},
# ]
)