Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion open_api_framework/conf/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
from corsheaders.defaults import default_headers as default_cors_headers
from csp.constants import NONCE, NONE, SELF
from log_outgoing_requests.formatters import HttpFormatter
from maykin_common.config_helpers import config
from notifications_api_common.settings import * # noqa

from .utils import (
config,
get_django_project_dir,
get_project_dirname,
get_sentry_integrations,
Expand Down
82 changes: 1 addition & 81 deletions open_api_framework/conf/utils.py
Original file line number Diff line number Diff line change
@@ -1,93 +1,13 @@
import logging # noqa: TID251
import sys
from dataclasses import dataclass
from pathlib import Path
from typing import Any, Optional
from urllib.parse import urlparse

from decouple import Csv, Undefined, config as _config, undefined
from maykin_common.config_helpers import config
from sentry_sdk.integrations import DidNotEnable, django, redis
from sentry_sdk.integrations.logging import LoggingIntegration


@dataclass
class EnvironmentVariable:
name: str
default: Any
help_text: str
group: Optional[str] = None
auto_display_default: bool = True

def __post_init__(self):
if not self.group:
self.group = (
"Required" if isinstance(self.default, Undefined) else "Optional"
)

def __eq__(self, other):
return isinstance(other, EnvironmentVariable) and self.name == other.name


ENVVAR_REGISTRY = []


def config(
option: str,
default: Any = undefined,
help_text="",
group=None,
add_to_docs=True,
auto_display_default=True,
*args,
**kwargs,
):
"""
An override of ``decouple.config``, with custom options to construct documentation
for environment variables.

Pull a config parameter from the environment.

Read the config variable ``option``. If it's optional, use the ``default`` value.
Input is automatically cast to the correct type, where the type is derived from the
default value if possible.

Pass ``split=True`` to split the comma-separated input into a list.

Additionally, the variable is added to a registry that is used to construct documentation
via the ``generate_envvar_docs`` management command. The following arguments are added for this:

: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 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``
(e.g. if it is computed or based on another variable). Default ``True``
"""
if add_to_docs:
variable = EnvironmentVariable(
name=option,
default=default,
help_text=help_text,
group=group,
auto_display_default=auto_display_default,
)
if variable not in ENVVAR_REGISTRY:
ENVVAR_REGISTRY.append(variable)
else:
# If the same variable is defined again (i.e. because a project defines a custom default), override it
ENVVAR_REGISTRY[ENVVAR_REGISTRY.index(variable)] = variable

if "split" in kwargs:
kwargs.pop("split")
kwargs["cast"] = Csv()
if isinstance(default, list):
default = ",".join(default)

if default is not undefined and default is not None:
kwargs.setdefault("cast", type(default))
return _config(option, default=default, *args, **kwargs)


def get_sentry_integrations() -> list:
"""
Determine which Sentry SDK integrations to enable.
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ dependencies = [
"mozilla-django-oidc-db>=0.19.0",
"psycopg[binary]>=3.2.9",
"python-dotenv>=1.0.0",
"python-decouple>=3.8",
"requests>=2.32.3",
"sentry-sdk>=2.11.0",
"elastic-apm>=6.22.0",
"maykin-2fa>=1.0.1",
"django-setup-configuration>=0.1.0",
"django-sessionprofile>=3.0.0",
"django-upgrade-check>=1.1.0",
"maykin-common>=0.9.0",
]

[project.urls]
Expand Down
Loading