diff --git a/open_api_framework/conf/base.py b/open_api_framework/conf/base.py index 5dcf37a..5c08003 100644 --- a/open_api_framework/conf/base.py +++ b/open_api_framework/conf/base.py @@ -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, diff --git a/open_api_framework/conf/utils.py b/open_api_framework/conf/utils.py index abea579..8ab83cf 100644 --- a/open_api_framework/conf/utils.py +++ b/open_api_framework/conf/utils.py @@ -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. diff --git a/pyproject.toml b/pyproject.toml index 5969d5a..ea83454 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -36,7 +36,6 @@ 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", @@ -44,6 +43,7 @@ dependencies = [ "django-setup-configuration>=0.1.0", "django-sessionprofile>=3.0.0", "django-upgrade-check>=1.1.0", + "maykin-common>=0.9.0", ] [project.urls]