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 6043e1f commit dcaf429
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 17 deletions.
48 changes: 41 additions & 7 deletions open_api_framework/management/commands/generate_envvar_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

from django.core.management.base import BaseCommand
from django.template import loader
from django.conf import settings
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 +25,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):
from open_api_framework.conf.utils import ENVVAR_REGISTRY
self.generate_regular_config_docs(*args, **options)
self.generate_setup_config_docs(*args, **options)

file_path = options["file"]
def generate_regular_config_docs(self, *args, **options):
from open_api_framework.conf.utils import ENVVAR_REGISTRY
file_path = options["envvar_file"]
exclude_groups = options["exclude_group"] or []

def _sort(envvar):
Expand All @@ -51,8 +65,6 @@ def _sort(envvar):
return 0
case "Optional":
return 2
case "Setup Configuration":
return 3
case _:
return 1

Expand All @@ -62,3 +74,25 @@ 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)
20 changes: 10 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,7 @@ 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 dcaf429

Please sign in to comment.