import copy
import mock
from functools import wraps

from django.conf import global_settings, settings

SETTING_DELETED = mock.sentinel.SETTING_DELETED

class override_settings(object):
    def __init__(self, **kwargs):
        self.patcher = mock.patch('django.conf.settings._wrapped', **kwargs)

    def __call__(self, test_func):
        if isinstance(test_func, type):
            def _pre_setup(innerself):
                self.enable()
                test_func.setUp(innerself)
            def _post_teardown(innerself):
                test_func.tearDown(innerself)
                self.disable()

            # When decorating a class, we need to construct a new class
            # with the same name so that the test discovery tools can
            # get a useful name.
            inner = type(
                test_func.__name__,
                (test_func,),
                {
                    'setUp': _pre_setup,
                    'tearDown': _post_teardown,
                    '__module__': test_func.__module__,
                })
            return inner
        else:
            @wraps(test_func)
            def inner(*args, **kwargs):
                with self:
                    return test_func(*args, **kwargs)
            return inner

    def enable(self):
        self.mocked = self.patcher.start()
        return self.mocked

    def disable(self):
        self.patcher.stop()

    def __enter__(self):
        self.enable()

    def __exit__(self, exc_type, exc_value, traceback):
        self.disable()

# Backported from Django trunk (r16377)
# class override_settings(object):
#     """
#     Temporarily override Django settings.

#     Acts as either a decorator, or a context manager.  If it's a decorator it
#     takes a function and returns a wrapped function.  If it's a contextmanager
#     it's used with the ``with`` statement.  In either event entering/exiting
#     are called before and after, respectively, the function/block is executed.
#     """
#     def __init__(self, **kwargs):
#         self.options = kwargs
#         self.wrapped = settings._wrapped

#     def __enter__(self):
#         self.enable()

#     def __exit__(self, exc_type, exc_value, traceback):
#         self.disable()

#     def __call__(self, test_func):
#         from django.test import TransactionTestCase
#         if isinstance(test_func, type) and issubclass(test_func, TransactionTestCase):
#             # When decorating a class, we need to construct a new class
#             # with the same name so that the test discovery tools can
#             # get a useful name.
#             def _pre_setup(innerself):
#                 self.enable()
#                 test_func._pre_setup(innerself)
#             def _post_teardown(innerself):
#                 test_func._post_teardown(innerself)
#                 self.disable()
#             inner = type(
#                 test_func.__name__,
#                 (test_func,),
#                 {
#                     '_pre_setup': _pre_setup,
#                     '_post_teardown': _post_teardown,
#                     '__module__': test_func.__module__,
#                 })
#         else:
#             @wraps(test_func)
#             def inner(*args, **kwargs):
#                 with self:
#                     return test_func(*args, **kwargs)
#         return inner

#     def enable(self):
#         class OverrideSettingsHolder(UserSettingsHolder):
#             def __getattr__(self, name):
#                 if name == "default_settings":
#                     return self.__dict__["default_settings"]
#                 return getattr(self.default_settings, name)

#         override = OverrideSettingsHolder(copy.copy(settings._wrapped))
#         for key, new_value in self.options.iteritems():
#             if new_value is SETTING_DELETED:
#                 try:
#                     delattr(override.default_settings, key)
#                 except AttributeError:
#                     pass
#             else:
#                 setattr(override, key, new_value)
#         settings._wrapped = override

#     def disable(self):
#         settings._wrapped = self.wrapped

def with_apps(*apps):
    """
    Class decorator that makes sure the passed apps are present in
    INSTALLED_APPS.
    """
    apps_set = set(settings.INSTALLED_APPS)
    apps_set.update(apps)
    return override_settings(INSTALLED_APPS=list(apps_set))

def without_apps(*apps):
    """
    Class decorator that makes sure the passed apps are not present in
    INSTALLED_APPS.
    """
    apps_list = [a for a in settings.INSTALLED_APPS if a not in apps]
    return override_settings(INSTALLED_APPS=apps_list)

@@ -1541,2648 +1541,8 @@
 ()%0A%0A
-# Backported from Django trunk (r16377)%0A# class override_settings(object):%0A#     %22%22%22%0A#     Temporarily override Django settings.%0A%0A#     Acts as either a decorator, or a context manager.  If it's a decorator it%0A#     takes a function and returns a wrapped function.  If it's a contextmanager%0A#     it's used with the %60%60with%60%60 statement.  In either event entering/exiting%0A#     are called before and after, respectively, the function/block is executed.%0A#     %22%22%22%0A#     def __init__(self, **kwargs):%0A#         self.options = kwargs%0A#         self.wrapped = settings._wrapped%0A%0A#     def __enter__(self):%0A#         self.enable()%0A%0A#     def __exit__(self, exc_type, exc_value, traceback):%0A#         self.disable()%0A%0A#     def __call__(self, test_func):%0A#         from django.test import TransactionTestCase%0A#         if isinstance(test_func, type) and issubclass(test_func, TransactionTestCase):%0A#             # When decorating a class, we need to construct a new class%0A#             # with the same name so that the test discovery tools can%0A#             # get a useful name.%0A#             def _pre_setup(innerself):%0A#                 self.enable()%0A#                 test_func._pre_setup(innerself)%0A#             def _post_teardown(innerself):%0A#                 test_func._post_teardown(innerself)%0A#                 self.disable()%0A#             inner = type(%0A#                 test_func.__name__,%0A#                 (test_func,),%0A#                 %7B%0A#                     '_pre_setup': _pre_setup,%0A#                     '_post_teardown': _post_teardown,%0A#                     '__module__': test_func.__module__,%0A#                 %7D)%0A#         else:%0A#             @wraps(test_func)%0A#             def inner(*args, **kwargs):%0A#                 with self:%0A#                     return test_func(*args, **kwargs)%0A#         return inner%0A%0A#     def enable(self):%0A#         class OverrideSettingsHolder(UserSettingsHolder):%0A#             def __getattr__(self, name):%0A#                 if name == %22default_settings%22:%0A#                     return self.__dict__%5B%22default_settings%22%5D%0A#                 return getattr(self.default_settings, name)%0A%0A#         override = OverrideSettingsHolder(copy.copy(settings._wrapped))%0A#         for key, new_value in self.options.iteritems():%0A#             if new_value is SETTING_DELETED:%0A#                 try:%0A#                     delattr(override.default_settings, key)%0A#                 except AttributeError:%0A#                     pass%0A#             else:%0A#                 setattr(override, key, new_value)%0A#         settings._wrapped = override%0A%0A#     def disable(self):%0A#         settings._wrapped = self.wrapped%0A%0A
 def 

Delete old override_settings