diff --git a/docs/release-notes.md b/docs/release-notes.md index fb3b493..c687fdf 100644 --- a/docs/release-notes.md +++ b/docs/release-notes.md @@ -1,8 +1,17 @@ # Release Notes +## 1.0.5 + +### Added + +- `reload_settings` allowing to reset the internals of the default global settings. +- `configure` allowing to set a new global lazy settings. + ## 1.0.4 -- Fix global settings reference. +### Fixed + +- Global settings reference. ## 1.0.3 diff --git a/dymmond_settings/__init__.py b/dymmond_settings/__init__.py index 98dce3a..6ed90a5 100644 --- a/dymmond_settings/__init__.py +++ b/dymmond_settings/__init__.py @@ -1,4 +1,4 @@ -__version__ = "1.0.4" +__version__ = "1.0.5" from .base import Settings from .conf import settings diff --git a/dymmond_settings/conf.py b/dymmond_settings/conf.py index 60d95dc..4e65a32 100644 --- a/dymmond_settings/conf.py +++ b/dymmond_settings/conf.py @@ -1,4 +1,5 @@ import os +from functools import lru_cache from typing import TYPE_CHECKING, Any, Optional, Type from dymmond_settings.functional import LazyObject, empty @@ -11,6 +12,17 @@ ENVIRONMENT_VARIABLE = os.environ.get(OVERRIDE_VARIABLE) or "SETTINGS_MODULE" +@lru_cache +def reload_settings() -> Type["Settings"]: + """ + Reloads the global settings. + """ + settings_module: str = os.environ.get(ENVIRONMENT_VARIABLE, "dymmond_settings.base.Settings") + settings: Type["Settings"] = import_string(settings_module) + + return settings + + class LazySettings(LazyObject): """ A lazy proxy for either global application settings or a custom settings object. @@ -24,17 +36,22 @@ def _setup(self, name: Optional[str] = None) -> None: is used the first time settings are needed, if the user hasn't configured settings manually. """ - settings_module: str = os.environ.get( - ENVIRONMENT_VARIABLE, "dymmond_settings.base.Settings" - ) - settings: Type["Settings"] = import_string(settings_module) + settings: Type["Settings"] = reload_settings() for setting, _ in settings().dict().items(): assert setting.islower(), "%s should be in lower case." % setting self._wrapped = settings() + def configure(self, override_settings: "LazySettings") -> None: + """ + Making sure the settings are overriden by the settings + provided by a given application and therefore use it as the application + global. + """ + self._wrapped = override_settings + def __repr__(self: "LazySettings") -> str: # Hardcode the class name as otherwise it yields 'Settings'. if self._wrapped is empty: