Skip to content

Commit

Permalink
Merge pull request #137 from tarsil/feat/new-settings
Browse files Browse the repository at this point in the history
New settings core
  • Loading branch information
tarsil authored Mar 8, 2024
2 parents aaec008 + e9a7883 commit aec6ab0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
2 changes: 1 addition & 1 deletion saffier/cli/templates/default/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def is_async_connection(url: DatabaseURL) -> bool:
(url.driver in settings.postgres_drivers)
or (url.driver in settings.mysql_drivers)
or (url.driver in settings.sqlite_drivers)
or url.driver in settings.mssql_drivers
or url.driver in settings.mssql_drivers # type: ignore
):
return True
return False
Expand Down
53 changes: 47 additions & 6 deletions saffier/conf/__init__.py
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
2 changes: 1 addition & 1 deletion saffier/core/connection/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def _get_database_url(self) -> str:
url = url.replace(driver="aiosqlite")
elif url.dialect in settings.mssql_dialects:
raise ImproperlyConfigured("Saffier does not support MSSQL at the moment.")
elif url.driver in settings.mssql_drivers:
elif url.driver in settings.mssql_drivers: # type: ignore
raise ImproperlyConfigured("Saffier does not support MSSQL at the moment.")
return str(url)

Expand Down

0 comments on commit aec6ab0

Please sign in to comment.