Skip to content

Commit

Permalink
✨[#64] add setup config documentation command
Browse files Browse the repository at this point in the history
  • Loading branch information
Coperh committed Oct 1, 2024
1 parent 0c76f52 commit 3a07693
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 17 deletions.
47 changes: 41 additions & 6 deletions open_api_framework/management/commands/generate_envvar_docs.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -21,28 +27,38 @@ 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.",
action="append",
)

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):
Expand All @@ -51,8 +67,6 @@ def _sort(envvar):
return 0
case "Optional":
return 2
case "Setup Configuration":
return 3
case _:
return 1

Expand All @@ -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)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
22 changes: 12 additions & 10 deletions tests/test_generate_envvar_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------------------
Expand All @@ -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``.
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 3a07693

Please sign in to comment.