django-constance/constance/base.py

32 lines
898 B
Python
Raw Normal View History

from . import settings
from . import utils
2010-08-23 11:06:52 +00:00
2013-04-12 15:39:10 +00:00
class Config:
2024-07-05 14:38:26 +00:00
"""The global config wrapper that handles the backend."""
2010-08-23 11:06:52 +00:00
def __init__(self):
2025-10-07 09:25:07 +00:00
super().__setattr__("_backend", utils.import_module_attr(settings.BACKEND)())
2010-08-23 11:06:52 +00:00
def __getattr__(self, key):
2010-08-25 12:55:01 +00:00
try:
2024-07-05 14:38:26 +00:00
if len(settings.CONFIG[key]) not in (2, 3):
raise AttributeError(key)
default = settings.CONFIG[key][0]
2024-07-05 14:38:26 +00:00
except KeyError as e:
raise AttributeError(key) from e
result = self._backend.get(key)
2010-08-23 11:06:52 +00:00
if result is None:
result = default
setattr(self, key, default)
2010-08-23 13:48:15 +00:00
return result
return result
2010-08-23 11:06:52 +00:00
def __setattr__(self, key, value):
if key not in settings.CONFIG:
2010-08-25 12:55:01 +00:00
raise AttributeError(key)
self._backend.set(key, value)
2010-08-23 11:06:52 +00:00
def __dir__(self):
2011-08-29 13:31:44 +00:00
return settings.CONFIG.keys()