Skip to content

Commit

Permalink
✨[#64] add subgroup to generate_envvar_docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Coperh committed Sep 17, 2024
1 parent 9ef90bb commit 5fdf729
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
4 changes: 4 additions & 0 deletions open_api_framework/conf/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class EnvironmentVariable:
default: Any
help_text: str
group: Optional[str] = None
sub_group: Optional[str] = None
auto_display_default: bool = True

def __post_init__(self):
Expand All @@ -34,6 +35,7 @@ def config(
default: Any = undefined,
help_text="",
group=None,
sub_group=None,
add_to_docs=True,
auto_display_default=True,
*args,
Expand All @@ -56,6 +58,7 @@ def config(
:param help_text: The help text to be displayed for this variable in the documentation. Default `""`
:param group: The name of the section under which this variable will be grouped. Default ``None``
:param sub_group: The name of the subsection under which this variable will be grouped. Default ``None``
:param add_to_docs: Whether or not this variable will be displayed in the documentation. Default ``True``
:param auto_display_default: Whether or not the passed ``default`` value is displayed in the docs, this can be
set to ``False`` in case a default needs more explanation that can be added to the ``help_text``
Expand All @@ -67,6 +70,7 @@ def config(
default=default,
help_text=help_text,
group=group,
sub_group=sub_group,
auto_display_default=auto_display_default,
)
if variable not in ENVVAR_REGISTRY:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

def convert_variables_to_rst(variables: list[EnvironmentVariable]) -> str:
template = loader.get_template("open_api_framework/env_config.rst")
grouped_vars = defaultdict(list)
grouped_vars = defaultdict(lambda: 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)
grouped_vars[var.group][var.sub_group].append(var)
return template.render({"vars": grouped_vars.items()})


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

Expand Down
12 changes: 10 additions & 2 deletions open_api_framework/templates/open_api_framework/env_config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,19 @@ Environment configuration reference
Available environment variables
===============================

{% for group_name, vars in vars %}
{% for group_name, group_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 var.auto_display_default and not var.default|is_undefined %} Defaults to: ``{{var.default|to_str|safe}}``.{% endif %}
{% for subgroup_name, subgroup_vars in group_vars %}

{% if subgroup_name is not null %}
{{subgroup_name}}
{{subgroup_name|repeat_char:"^"}}
{% end if %}

{% for var in group_vars %}* ``{{var.name}}``: {% if var.help_text %}{{var.help_text|safe|ensure_endswith:"."}}{% endif %}{% if var.auto_display_default and not var.default|is_undefined %} Defaults to: ``{{var.default|to_str|safe}}``.{% endif %}
{% endfor %}
{% endfor %}
{% endfor %}

Expand Down

0 comments on commit 5fdf729

Please sign in to comment.