def load_default_settings(config, default_settings): """Read settings provided in Paste ini file, set default values and replace if defined as environment variable. """ settings = config.get_settings() project_name = settings['project_name'] def _prefixed_keys(key): unprefixed = key if key.startswith('kinto.') or key.startswith(project_name + '.'): unprefixed = key.split('.', 1)[1] project_prefix = project_name + '.' + unprefixed kinto_prefix = 'kinto.' + unprefixed return unprefixed, project_prefix, kinto_prefix # Fill settings with default values if not defined. for key, default_value in sorted(default_settings.items()): unprefixed, project_prefix, kinto_prefix = keys = _prefixed_keys(key) is_defined = len(set(settings.keys()).intersection(set(keys))) > 0 if not is_defined: settings[unprefixed] = default_value for key, value in sorted(settings.items()): unprefixed, project_prefix, kinto_prefix = keys = _prefixed_keys(key) # Fail if not only one is defined. defined = set(settings.keys()).intersection(set(keys)) distinct_values = set([str(settings[d]) for d in defined]) if len(defined) > 1 and len(distinct_values) > 1: names = "', '".join(defined) raise ValueError("Settings '%s' are in conflict." % names) # Maintain backwards compatibility with old settings files that # have backend settings like cliquet.foo (which is now # kinto.core.foo). unprefixed, _, _ = _prefixed_keys(key) CONTAIN_CLIQUET_MODULE_NAMES = [ 'storage_backend', 'cache_backend', 'permission_backend', 'logging_renderer', ] if unprefixed in CONTAIN_CLIQUET_MODULE_NAMES and \ value.startswith('cliquet.'): new_value = value.replace('cliquet.', 'kinto.core.') logger.warn( "Backend settings referring to cliquet are DEPRECATED. " "Please update your {} setting to {} (was: {}).".format( key, new_value, value)) value = new_value # Override settings from OS env values. # e.g. HTTP_PORT, READINGLIST_HTTP_PORT, KINTO_HTTP_PORT from_env = utils.read_env(unprefixed, value) from_env = utils.read_env(project_prefix, from_env) from_env = utils.read_env(kinto_prefix, from_env) settings[unprefixed] = from_env config.add_settings(settings)