diff --git a/swh/docs/django_settings.py b/swh/docs/django_settings.py --- a/swh/docs/django_settings.py +++ b/swh/docs/django_settings.py @@ -1,16 +1,23 @@ -from swh.deposit.settings.development import * # noqa -import swh.web.settings.development as web +# Copyright (C) 2021 The Software Heritage developers +# See the AUTHORS file at the top-level directory of this distribution +# License: GNU Affero General Public License version 3, or any later version +# See top-level LICENSE file for more information -# merge some config variables -ns = globals() -for var in dir(web): - if var.isupper() and var in ns and isinstance(ns[var], list): - for elt in getattr(web, var): - if elt not in ns[var]: - ns[var].append(elt) +from importlib import reload +import os -# swh-web needs to find its static files when running autodoc -STATIC_DIR = web.STATIC_DIR -STATICFILES_DIRS = web.STATICFILES_DIRS -SECRET_KEY = "change me" +def force_django_settings(settings_module): + """ + Enable to modify django settings module dynamically while + building sphinx documentation and force settings full reloading. + """ + if os.environ.get("DJANGO_SETTINGS_MODULE") != settings_module: + os.environ["DJANGO_SETTINGS_MODULE"] = settings_module + + import django + from django import conf as django_conf + + # reset django settings to force their reloading + reload(django_conf) + django.setup() diff --git a/swh/docs/sphinx/conf.py b/swh/docs/sphinx/conf.py --- a/swh/docs/sphinx/conf.py +++ b/swh/docs/sphinx/conf.py @@ -5,9 +5,10 @@ import os from typing import Dict -import django from sphinx.ext import autodoc +from swh.docs.django_settings import force_django_settings + # General information about the project. project = "Software Heritage - Development Documentation" copyright = "2015-2021 The Software Heritage developers" @@ -201,12 +202,24 @@ anonlabels["routingtable"] = "http-routingtable", "" -def setup(app): - # hack to set the adequate django settings when building global swh doc - # to avoid autodoc build errors - os.environ.setdefault("DJANGO_SETTINGS_MODULE", "swh.docs.django_settings") - django.setup() +# sphinx event handler to set adequate django settings prior reading +# apidoc generated rst files when building doc to avoid autodoc errors +def set_django_settings(app, env, docname): + package_settings = { + "auth": "swh.auth.tests.django.app.apptest.settings", + "deposit": "swh.deposit.settings.development", + "web": "swh.web.settings.development", + } + for package, settings in package_settings.items(): + if any( + [pattern in docname for pattern in (f"swh.{package}", f"swh-{package}")] + ): + force_django_settings(settings) + +def setup(app): + # env-purge-doc event is fired before source-read + app.connect("env-purge-doc", set_django_settings) # add autosimple directive (used in swh-web) app.add_autodocumenter(SimpleDocumenter) # set an environment variable indicating we are currently building