def test_flat(self, tmpdir): """Test flat specification works""" yaml_filename = tmpdir / 'config.yaml' yaml_filename.write(YAML_FLAT) cie = ConfigYamlEnv([str(yaml_filename)]) assert cie.get('foo') == 'bar' assert cie.get('foo', namespace='nsbaz') == 'bat' assert cie.get('foo', namespace=['nsbaz']) == 'bat' assert cie.get('foo', namespace=['nsbaz', 'nsbaz2']) == 'bat2'
def test_hierarchical(self, tmpdir): """Test hierarchical specification works""" yaml_filename = tmpdir / 'config.yaml' yaml_filename.write(YAML_HIERARCHY) cie = ConfigYamlEnv([str(yaml_filename)]) assert cie.get('foo') == 'bar' assert cie.get('foo', namespace='nsbaz') == 'bat' assert cie.get('foo', namespace=['nsbaz']) == 'bat' assert cie.get('foo', namespace=['nsbaz', 'nsbaz2']) == 'bat2'
def test_flat_caps(self, tmpdir): """Test case-insensitive""" yaml_filename = tmpdir / 'config.yaml' yaml_filename.write(YAML_FLAT) cie = ConfigYamlEnv([str(yaml_filename)]) assert cie.get('foo') == 'bar' assert cie.get('FOO') == 'bar' assert cie.get('Foo') == 'bar' assert cie.get('foo', namespace='nsbaz') == 'bat' assert cie.get('foo', namespace='NsBaz') == 'bat' assert cie.get('FOO', namespace='NSBAZ') == 'bat'
def test_multiple_files(self, tmpdir): """Test multiple files--uses first found""" yaml_filename = tmpdir / 'config.yaml' yaml_filename.write(YAML_FLAT) yaml_filename_2 = tmpdir / 'config2.yaml' yaml_filename_2.write(YAML_FLAT_2) cie = ConfigYamlEnv([str(yaml_filename), str(yaml_filename_2)]) # Only the first found file is loaded, so foo_original does not exist assert cie.get('foo_original') == NO_VALUE cie = ConfigYamlEnv([str(yaml_filename_2)]) # ... but it is there if only the original is loaded (safety check) assert cie.get('foo_original') == 'original'
def get_config(config_file=None): """Loads the configuration Loads either the user supplied configuration, the configuration in the XDG path, or the default config. Configuration may be given incompletely, so if you only supply the color (for example), other configuration values are taken from the defaults. The user can also supply a configuration as a dictionary as an argument to this function, this takes first priority. Parameters ---------- config_file : str or Path Which config to load Returns ------- config : dict The configuration to use Example ------- >>> config = get_config() >>> debug = config.get("debug") # Evaluates to whatever debug is set in # the first configuration found """ environments = [ # Look in OS process environment first ConfigOSEnv(), # Look in YAML files in order specified ConfigYamlEnv(CONFIG_FILES), ] if config_file: environments.insert(0, config_file) manager = ConfigManager( # Specify one or more configuration environments in # the order they should be checked environments=environments, # Provide users a link to documentation for when they hit # configuration errors doc="Check https://example.com/configuration for docs.", ) # Apply the configuration class to the configuration manager # so that it handles option properties like defaults, parsers, # documentation, and so on. return manager.with_options(AppConfig())
def test_flat(self, tmpdir): """Test flat specification works""" yaml_filename = tmpdir / "config.yaml" yaml_filename.write(YAML_FLAT) cie = ConfigYamlEnv([str(yaml_filename)]) assert cie.get("foo") == "bar" assert cie.get("foo", namespace="nsbaz") == "bat" assert cie.get("foo", namespace=["nsbaz"]) == "bat" assert cie.get("foo", namespace=["nsbaz", "nsbaz2"]) == "bat2"
def test_hierarchical(self, tmpdir): """Test hierarchical specification works""" yaml_filename = tmpdir / "config.yaml" yaml_filename.write(YAML_HIERARCHY) cie = ConfigYamlEnv([str(yaml_filename)]) assert cie.get("foo") == "bar" assert cie.get("foo", namespace="nsbaz") == "bat" assert cie.get("foo", namespace=["nsbaz"]) == "bat" assert cie.get("foo", namespace=["nsbaz", "nsbaz2"]) == "bat2"
def test_flat_caps(self, tmpdir): """Test case-insensitive""" yaml_filename = tmpdir / "config.yaml" yaml_filename.write(YAML_FLAT) cie = ConfigYamlEnv([str(yaml_filename)]) assert cie.get("foo") == "bar" assert cie.get("FOO") == "bar" assert cie.get("Foo") == "bar" assert cie.get("foo", namespace="nsbaz") == "bat" assert cie.get("foo", namespace="NsBaz") == "bat" assert cie.get("FOO", namespace="NSBAZ") == "bat"
def test_dummy(): config = ConfigManager([ ConfigYamlEnv('my.yaml'), ConfigOSEnv(), ConfigDictEnv({'COMESFROMDICT': 'comes from dict'}) ]) assert config('comesfromyaml') == 'comes from yaml' assert config('THIS_COMES_FROM_YAML') == 'too' assert config.with_namespace('this').with_namespace( 'comes').with_namespace('from')('yaml') == 'too' assert config('USER') == 'aagibalov' assert config('comesfromdict') == 'comes from dict' try: config('SomeSecret') pytest.fail() except everett.ConfigurationError: pass assert config('SomeSecret', default='SomeDefault') == 'SomeDefault'
def test_non_string_values(self, tmpdir): yaml_filename = tmpdir / 'config.yaml' yaml_filename.write(YAML_NON_STRING_VALUES) with pytest.raises(ConfigurationError): ConfigYamlEnv([str(yaml_filename)])
def test_missing_file(self): cie = ConfigYamlEnv(['/a/b/c/bogus/filename']) assert cie.get('foo') == NO_VALUE
import stomp import time from everett.ext.yamlfile import ConfigYamlEnv from everett.manager import ConfigManager config = ConfigManager([ ConfigYamlEnv('local_config.yaml'), ]) class MyListener(stomp.ConnectionListener): def on_error(self, headers, message): print('received an error "%s"' % message) def on_message(self, headers, message): for k, v in headers.items(): print('header: key %s , value %s' % (k, v)) print('received a message "%s"' % message) with open("messages.log", "a") as logfile: logfile.write(message) YOUR_API_KEY_HERE = config('BMRS_API_KEY') conn = stomp.Connection12(host_and_ports=[('api.bmreports.com', 61613)], use_ssl=True) conn.set_listener('', MyListener()) conn.start() conn.connect(YOUR_API_KEY_HERE, YOUR_API_KEY_HERE, True) conn.subscribe(destination='/topic/bmrsTopic', ack='auto', id='CS_UOB') while conn.is_connected():
def get_program_config(): sources = [ConfigOSEnv()] if "RS_CONFIG_FILE" in os.environ: sources.append(ConfigYamlEnv(os.environ["RS_CONFIG_FILE"])) config = ConfigManager(sources) return MainConfig(config.with_namespace("rs"))
def test_missing_file(self): cie = ConfigYamlEnv(["/a/b/c/bogus/filename"]) assert cie.get("foo") == NO_VALUE