django-constance/constance/base.py

31 lines
824 B
Python
Raw Normal View History

from . import settings, utils
2010-08-23 11:06:52 +00:00
2013-04-12 15:39:10 +00:00
2010-08-23 11:06:52 +00:00
class Config(object):
"""
The global config wrapper that handles the backend.
"""
2010-08-23 11:06:52 +00:00
def __init__(self):
super(Config, self).__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:
default, help_text = settings.CONFIG[key]
except KeyError:
2010-08-25 12:55:01 +00:00
raise AttributeError(key)
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()