text: """
  Settings and configuration for Astral.
  
  Values will be read from the module specified by the ASTRAL_SETTINGS_MODULE environment
  variable, and then from astral.conf.global_settings; see the global settings file for
  a list of all possible variables.
  
  Borrowed from Django.
  """
  
  import os
  import importlib
  
  from astral.conf import global_settings
  from astral.utils.functional import LazyObject
  
  ENVIRONMENT_VARIABLE = "ASTRAL_SETTINGS_MODULE"
  
  
  class LazySettings(LazyObject):
      """
      A lazy proxy for either global Astral settings or a custom settings object.
      The user can manually configure settings prior to using them. Otherwise,
      Astral uses the settings module pointed to by ASTRAL_SETTINGS_MODULE.
      """
      def _setup(self):
          """
          Load the settings module pointed to by the environment variable. This
          is used the first time we need any settings at all, if the user has not
          previously configured the settings manually.
          """
          try:
              settings_module = os.environ.get(ENVIRONMENT_VARIABLE,
                      'astral.conf.global_settings')
              if not settings_module: # If it's set but is an empty string.
                  raise KeyError
          except KeyError:
              # NOTE: This is arguably an EnvironmentError, but that causes
              # problems with Python's interactive help.
              raise ImportError("Settings cannot be imported, because environment variable %s is undefined." % ENVIRONMENT_VARIABLE)
  
          self._wrapped = Settings(settings_module)
  
      def configure(self, default_settings=global_settings, **options):
          """
          Called to manually configure the settings. The 'default_settings'
          parameter sets where to retrieve any unspecified values from (its
          argument must support attribute access (__getattr__)).
          """
          if self._wrapped != None:
              raise RuntimeError('Settings already configured.')
          holder = UserSettingsHolder(default_settings)
          for name, value in options.items():
              setattr(holder, name, value)
          self._wrapped = holder
  
      def configured(self):
          """
          Returns True if the settings have already been configured.
          """
          return bool(self._wrapped)
      configured = property(configured)
  
  
  class Settings(object):
      def __init__(self, settings_module):
          # update this dict from global settings (but only for ALL_CAPS settings)
          for setting in dir(global_settings):
              if setting == setting.upper():
                  setattr(self, setting, getattr(global_settings, setting))
  
          # store the settings module in case someone later cares
          self.SETTINGS_MODULE = settings_module
  
          try:
              mod = importlib.import_module(self.SETTINGS_MODULE)
          except ImportError, e:
              raise ImportError("Could not import settings '%s' (Is it on sys.path? Does it have syntax errors?): %s" % (self.SETTINGS_MODULE, e))
  
          for setting in dir(mod):
              if setting == setting.upper():
                  setting_value = getattr(mod, setting)
                  setattr(self, setting, setting_value)
  
          # Settings are configured, so we can set up the logger if required
          if self.LOGGING_CONFIG:
              # First find the logging configuration function ...
              logging_config_path, logging_config_func_name = self.LOGGING_CONFIG.rsplit('.', 1)
              logging_config_module = importlib.import_module(logging_config_path)
              logging_config_func = getattr(logging_config_module, logging_config_func_name)
  
              # ... then invoke it with the logging settings
              logging_config_func(self.LOGGING)
  
  
  class UserSettingsHolder(Settings):
      """
      Holder for user configured settings.
      """
      # SETTINGS_MODULE doesn't make much sense in the manually configured
      # (standalone) case.
      SETTINGS_MODULE = None
  
      def __init__(self, default_settings):
          """
          Requests for configuration variables not in this class are satisfied
          from the module specified in default_settings (if possible).
          """
          self.default_settings = default_settings
  
      def __getattr__(self, name):
          return getattr(self.default_settings, name)
  
      def __dir__(self):
          return self.__dict__.keys() + dir(self.default_settings)
  
  settings = LazySettings()
  