settings.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. """
  2. Settings and configuration for Django.
  3. Values will be read from the module specified by the DJANGO_SETTINGS_MODULE environment
  4. variable, and then from django.conf.global_settings; see the global settings file for
  5. a list of all possible variables.
  6. """
  7. import os
  8. import sys
  9. from django.conf import global_settings
  10. ENVIRONMENT_VARIABLE = "DJANGO_SETTINGS_MODULE"
  11. # get a reference to this module (why isn't there a __module__ magic var?)
  12. me = sys.modules[__name__]
  13. # update this dict from global settings (but only for ALL_CAPS settings)
  14. for setting in dir(global_settings):
  15. if setting == setting.upper():
  16. setattr(me, setting, getattr(global_settings, setting))
  17. # try to load DJANGO_SETTINGS_MODULE
  18. try:
  19. me.SETTINGS_MODULE = os.environ[ENVIRONMENT_VARIABLE]
  20. if not me.SETTINGS_MODULE: # If it's set but is an empty string.
  21. raise KeyError
  22. except KeyError:
  23. raise EnvironmentError, "Environment variable %s is undefined." % ENVIRONMENT_VARIABLE
  24. try:
  25. mod = __import__(me.SETTINGS_MODULE, '', '', [''])
  26. except ImportError, e:
  27. raise EnvironmentError, "Could not import %s '%s' (is it on sys.path?): %s" % (ENVIRONMENT_VARIABLE, me.SETTINGS_MODULE, e)
  28. # Settings that should be converted into tuples if they're mistakenly entered
  29. # as strings.
  30. tuple_settings = ("INSTALLED_APPS", "TEMPLATE_DIRS")
  31. for setting in dir(mod):
  32. if setting == setting.upper():
  33. setting_value = getattr(mod, setting)
  34. if setting in tuple_settings and type(setting_value) == str:
  35. setting_value = (setting_value,) # In case the user forgot the comma.
  36. setattr(me, setting, setting_value)
  37. # Expand entries in INSTALLED_APPS like "django.contrib.*" to a list
  38. # of all those apps.
  39. new_installed_apps = []
  40. for app in me.INSTALLED_APPS:
  41. if app.endswith('.*'):
  42. appdir = os.path.dirname(__import__(app[:-2], '', '', ['']).__file__)
  43. for d in os.listdir(appdir):
  44. if d.isalpha() and os.path.isdir(os.path.join(appdir, d)):
  45. new_installed_apps.append('%s.%s' % (app[:-2], d))
  46. else:
  47. new_installed_apps.append(app)
  48. me.INSTALLED_APPS = new_installed_apps
  49. # save DJANGO_SETTINGS_MODULE in case anyone in the future cares
  50. me.SETTINGS_MODULE = os.environ.get(ENVIRONMENT_VARIABLE, '')
  51. # move the time zone info into os.environ
  52. os.environ['TZ'] = me.TIME_ZONE
  53. # finally, clean up my namespace
  54. for k in dir(me):
  55. if not k.startswith('_') and k != 'me' and k != k.upper():
  56. delattr(me, k)
  57. del me, k
  58. # as the last step, install the translation machinery and
  59. # remove the module again to not clutter the namespace.
  60. from django.utils import translation
  61. translation.install()
  62. del translation