Ticket #12737: #thread_local_settings.diff

File #thread_local_settings.diff, 2.8 KB (added by German M. Bravo, 13 years ago)

Thread local settings

  • django/conf/__init__.py

     
    99import re
    1010import time     # Needed for Windows
    1111import warnings
     12import copy
     13try:
     14    import thread
     15except ImportError:
     16    import dummy_thread as thread
    1217
    1318from django.conf import global_settings
    1419from django.utils.functional import LazyObject
     
    3742            # problems with Python's interactive help.
    3843            raise ImportError("Settings cannot be imported, because environment variable %s is undefined." % ENVIRONMENT_VARIABLE)
    3944
    40         self._wrapped = Settings(settings_module)
     45        self._wrapped = UserSettingsHolder(Settings(settings_module))
    4146
    4247    def configure(self, default_settings=global_settings, **options):
    4348        """
     
    110115
    111116class UserSettingsHolder(object):
    112117    """
    113     Holder for user configured settings.
     118    Settings holder that allows thread-local overrides of defaults.
    114119    """
    115     # SETTINGS_MODULE doesn't make much sense in the manually configured
    116     # (standalone) case.
    117     SETTINGS_MODULE = None
    118120
    119121    def __init__(self, default_settings):
    120122        """
    121123        Requests for configuration variables not in this class are satisfied
    122124        from the module specified in default_settings (if possible).
    123125        """
    124         self.default_settings = default_settings
     126        self.__dict__['default_settings'] = default_settings
     127        self.__dict__['local_settings'] = {}
    125128
    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)
    128137
     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
    129158    def __dir__(self):
    130159        return self.__dict__.keys() + dir(self.default_settings)
    131160
     
    133156    __members__ = property(lambda self: self.__dir__())
    134157
    135158settings = LazySettings()
    136 
Back to Top