-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ [open-zaak/open-zaak#1649] Command to document envvars
- Loading branch information
Showing
8 changed files
with
374 additions
and
46 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
27 changes: 27 additions & 0 deletions
27
open_api_framework/management/commands/generate_envvar_docs.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import warnings | ||
from collections import defaultdict | ||
|
||
from django.core.management.base import BaseCommand | ||
from django.template import loader | ||
|
||
from open_api_framework.conf.utils import EnvironmentVariable | ||
|
||
|
||
def convert_group_to_rst(variables: set[EnvironmentVariable]) -> str: | ||
template = loader.get_template("open_api_framework/env_config.rst") | ||
grouped_vars = defaultdict(list) | ||
for var in variables: | ||
if not var.help_text: | ||
warnings.warn(f"missing help_text for environment variable {var}") | ||
grouped_vars[var.group].append(var) | ||
return template.render({"vars": grouped_vars.items()}) | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Generate documentation for all used envvars" | ||
|
||
def handle(self, *args, **options): | ||
from open_api_framework.conf.utils import ENVVAR_REGISTRY | ||
|
||
with open("docs/env_config.rst", "w") as f: | ||
f.write(convert_group_to_rst(ENVVAR_REGISTRY)) |
18 changes: 18 additions & 0 deletions
18
open_api_framework/templates/open_api_framework/env_config.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{% load doc_tags %}.. _installation_env_config: | ||
|
||
=================================== | ||
Environment configuration reference | ||
=================================== | ||
|
||
{% block intro %}{% endblock %} | ||
|
||
Available environment variables | ||
=============================== | ||
|
||
{% for group_name, vars in vars %} | ||
{{group_name}} | ||
{{group_name|repeat_char:"="}} | ||
|
||
{% for var in vars %}* ``{{var.name}}``: {% if var.help_text %}{{var.help_text|safe|ensure_endswith:"."}}{% endif %}{% if not var.default|is_undefined %} Defaults to: ``{{var.default|to_str}}``{% endif %} | ||
{% endfor %} | ||
{% endfor %} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from django import template | ||
|
||
from decouple import Undefined | ||
|
||
register = template.Library() | ||
|
||
|
||
@register.filter(name="repeat_char") | ||
def repeat_char(value, char="-"): | ||
try: | ||
length = len(value) | ||
return char * length | ||
except TypeError: | ||
return "" | ||
|
||
|
||
@register.filter(name="is_undefined") | ||
def is_undefined(value): | ||
return isinstance(value, Undefined) | ||
|
||
|
||
@register.filter(name="to_str") | ||
def to_str(value): | ||
if value == "": | ||
return "(empty string)" | ||
return str(value) | ||
|
||
|
||
@register.filter(name="ensure_endswith") | ||
def ensure_endswith(value, char): | ||
if not isinstance(value, str): | ||
value = str(value) | ||
if not value.endswith(char): | ||
value += char | ||
return value |