From 3a07693df2410dc2ff8ad9553c6489dc7b75d1e8 Mon Sep 17 00:00:00 2001 From: Conor Holden Date: Tue, 1 Oct 2024 14:30:18 +0200 Subject: [PATCH] :sparkles:[#64] add setup config documentation command --- .../commands/generate_envvar_docs.py | 47 ++++++++++++++++--- pyproject.toml | 2 +- tests/test_generate_envvar_docs.py | 22 +++++---- 3 files changed, 54 insertions(+), 17 deletions(-) diff --git a/open_api_framework/management/commands/generate_envvar_docs.py b/open_api_framework/management/commands/generate_envvar_docs.py index 85c6b11..fafaa1a 100644 --- a/open_api_framework/management/commands/generate_envvar_docs.py +++ b/open_api_framework/management/commands/generate_envvar_docs.py @@ -1,8 +1,14 @@ import warnings from collections import defaultdict +from django.conf import settings from django.core.management.base import BaseCommand from django.template import loader +from django.utils.module_loading import import_string + +from django_setup_configuration.management.commands.generate_config_docs import ( + ConfigDocBase, +) from open_api_framework.conf.utils import EnvironmentVariable @@ -21,18 +27,24 @@ def convert_variables_to_rst(variables: list[EnvironmentVariable]) -> str: return template.render({"vars": vars}) -class Command(BaseCommand): +class Command(ConfigDocBase, BaseCommand): help = "Generate documentation for all used envvars" def add_arguments(self, parser): super().add_arguments(parser) parser.add_argument( - "--file", - help="Name and path of the file to which the documentation will be written.", + "--envvar-file", + help="Name and path of the file to which the envvar documentation will be written.", nargs="?", default="docs/env_config.rst", ) + parser.add_argument( + "--config-file", + help="Name and path of the file to which the setup configuration documentation will be written.", + nargs="?", + default="docs/setup_config.rst", + ) parser.add_argument( "--exclude-group", help="Names of groups that should not be excluded in the generated docs.", @@ -40,9 +52,13 @@ def add_arguments(self, parser): ) def handle(self, *args, **options): + self.generate_regular_config_docs(*args, **options) + self.generate_setup_config_docs(*args, **options) + + def generate_regular_config_docs(self, *args, **options): from open_api_framework.conf.utils import ENVVAR_REGISTRY - file_path = options["file"] + file_path = options["envvar_file"] exclude_groups = options["exclude_group"] or [] def _sort(envvar): @@ -51,8 +67,6 @@ def _sort(envvar): return 0 case "Optional": return 2 - case "Setup Configuration": - return 3 case _: return 1 @@ -62,3 +76,24 @@ def _sort(envvar): ) with open(file_path, "w") as f: f.write(convert_variables_to_rst(sorted_registry)) + + def generate_setup_config_docs(self, *args, **options) -> None: + full_rendered_content = "" + + file_path = options["config_file"] + if not hasattr(settings, "SETUP_CONFIGURATION_STEPS"): + return + + for config_string in settings.SETUP_CONFIGURATION_STEPS: + config_step = import_string(config_string) + + config_settings = getattr(config_step, "config_settings", None) + if not config_settings or not config_settings.independent: + continue + + rendered_content = self.render_doc(config_settings, config_step) + full_rendered_content += rendered_content + + if len(full_rendered_content) > 0: + with open(file_path, "w") as f: + f.write(full_rendered_content) diff --git a/pyproject.toml b/pyproject.toml index 3e23499..3457c9b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -55,7 +55,7 @@ dependencies = [ "celery>=5.4.0", "flower>=2.0.1", "maykin-2fa>=1.0.1", - "django-setup-configuration>=0.1.0", + "django-setup-configuration>=0.3.0", ] [project.urls] diff --git a/tests/test_generate_envvar_docs.py b/tests/test_generate_envvar_docs.py index fcc66bc..d6be4dd 100644 --- a/tests/test_generate_envvar_docs.py +++ b/tests/test_generate_envvar_docs.py @@ -22,15 +22,6 @@ ``so-secret-i-cant-believe-you-are-looking-at-this``. -Optional --------- - -* ``DEBUG``: Only set this to ``True`` on a local development environment. Various other \ -security settings are derived from this setting!. Defaults to: ``False``. -* ``IS_HTTPS``: Used to construct absolute URLs and controls a variety of security settings. \ -Defaults to the inverse of ``DEBUG``. - - Setup Configuration ------------------- @@ -40,6 +31,15 @@ * ``NOTIF_CONFIG_ENABLE``: Enable Notification Configuration. Defaults to: ``False``. +Optional +-------- + +* ``DEBUG``: Only set this to ``True`` on a local development environment. Various other \ +security settings are derived from this setting!. Defaults to: ``False``. +* ``IS_HTTPS``: Used to construct absolute URLs and controls a variety of security settings. \ +Defaults to the inverse of ``DEBUG``. + + @@ -81,7 +81,9 @@ def test_generate_envvar_docs(): "open_api_framework.management.commands.generate_envvar_docs.open", mock_file ): call_command( - "generate_envvar_docs", file="some/file/path.txt", exclude_group="Excluded" + "generate_envvar_docs", + envvar_file="some/file/path.txt", + exclude_group="Excluded", ) mock_file.assert_called_once_with("some/file/path.txt", "w")