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() for key, value in default_settings.items(): configured = settings.get(key, value) settings[key] = utils.read_env(key, configured) deprecated_settings = [ ('cliquet.cache_pool_maxconn', 'cliquet.cache_pool_size'), ('cliquet.storage_pool_maxconn', 'cliquet.storage_pool_size'), ('cliquet.basic_auth_enabled', 'multiauth.policies') ] for old, new in deprecated_settings: if old in settings: msg = "'%s' setting is deprecated. Use '%s' instead." % (old, new) warnings.warn(msg, DeprecationWarning) if old == 'cliquet.basic_auth_enabled': # Transform former setting into pyramid_multiauth config: is_already_set = 'basicauth' in settings['multiauth.policies'] if asbool(settings.pop(old)) and not is_already_set: settings['multiauth.policies'] += ' basicauth' else: settings[new] = settings.pop(old) config.add_settings(settings)
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('cliquet.') or key.startswith(project_name + '.'): unprefixed = key.split('.', 1)[1] project_prefix = project_name + '.' + unprefixed cliquet_prefix = 'cliquet.' + unprefixed return unprefixed, project_prefix, cliquet_prefix # Fill settings with default values if not defined. for key, default_value in sorted(default_settings.items()): unprefixed, project_prefix, cliquet_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, cliquet_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) # Override settings from OS env values. # e.g. HTTP_PORT, KINTO_HTTP_PORT, CLIQUET_HTTP_PORT from_env = utils.read_env(unprefixed, value) from_env = utils.read_env(project_prefix, from_env) from_env = utils.read_env(cliquet_prefix, from_env) settings[unprefixed] = from_env config.add_settings(settings)
def load_default_settings(config): """Read settings provided in Paste ini file, set default values and replace if defined as environment variable. """ settings = config.get_settings() for key, value in DEFAULT_SETTINGS.items(): configured = settings.get(key, value) settings[key] = utils.read_env(key, configured) deprecated_settings = [ ('cliquet.cache_pool_maxconn', 'cliquet.cache_pool_size'), ('cliquet.storage_pool_maxconn', 'cliquet.storage_pool_size'), ] for old, new in deprecated_settings: if old in settings: msg = "'%s' setting is deprecated. Use '%s' instead." % (old, new) warnings.warn(msg, DeprecationWarning) settings[new] = settings.pop(old) config.add_settings(settings)
def test_return_env_value_is_coerced_to_python(self): os.environ.setdefault('CLIQUET_CONF_NAME', '3.14') self.assertEqual(read_env('cliquet-conf.name', 12), 3.14)
def test_return_env_name_as_uppercase(self): os.environ.setdefault('CLIQUET_NAME', 'abc') self.assertEqual(read_env('cliquet.name', 12), 'abc')
def test_return_env_value_if_defined_in_env(self): os.environ.setdefault('CLIQUET_CONF', 'abc') self.assertEqual(read_env('CLIQUET_CONF', 12), 'abc')
def test_return_passed_value_if_not_defined_in_env(self): self.assertEqual(read_env('missing', 12), 12)
def test_return_env_value_is_coerced_to_python(self): os.environ.setdefault("CLIQUET_CONF_NAME", "3.14") self.assertEqual(read_env("cliquet-conf.name", 12), 3.14)
def test_return_env_name_as_uppercase(self): os.environ.setdefault("CLIQUET_NAME", "abc") self.assertEqual(read_env("cliquet.name", 12), "abc")
def test_return_env_value_if_defined_in_env(self): os.environ.setdefault("CLIQUET_CONF", "abc") self.assertEqual(read_env("CLIQUET_CONF", 12), "abc")
def __init__(self, settings): super(MozillaHekaRenderer, self).__init__() self.appname = settings['cliquet.project_name'] self.hostname = utils.read_env('HOSTNAME', os.uname()[1]) self.pid = os.getpid()
def __init__(self, settings): super(MozillaHekaRenderer, self).__init__() self.appname = settings['project_name'] self.hostname = utils.read_env('HOSTNAME', os.uname()[1]) self.pid = os.getpid()