Source

django-envconf / environment.py

Full commit
from django.utils.importlib import import_module
import sys
from inspect import ismodule

class EnvironmentResolver(object):
    def __init__(self, project_name):
        self.project_name = project_name
    
    def resolve_environment(self):
        try:
            environment = import_module('environment')

            self.get_environment_settings(environment)
        except:
            self.settings = self.load_settings('settings')

    def get_environment_settings(self, environment):
        try:
            environment_attribute = environment.getattr("ENVIRONMENT")
            self.settings_module_name = environment_attribute() if callable(environment_attribute) else environment_attribute
        except:
            raise AttributeError("Could not find settings environment.")

        self.settings = self.load_settings(self.settings_module_name)

    def load_settings(self, module_name, environment = None):
            try:
                return import_module(module_name, self.project_name)
            except ImportError:
                message = "Could not import settings."

                if environment:
                    message += "from %s" % environment

                raise ImportError(message)

class EnvironmentLoader(object):
    def load_environment(self):
        pass