import sys import django conf class FakeSettings object Fake replaceme

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import sys
import django.conf
class FakeSettings(object):
"""
Fake replacement for django.conf.settings
"""
def __init__(self, **kwargs):
self._local_settings = kwargs
# checks that we haven't replaced orig settings by fake :)
if not isinstance(django.conf.settings, self.__class__):
self._django_orig_settings = django.conf.settings
else:
self.django_orig_settings = django.conf.settings._django_orig_settings
def install(self):
# replace django.conf.settings by self
sys.modules['django.conf.settings'] = self
django.conf.settings = self
# check that we replace django's settings
from django.conf import settings as S
assert S is self
def deinstall(self):
sys.modules['django.conf.settings'] = self._django_orig_settings
django.conf.settings = self._django_orig_settings
# check that we restore django's settings
from django.conf import settings as S
assert S is not self
assert not isinstance(S, self.__class__)
def __getattr__(self, attr):
if attr not in self._local_settings:
return getattr(self._django_orig_settings, attr)
else:
return self._local_settings[attr]
def __setattr__(self, attr, value):
if attr.startswith('_'):
self.__dict__[attr] = value
else:
self._local_settings[attr] = value