Skip to content

Commit

Permalink
Add reload optional for settings
Browse files Browse the repository at this point in the history
  • Loading branch information
tarsil committed Apr 4, 2024
1 parent e2106e3 commit 0d89e52
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion dymmond_settings/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "1.0.4"
__version__ = "1.0.5"

from .base import Settings
from .conf import settings
Expand Down
25 changes: 21 additions & 4 deletions dymmond_settings/conf.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.
Expand All @@ -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:
Expand Down

0 comments on commit 0d89e52

Please sign in to comment.