This repository has been archived on 2023-01-02. You can view files and clone it, but cannot push or open issues or pull requests.
django-pyvows/django_pyvows/settings_manager.py

43 lines
1.4 KiB
Python
Raw Normal View History

2011-08-22 20:06:54 +00:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
# django-pyvows extensions
# https://github.com/rafaelcaricio/django-pyvows
# Licensed under the MIT license:
# http://www.opensource.org/licenses/mit-license
# Copyright (c) 2011 Rafael Caricio rafael@caricio.com
2011-08-22 21:51:39 +00:00
from threading import current_thread
2011-08-22 20:06:54 +00:00
2011-08-22 21:51:39 +00:00
class SettingsTracker(object):
2011-08-22 20:06:54 +00:00
def install(self):
2011-08-22 21:51:39 +00:00
actual_import = __builtins__['__import__']
if actual_import != self._import:
self.real_import = actual_import
__builtins__['__import__'] = self._import
2011-08-22 20:06:54 +00:00
def _import(self, name, globals=None, locals=None, fromlist=[], level=-1):
result = apply(self.real_import, (name, globals, locals, fromlist, level))
fromlist = (fromlist or [])
if name == 'django.conf' and 'settings' in fromlist:
2011-08-22 21:51:39 +00:00
result.settings = VowsSettings(result.settings)
2011-12-21 19:36:13 +00:00
elif name == 'django' and 'conf' in fromlist:
result.conf.settings = VowsSettings(result.conf.settings)
2011-08-22 20:06:54 +00:00
return result
2011-08-22 21:51:39 +00:00
class VowsSettings(object):
2011-08-22 20:06:54 +00:00
2011-08-22 21:51:39 +00:00
def __init__(self, original_settings):
self.original_settings = original_settings
2011-08-22 20:06:54 +00:00
2011-08-22 21:51:39 +00:00
def __getattr__(self, attr_name):
thread = current_thread()
2011-12-21 19:36:13 +00:00
if hasattr(thread, 'settings'):
2012-03-09 21:55:05 +00:00
if attr_name in thread.settings:
return thread.settings[attr_name]
2011-08-22 21:51:39 +00:00
return getattr(self.original_settings, attr_name)
settings_tracker = SettingsTracker()