-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #137 from tarsil/feat/new-settings
New settings core
- Loading branch information
Showing
3 changed files
with
49 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,51 @@ | ||
import os | ||
from typing import TYPE_CHECKING, Any, Optional, Type | ||
|
||
os.environ.setdefault("OVERRIDE_SETTINGS_MODULE_VARIABLE", "SAFFIER_SETTINGS_MODULE") | ||
from saffier.conf.functional import LazyObject, empty | ||
from saffier.conf.module_import import import_string | ||
|
||
if not os.environ.get("SAFFIER_SETTINGS_MODULE"): | ||
os.environ.setdefault( | ||
"SAFFIER_SETTINGS_MODULE", "saffier.conf.global_settings.SaffierSettings" | ||
) | ||
if TYPE_CHECKING: | ||
from saffier.conf.global_settings import SaffierSettings | ||
|
||
from dymmond_settings import settings as settings # noqa | ||
ENVIRONMENT_VARIABLE = "SAFFIER_SETTINGS_MODULE" | ||
|
||
|
||
class SaffierLazySettings(LazyObject): | ||
""" | ||
A lazy proxy for either global Saffier settings or a custom settings object. | ||
The user can manually configure settings prior to using them. Otherwise, | ||
Saffier uses the settings module pointed to by LILYA_SETTINGS_MODULE. | ||
""" | ||
|
||
def _setup(self, name: Optional[str] = None) -> None: | ||
""" | ||
Load the settings module pointed to by the environment variable. This | ||
is used the first time settings are needed, if the user hasn't | ||
configured settings manually. | ||
""" | ||
settings_module: str = os.environ.get( | ||
ENVIRONMENT_VARIABLE, "saffier.conf.global_settings.SaffierSettings" | ||
) | ||
|
||
settings: Type["SaffierSettings"] = import_string(settings_module) | ||
|
||
for setting, _ in settings().dict().items(): | ||
assert setting.islower(), "%s should be in lowercase." % setting | ||
|
||
self._wrapped = settings() | ||
|
||
def __repr__(self: "SaffierLazySettings") -> str: | ||
# Hardcode the class name as otherwise it yields 'SaffierSettings'. | ||
if self._wrapped is empty: | ||
return "<SaffierLazySettings [Unevaluated]>" | ||
return '<SaffierLazySettings "{settings_module}">'.format( | ||
settings_module=self._wrapped.__class__.__name__ | ||
) | ||
|
||
@property | ||
def configured(self) -> Any: | ||
"""Return True if the settings have already been configured.""" | ||
return self._wrapped is not empty | ||
|
||
|
||
settings: Type["SaffierSettings"] = SaffierLazySettings() # type: ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters