Ticket #12737: #thread_local_settings.diff
File #thread_local_settings.diff, 2.8 KB (added by , 13 years ago) |
---|
-
django/conf/__init__.py
9 9 import re 10 10 import time # Needed for Windows 11 11 import warnings 12 import copy 13 try: 14 import thread 15 except ImportError: 16 import dummy_thread as thread 12 17 13 18 from django.conf import global_settings 14 19 from django.utils.functional import LazyObject … … 37 42 # problems with Python's interactive help. 38 43 raise ImportError("Settings cannot be imported, because environment variable %s is undefined." % ENVIRONMENT_VARIABLE) 39 44 40 self._wrapped = Settings(settings_module)45 self._wrapped = UserSettingsHolder(Settings(settings_module)) 41 46 42 47 def configure(self, default_settings=global_settings, **options): 43 48 """ … … 110 115 111 116 class UserSettingsHolder(object): 112 117 """ 113 Holder for user configured settings.118 Settings holder that allows thread-local overrides of defaults. 114 119 """ 115 # SETTINGS_MODULE doesn't make much sense in the manually configured116 # (standalone) case.117 SETTINGS_MODULE = None118 120 119 121 def __init__(self, default_settings): 120 122 """ 121 123 Requests for configuration variables not in this class are satisfied 122 124 from the module specified in default_settings (if possible). 123 125 """ 124 self.default_settings = default_settings 126 self.__dict__['default_settings'] = default_settings 127 self.__dict__['local_settings'] = {} 125 128 126 def __getattr__(self, name): 127 return getattr(self.default_settings, name) 129 def __getattr__(self, attr): 130 thread_ident = thread.get_ident() 131 local_settings = self.__dict__['local_settings'] 132 default_settings = self.__dict__['default_settings'] 133 try: 134 return local_settings[thread_ident][attr] 135 except KeyError: 136 return getattr(default_settings, attr) 128 137 138 def __setattr__(self, attr, val): 139 thread_ident = thread.get_ident() 140 local_settings = self.__dict__['local_settings'] 141 if thread_ident in local_settings: 142 local_settings[thread_ident][attr] = val 143 else: 144 local_settings[thread_ident] = {attr: val} 145 146 def __delattr__(self, attr): 147 thread_ident = thread.get_ident() 148 local_settings = self.__dict__['local_settings'] 149 if thread_ident in local_settings: 150 local_settings[thread_ident].pop(attr, None) 151 152 def clear(self): 153 thread_ident = thread.get_ident() 154 local_settings = self.__dict__['local_settings'] 155 if thread_ident in local_settings: 156 local_settings[thread_ident].clear() 157 129 158 def __dir__(self): 130 159 return self.__dict__.keys() + dir(self.default_settings) 131 160 … … 133 156 __members__ = property(lambda self: self.__dir__()) 134 157 135 158 settings = LazySettings() 136