Skip to content

Commit

Permalink
Merge pull request #5 from dymmond/feature/settings_module
Browse files Browse the repository at this point in the history
Add reload optional for settings
  • Loading branch information
tarsil authored Apr 4, 2024
2 parents e2106e3 + f699b66 commit 69265c7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
11 changes: 10 additions & 1 deletion docs/release-notes.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
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 69265c7

Please sign in to comment.