import imp
import os
import sys

from .dicts import DotDict


def config_sources(app, environment, cluster, deployconfigs_dirs, app_dir):
    '''Return a list of the configuration files used by app in the specified
    environment+cluster
    '''
    sources = [
        # Environment + Cluster
        (deployconfigs_dirs, 'common-%s' % environment),
        (deployconfigs_dirs, 'common-%s-%s' % (environment, cluster)),
        # Global
        (deployconfigs_dirs, 'common'),
        # Machine-specific overrides
        (deployconfigs_dirs, 'common-overrides'),
        # Application-specific
        ([app_dir], '%s-default' % app),
        (deployconfigs_dirs, '%s-%s' % (app, environment)),
        (deployconfigs_dirs, '%s-%s-%s' % (app, environment, cluster)),
        # Application override
        (deployconfigs_dirs, '%s-overrides' % app),
    ]
    for dirs, name in sources:
        for directory in dirs:
            fn = os.path.join(directory, name) + '.py'
            if os.path.isfile(fn):
                yield fn
                break


def smush_config(sources):
    '''Merge the configuration files specified, and return the resulting
    DotDict
    '''
    if 'yola.config' not in sys.modules:
        sys.modules['yola.config'] = imp.new_module('yola.config')
    config = DotDict()
    for fn in sources:
        mod_name = 'yola.config.' + os.path.basename(fn).rsplit('.', 1)[-1]
        description = ('.py', 'r', imp.PY_SOURCE)
        with open(fn) as f:
            m = imp.load_module(mod_name, f, fn, description)
            config = m.update(config)
    return config

@@ -1178,28 +1178,161 @@
 '''%0A
+%0A
     
-if 'yola.config'
+# Create a fake module that we can import the configuration-generating%0A    # modules into%0A    fake_mod = 'yola.configurator.configs'%0A    if fake_mod
  not
@@ -1368,29 +1368,24 @@
 modules%5B
-'yola.config'
+fake_mod
 %5D = imp.
@@ -1399,23 +1399,19 @@
 ule(
-'yola.config'
+fake_mod
 )%0A
+%0A
     
@@ -1471,28 +1471,28 @@
 _name = 
-'yola.config
+fake_mod + '
 .' + os.

Rename the fake module to yola.configurator.configs