def django_staticfiles(): import imp, sys if 'django' in sys.modules: django = sys.modules['django'] else: django = imp.new_module('django') contrib = imp.new_module('contrib') staticfiles = imp.new_module('staticfiles') @classmethod def raiser(*args, **kwargs): raise TestException staticfiles.__dict__.update( {'finders': type('find', (object, ), { 'find': raiser, })}) contrib.__dict__.update({'staticfiles': staticfiles}) django.__dict__.update({'contrib': contrib}) sys.modules.update({ 'django': django, 'django.contrib': contrib, 'django.contrib.staticfiles': staticfiles }) yield del_keys(sys.modules, 'django', 'django.contrib', 'django.contrib.staticfiles')
def django_exceptions(): import imp, sys if 'django' in sys.modules: django = sys.modules['django'] else: django = imp.new_module('django') core = imp.new_module('core') exceptions = imp.new_module('exceptions') class ImproperlyConfigured(Exception): pass exceptions.__dict__.update({'ImproperlyConfigured': ImproperlyConfigured}) core.__dict__.update({'exceptions': exceptions}) django.__dict__.update({'core': core}) sys.modules.update({ 'django': django, 'django.core': core, 'django.core.exceptions': exceptions }) yield del_keys(sys.modules, 'django', 'django.core', 'django.core.exceptions')
def django_template(): #Code to make # from django import template # register = template.Library() # register.tag('compile', do_compile) #run without a hitch. Uses a bunch of inline class creations import imp, sys if 'django' in sys.modules: django = sys.modules['django'] else: django = imp.new_module('django') template = type( 'template', (object, ), { 'Node': object, 'Library': classmethod(lambda x: type('tag', ( object, ), {'tag': classmethod(lambda x, y, z: None)})) }) django.__dict__.update({'template': template}) sys.modules.update({'django': django}) yield del_keys(sys.modules, 'django')
def django_staticfiles(): import imp, sys if 'django' in sys.modules: django = sys.modules['django'] else: django = imp.new_module('django') contrib = imp.new_module('contrib') staticfiles = imp.new_module('staticfiles') @classmethod def raiser(*args, **kwargs): raise TestException staticfiles.__dict__.update({ 'finders': type('find', (object,), { 'find': raiser, }) }) contrib.__dict__.update({'staticfiles': staticfiles}) django.__dict__.update({'contrib': contrib}) sys.modules.update({ 'django': django, 'django.contrib': contrib, 'django.contrib.staticfiles': staticfiles }) yield del_keys(sys.modules, 'django', 'django.contrib', 'django.contrib.staticfiles')
def django_template(): #Code to make # from django import template # register = template.Library() # register.tag('compile', do_compile) #run without a hitch. Uses a bunch of inline class creations import imp, sys if 'django' in sys.modules: django = sys.modules['django'] else: django = imp.new_module('django') template = type('template', (object,), { 'Node': object, 'Library': classmethod(lambda x: type('tag', (object, ), { 'tag': classmethod(lambda x, y, z: None) })) }) django.__dict__.update({'template':template}) sys.modules.update({ 'django':django }) yield del_keys(sys.modules, 'django')
def django_settings(settings = {}): #There be magic here! import imp, sys base_settings = { 'MEDIA_ROOT': '/', 'MEDIA_URL': '/test/', 'INSTALLED_APPS': [], } base_settings.update(settings) #Create some fake django modules if 'django' in sys.modules: django = sys.modules['django'] else: django = imp.new_module('django') conf = imp.new_module('conf') #Make the settings object and stick it in the conf module conf.__dict__.update({ 'settings': type('settings', (object,), base_settings) }) #Stick conf in the django module django.__dict__.update({'conf': conf}) #Add the modules in directly (they appear as builtin) sys.modules.update({ 'django': django, 'django.conf': conf, }) #Ready to test! yield #Context over, reset modules del_keys(sys.modules, 'django', 'django.conf')
def django_settings(settings={}): #There be magic here! import imp, sys base_settings = { 'MEDIA_ROOT': '/', 'MEDIA_URL': '/test/', 'INSTALLED_APPS': [], } base_settings.update(settings) #Create some fake django modules if 'django' in sys.modules: django = sys.modules['django'] else: django = imp.new_module('django') conf = imp.new_module('conf') #Make the settings object and stick it in the conf module conf.__dict__.update( {'settings': type('settings', (object, ), base_settings)}) #Stick conf in the django module django.__dict__.update({'conf': conf}) #Add the modules in directly (they appear as builtin) sys.modules.update({ 'django': django, 'django.conf': conf, }) #Ready to test! yield #Context over, reset modules del_keys(sys.modules, 'django', 'django.conf')