Beispiel #1
0
def test_as_dict_ns():

    config = MergedConfiguration([D1])

    c = config.as_dict('c')
    assert c['ca'] == '131'
    assert c['cb'] == '132'
Beispiel #2
0
 def __init__(self):
     self.cfg = MergedConfiguration([
         YamlConfigFile("/etc/myapp.yaml"),
         YamlConfigFile("~/.myapp.yaml"),
         YamlConfigFile(os.environ.get("MYAPPCONF")),
         OSEnvConfig("MYAPP"),
     ])
Beispiel #3
0
def test_subset_source():

    a = {'a': '1',
         'b': '1',
         'c': '1'
        }
    b = {'a': '2',
         'b': '2',
         'c': '2'
        }
    a_src = DictSource(a)
    b_src = DictSource(b)
    config = MergedConfiguration([a_src, b_src, SubsetSource(a_src, set('a'))])

    assert config.get('a') == '1'
    assert config.get('b') == '2'
    assert config.get('c') == '2'
Beispiel #4
0
def test_osenviron_source():

    cur_keys = set(os.environ.keys())
    os.environ.update({
        'MINCFGTST_A': 'a',
        'MINCFGTST_B': 'b',
        'MINCFGTST_C_A': 'ca',
        'MINCFGTST_C_B': 'cb',
    })

    shell = os.environ.get('SHELL', 'no-shell-defined')
    user = os.environ.get('USER', 'unknown user')

    config = MergedConfiguration([OSEnvironSource('MINCFGTST')])
    assert config.get('a') == 'a'
    assert config.get('b') == 'b'
    assert config.get('a', namespace=['c']) == 'ca'
    assert config.get('b', namespace=['c']) == 'cb'
Beispiel #5
0
def test_yaml_file_source(tmp_path):

    nonexistantsrc = YamlFileSource(None)

    # make up a tempfile name
    cfgfile = tmp_path / "config.yaml"

    # write test config to the temp file
    cfgfile.write_text("a: 1\nb: 2\nc:\n  ca: 3\n  cb: 4\n\n")

    cfgfilesrc = YamlFileSource(str(cfgfile))

    # point the config at it
    config = MergedConfiguration([nonexistantsrc, cfgfilesrc])

    assert config.get('a') == '1'
    assert config.get('b') == '2'
    assert config.get('ca', namespace=['c']) == '3'
    assert config.get('cb', namespace=['c']) == '4'
Beispiel #6
0
def test_dotenv_file_source(tmp_path):

    nonexistantsrc = DotEnvFileSource(None)

    # make up a tempfile name
    cfgfile = tmp_path / "config.env"

    # write test config to the temp file
    cfgfile.write_text("a=1\nb=2\nc_a=3\nc_b=4\n\n")

    cfgfilesrc = DotEnvFileSource(str(cfgfile))

    # point the config at it
    config = MergedConfiguration([nonexistantsrc, cfgfilesrc])

    assert config.get('a') == '1'
    assert config.get('b') == '2'
    assert config.get('a', namespace=['c']) == '3'
    assert config.get('b', namespace=['c']) == '4'
Beispiel #7
0
def test_merged_case_insensitive():

    config = MergedConfiguration([D1, D2])

    assert config.get('a') == '21'
    assert config.get('b') == '12'
    assert config.get('d') == '24'

    for k in ('a', 'b', 'd'):
        assert config.get(k.lower()) == config.get(k.upper())
Beispiel #8
0
class MyComplexConfig:
    def __init__(self):
        self.cfg = MergedConfiguration([
            YamlConfigFile("/etc/myapp.yaml"),
            YamlConfigFile("~/.myapp.yaml"),
            YamlConfigFile(os.environ.get("MYAPPCONF")),
            OSEnvConfig("MYAPP"),
        ])

    def subbar_cfg(self):
        '''
        returns the configuration values in the 'subbar' namespace
        as an object.  So using the example config, the result would be:
            SimpleNamespace(bar="1", baz="2", blah="3", brr="4")
        '''
        return self.cfg.as_ns(['subbar'])