def test_reload_uncommented_template(self): getter = getconf.ConfigGetter('TESTNS') getter.getstr('foo', default='foo bar', doc='Foo\nbar') getter.getlist('section2.some_list', default=['first', 'second']) getter.getlist('section2.empty_list', default=[], doc='Emptylist') getter.getbool('section3.true_bool', default=True, doc='Important bool value') content = getter.get_ini_template() with tempfile.NamedTemporaryFile('wt', delete=False) as f: for line in content.splitlines(): if line.startswith(';') and not line.startswith('; '): # Only uncomment entries, not comment line = line[1:] f.write(line + '\n') other_getter = getconf.ConfigGetter('TESTNS', config_files=(f.name, )) try: self.assertEqual('foo bar', other_getter.getstr('foo')) self.assertEqual(['first', 'second'], other_getter.getlist('section2.some_list')) self.assertEqual([], other_getter.getlist('section2.empty_list')) self.assertTrue(other_getter.getlist('section3.true_bool')) finally: os.unlink(f.name)
def set_setting(conffile, settings, s, k): config = getconf.ConfigGetter('matrixbot', config_files=[ '/etc/matrixbot/settings.ini', '.matrixbot.ini', conffile ], defaults=settings) if s == "DEFAULT": if k == "loglevel": settings[s][k] = config.getint(k) elif k == "period": settings[s][k] = config.getint(k) else: settings[s][k] = config.get(k) else: if s == "matrix" and k == "rooms": settings[s][k] = config.getlist("%s.%s" % (s, k)) elif s == "ldap" and k == "groups": settings[s][k] = config.getlist("%s.%s" % (s, k)) # Load the LDAP filters for g in settings[s][k]: settings[s][g] = config.get("%s.%s" % (s, g)) elif s == "aliases" and k == "aliases": settings[s][k] = config.getlist("%s.%s" % (s, k)) # Load the command aliases for a in settings[s][k]: settings[s][a] = config.get("%s.%s" % (s, a)) elif s in ["subscriptions", "revokations"] and k == "rooms": settings[s][k] = config.getlist("%s.%s" % (s, k)) # Load the rooms where invite/kick users for a in settings[s][k]: settings[s][a] = config.get("%s.%s" % (s, "room-" + a)) else: settings[s][k] = config.get("%s.%s" % (s, k))
def _load_config(self): for ini_file in self.ini_files: if (ini_file is not None) and (not os.path.exists(ini_file)): logger.warning('ini-file does not exist: %s', ini_file) self._config = getconf.ConfigGetter(self.env_namespace, config_files=self.ini_files) self._service_cache = {}
def __init__(self): config = getconf.ConfigGetter( 'fpost', ['./app/config.ini', './app/settings.ini']) self.__db = config.get('db.db', '') self.__host = config.get('db.host', '') self.__user = config.get('db.user', '') self.__pas = config.get('db.pas', '')
def test_get_defaults_dict(self): """Test fetching from the default config dict with non-string values.""" warnings.warn( "Use of get() directly is deprecated. Use .getstr() instead", DeprecationWarning) getter = getconf.ConfigGetter( 'TESTNS', defaults={'DEFAULT': { "test": '54', 'wrong': 'bad' }}) self.assertEqual('54', getter.get('test')) self.assertEqual('54', getter.getstr('test')) self.assertEqual(54, getter.getint('test')) with self.assertRaises(ValueError): getter.getint('wrong') self.assertEqual(54.0, getter.getfloat('test')) with self.assertRaises(ValueError): getter.getfloat('wrong') self.assertEqual(False, getter.getbool('test')) self.assertEqual([ '54', ], getter.getlist('test'))
def test_getlist_defaults_raises(self): """Test fetching from the default config dict with non-list or string values.""" getter = getconf.ConfigGetter('TESTNS') self.assertRaises(AssertionError, lambda: getter.getlist('test', False)) self.assertRaises(AssertionError, lambda: getter.getlist('test', 42)) self.assertRaises(AssertionError, lambda: getter.getlist('test', 4.2))
def test_gettimedelta_defaults(self): """Test fetching the default in every possible way""" getter = getconf.ConfigGetter('TESTNS') self.assertEqual(datetime.timedelta(), getter.gettimedelta('test')) self.assertEqual(datetime.timedelta(days=100), getter.gettimedelta('foo', '100d')) self.assertIsNone(getter.gettimedelta('test', None))
def test_gettimedelta_defaults_raises(self): """Test fetching from the default config dict with non-str values.""" getter = getconf.ConfigGetter('TESTNS') self.assertRaises(AssertionError, getter.gettimedelta, 'test', False) self.assertRaises(AssertionError, getter.gettimedelta, 'test', 42) self.assertRaises(AssertionError, getter.gettimedelta, 'test', 4.2) self.assertRaises(AssertionError, getter.gettimedelta, 'test', (1, ))
def test_environ_section(self): """Test fetching section.key from environment.""" getter = getconf.ConfigGetter('TESTNS') with Environ(TESTNS_FOO_BAR='blah'): self.assertEqual('blah', getter.getstr('foo.bar', 'foo')) self.assertEqual('', getter.getstr('foo.baz')) self.assertEqual('', getter.getstr('foo'))
def test_no_settings(self): """Test when no real settings exist.""" getter = getconf.ConfigGetter('TESTNS') self.assertEqual('foo', getter.getstr('bar', 'foo')) self.assertEqual('', getter.getstr('bar')) self.assertEqual([], getter.search_files) self.assertEqual([], getter.found_files)
def test_nonexistent_file(self): """Test fetching from a non-existent config file.""" getter = getconf.ConfigGetter('TESTNS', ['/invalid/path']) with Environ(TESTNS_FOO='blah'): # A non-file-defined value self.assertEqual('blah', getter.getstr('foo', 'foo')) self.assertEqual(['/invalid/path'], getter.search_files) self.assertEqual([], getter.found_files)
def test_getlist_nonempty_default(self): getter = getconf.ConfigGetter('TESTNS', []) self.assertEqual(['x', 'foo', 'bar'], getter.getlist('foo', 'x,,foo , , bar,,')) self.assertEqual(['x', 'foo', 'bar'], getter.getlist('foo', ['x', 'foo', 'bar'])) self.assertEqual(['x', 'foo', 'bar'], getter.getlist('foo', ('x', 'foo', 'bar')))
def test_getpath_defaults(self): """Test fetching a pathlib.Path""" getter = getconf.ConfigGetter('TESTNS') self.assertEqual(Path('.'), getter.getpath('test')) self.assertEqual(Path('foo'), getter.getpath('test', Path('foo'))) # "raw" paths tolerated and converted to Path self.assertEqual(Path('foo'), getter.getpath('test', 'foo')) self.assertIsNone(getter.getpath('test', None))
def test_named_arguments(self): """Test fetching from default and file with named arguments""" getter = getconf.ConfigGetter('TESTNS', [self.example_path], defaults={'DEFAULT': { "test": '54' }}) self.assertEqual('54', getter.getstr('test', 'foo')) self.assertEqual('13', getter.getstr('section1.foo', 'foo'))
def test_getfloat_value(self): getter = getconf.ConfigGetter('TESTNS', []) with Environ(TESTNS_FOO='42'): value = getter.getfloat('foo') self.assertEqual(42, value) self.assertEqual(42.0, value) with Environ(TESTNS_FOO='42.3'): self.assertEqual(42.3, getter.getfloat('foo'))
def test_environ_overrides_config(self): """Tests that environment variables take precedence over files.""" getter = getconf.ConfigGetter('TESTNS', [self.example_path]) self.assertEqual('42', getter.getstr('bar')) self.assertEqual('13', getter.getstr('section1.foo')) with Environ(TESTNS_BAR='blah', TESTNS_SECTION1_FOO='baz'): self.assertEqual('blah', getter.getstr('bar')) self.assertEqual('baz', getter.getstr('section1.foo'))
def test_getpath_defaults_raises(self): """Test fetching from the default config dict with non-Path values.""" getter = getconf.ConfigGetter('TESTNS') self.assertRaises(AssertionError, getter.getpath, 'test', [Path("foo")]) self.assertRaises(AssertionError, getter.getpath, 'test', False) self.assertRaises(AssertionError, getter.getpath, 'test', 42) self.assertRaises(AssertionError, getter.getpath, 'test', 4.2) self.assertRaises(AssertionError, getter.getpath, 'test', (1, ))
def test_getbool_defaults_raises(self): """Test fetching from the default config dict with non-bool values.""" getter = getconf.ConfigGetter('TESTNS') self.assertRaises(AssertionError, lambda: getter.getbool('test', 'False')) self.assertRaises(AssertionError, lambda: getter.getbool('test', 42)) self.assertRaises(AssertionError, lambda: getter.getbool('test', 4.2)) self.assertRaises(AssertionError, lambda: getter.getbool('test', (1, )))
def test_get_defaults(self): """Test fetching from the default config dict.""" warnings.warn( "Use of get() directly is deprecated. Use .getstr() instead", DeprecationWarning) getter = getconf.ConfigGetter('TESTNS') self.assertEqual('', getter.get('test')) self.assertEqual('foo', getter.get('test', 'foo')) self.assertIsNone(getter.get('test', None))
def test_environ_empty_namespace(self): """Tests that empty namespace maps to correct environment variables.""" getter = getconf.ConfigGetter(getconf.NO_NAMESPACE, [self.example_path]) self.assertEqual('42', getter.getstr('bar')) self.assertEqual('13', getter.getstr('section1.foo')) with Environ(BAR='blah', SECTION1_FOO='baz'): self.assertEqual('blah', getter.getstr('bar')) self.assertEqual('baz', getter.getstr('section1.foo'))
def test_from_pathlib_path(self): """Paths accepted as config files""" example_directory = Path(__file__).parent / 'config' example_path = example_directory / 'example.ini' invalid_path = example_directory / 'invalid.ini' getter = getconf.ConfigGetter('TESTNS', [example_path, invalid_path]) self.assertEqual( [str(example_path), str(invalid_path)], getter.search_files) self.assertEqual([str(example_path)], getter.found_files)
def test_environ_dash_declaration(self): """Test when a section declaration contains a dash""" getter = getconf.ConfigGetter('TESTNS', [self.example_path]) with Environ( TESTNS_NO_INTERPOLATION_NOINTERPOLATION="no-interpolation"): self.assertEqual('no-interpolation', getter.getstr('no-interpolation.nointerpolation')) with Environ(TESTNS_NO_INTERPOLATION_WITH_DASHES="value"): self.assertEqual('value', getter.getstr('no-interpolation.with-dashes'))
def test_file_overide_default(self): """Test that default dict is overridden by file""" getter = getconf.ConfigGetter('TESTNS', [self.example_path], defaults={ 'DEFAULT': { "test": '54' }, 'section1': { 'foo': '72' } }) self.assertEqual('13', getter.getstr('section1.foo', 'foo'))
def test_sub_section(self): """Test fetching a two dimension dict.""" getter = getconf.ConfigGetter('TESTNS', defaults={ 'DEFAULT': { "test": '54' }, 'section1': { 'key': '88' } }) self.assertEqual('88', getter.getstr('section1.key', 'foo'))
def test_environ_defined_file(self): """Test reading from an environment-defined config file.""" with Environ(TESTNS_CONFIG=self.example_path, TESTNS_FOO='blah'): getter = getconf.ConfigGetter('TESTNS', []) self.assertEqual([self.example_path], getter.search_files) self.assertEqual([self.example_path], getter.found_files) # A non-file-defined value self.assertEqual('blah', getter.getstr('foo', 'foo')) # A sectionless file-defined key self.assertEqual('42', getter.getstr('bar')) # A section.key file-defined self.assertEqual('13', getter.getstr('section1.foo'))
def test_environ_overrides_default(self): """Test that default dict is overridden by environment""" getter = getconf.ConfigGetter('TESTNS', defaults={ 'DEFAULT': { "test": '54' }, 'section1': { 'foo': '72' } }) with Environ(TESTNS_SECTION1_FOO='blah'): self.assertEqual('blah', getter.getstr('section1.foo'))
def test_get_defaults_raises(self): """Test fetching from the default config dict with non-string values.""" warnings.warn( "Use of get() directly is deprecated. Use .getstr() instead", DeprecationWarning) getter = getconf.ConfigGetter('TESTNS', defaults={'DEFAULT': { "test": '54' }}) self.assertRaises(AssertionError, getter.get, 'test', False) self.assertRaises(AssertionError, getter.get, 'test', 42) self.assertRaises(AssertionError, getter.get, 'test', 4.2) self.assertRaises(AssertionError, getter.get, 'test', (1, ))
def __init__(self): try: ROLE = os.environ["ROLE"] logging.info("ROLE : %s", ROLE) config_file_name = "{}.ini".format(str(ROLE).lower()) base_path = os.path.dirname(__file__) config_file = os.path.join(base_path, config_file_name) logging.info("loading configuration: %s %s", config_file_name, config_file) self._config = getconf.ConfigGetter(ROLE, [config_file]) except KeyError: logging.exception("please set ROLE environment variable") raise
def test_real_file(self): """Test fetching from the default config file.""" getter = getconf.ConfigGetter('TESTNS', [self.example_path]) self.assertEqual([self.example_path], getter.search_files) self.assertEqual([self.example_path], getter.found_files) with Environ(TESTNS_FOO='blah'): # A non-file-defined value self.assertEqual('blah', getter.getstr('foo', 'foo')) # A sectionless file-defined key self.assertEqual('42', getter.getstr('bar')) # A section.key file-defined self.assertEqual('13', getter.getstr('section1.foo')) # Same with an int self.assertEqual(13, getter.getint('section1.foo'))
def test_environ_defined_globs(self): """Test reading from an environment-defined config globs""" with Environ(TESTNS_CONFIG=os.path.join(self.example_directory, '*2.ini'), TESTNS_FOO='blah'): getter = getconf.ConfigGetter('TESTNS', []) self.assertEqual([os.path.join(self.example_directory, '*2.ini')], getter.search_files) self.assertEqual([self.example2_path], getter.found_files) # A non-file-defined value self.assertEqual('blah', getter.getstr('foo', 'foo')) # A sectionless file-defined key self.assertEqual('', getter.getstr('bar')) # A section.key file-defined self.assertEqual('24', getter.getstr('section1.foo'))