Beispiel #1
0
def test_Config_read_merge_False(tmpdir, cfg_source, cfg2):
    config = Config(cfg_source)
    tmpfile = tmpdir.join("read_nomerge.config").strpath
    cfg2.write(tmpfile)
    config.read(tmpfile, merge=False)
    assert compare_dicts(config, cfg2)
    assert len(config) == 2
Beispiel #2
0
def test_main_create_template_evaluate(files, string_io, evaluate):
    o_name = "xe-template.zirkon"
    s_name = "xe.zirkon-schema"
    with files.tmp(o_name):
        o_file = files[o_name]
        s_file = files[s_name]

        assert not os.path.exists(files[o_name])

        args = ["create", "-s", s_file, "-o", o_file]
        if evaluate:
            args.append("-e")
        log_stream, out_stream = run(args)

        config = Config()
        config.read(o_file, fmt="zirkon")
        config.dump(string_io)
        if evaluate:
            assert string_io.getvalue() == """\
a = 10
b = 20
c = 200
"""
        else:
            assert string_io.getvalue() == """\
Beispiel #3
0
def test_Config_defaults_sub():
    config = Config(defaults=True)
    config['a'] = 10
    config['sub'] = {}
    config['sub']['x'] = 1
    config['sub']['subsub'] = {}
    config['sub']['subsub']['xx'] = 11
    config['sub']['subsub']['subsubsub'] = {}
    config['sub']['subsub']['subsubsub']['xxx'] = 111
    config['sub']['subsub']['subsubsub']['yyy'] = 222
    config['sub']['subsub']['yy'] = 22
    config['sub']['y'] = 2
    config['b'] = 20

    config2 = Config(defaults=True)
    config2.set_defaults(**config)
    assert len(config2) == 0

    assert config2['a'] == 10
    assert config2['sub']['x'] == 1
    assert config2['sub']['subsub']['xx'] == 11
    assert config2['sub']['subsub']['subsubsub']['xxx'] == 111
    assert config2['sub']['subsub']['subsubsub']['yyy'] == 222
    assert config2['sub']['subsub']['yy'] == 22
    assert config2['sub']['y'] == 2
    assert config2['b'] == 20
    assert len(config2) == 1
    assert config2.has_option('a')
    assert config2.has_section('sub')
Beispiel #4
0
def test_Config_copy(defaultsvalue):
    config = Config(defaults=defaultsvalue)
    config['a'] = 10
    config.set_defaults(b=20)
    config2 = config.copy()
    assert config2 == config
    assert isinstance(config2, Config)
def test_Schema_PATH_add_default(tmpdir, defaults):
    fn_config = tmpdir.join("PATH_add_default.config").strpath
    fn_schema = tmpdir.join("PATH_add_default.schema").strpath
    dirname = os.path.dirname(fn_config)

    config = Config(defaults=defaults)
    schema = Schema()

    with open(fn_config, "w") as f_out:
        f_out.write("""\
[sub]
""")
    config.read(fn_config, merge=True)

    with open(fn_schema, "w") as f_out:
        f_out.write("""\
a = Str(default=PATH.dirname + 'a')
[sub]
    b = Str(default=PATH.dirname + 'b')
""")
    schema.read(fn_schema, merge=True)

    schema.validate(config)
    assert config['a'] == dirname + 'a'
    assert config['sub']['b'] == dirname + 'b'
Beispiel #6
0
def test_Config_defaults_add_subsub():
    config = Config(defaults=True)
    config['x'] = 10
    config.set_defaults(a=1,
                        sub={
                            'sa': 2,
                            'subsub': {
                                'ssa': 3,
                                'empty1': {},
                                'empty2': {
                                    'ee1': {},
                                    'ee2': {}
                                },
                                'subsubsub': {
                                    'sss': 4
                                },
                                'ssb': 5
                            },
                            'sb': 6
                        },
                        b=7)
    assert config.has_section('sub')
    assert config['sub'].has_section('subsub')
    assert config['sub']['subsub'].has_section('subsubsub')
    assert not config['sub']['subsub'].has_section('empty1')
    assert not config['sub']['subsub'].has_section('empty2')
Beispiel #7
0
def test_Config_defaults_get_defaults():
    config = Config(defaults=True)
    config.set_defaults(a={'x': 1}, b=10)
    assert isinstance(config.defaults, Section)
    assert config.defaults == {'a': {'x': 1}, 'b': 10}
    config = Config(defaults=False)
    assert config.defaults is None
Beispiel #8
0
def test_Config_create_dictionary_init(dictionary, simple_config_content,
                                       string_io, defaultsvalue):
    config = Config(dictionary=dictionary,
                    init=simple_config_content,
                    defaults=defaultsvalue)
    config.dump(stream=string_io)
    assert string_io.getvalue() == SIMPLE_SECTION_DUMP
    assert len(dictionary) > 0
def test_Config_set_schema(use_defaults):
    config = Config()
    schema = Schema(use_defaults=use_defaults)
    assert schema.use_defaults == use_defaults
    schema['x'] = Int(default=10)
    config['x'] = 4
    config.schema = schema
    config.self_validate()
Beispiel #10
0
def test_create_template_from_schema(string_io):
    schema = Schema()
    schema['m0'] = Int(default=3)
    schema['m1'] = Int(default=ROOT['m0'] + 1)
    config = Config()
    create_template_from_schema(schema, config=config)
    
    config.dump(stream=string_io)
    assert string_io.getvalue() == """\
Beispiel #11
0
def test_Config_defaults_Config():
    defaults = Config()
    defaults["y"] = ROOT["x"] * 10
    config1 = Config(defaults=defaults)
    config1["x"] = 3
    config2 = Config(defaults=defaults)
    config2["x"] = 7
    assert config1["y"] == 30
    assert config2["y"] == 70
Beispiel #12
0
def test_Config_defaults_add_subsub():
    config = Config(defaults=True)
    config['x'] = 10
    config.set_defaults(a=1, sub={'sa': 2, 'subsub': {'ssa': 3, 'empty1': {}, 'empty2': {'ee1': {}, 'ee2': {}}, 'subsubsub': {'sss': 4}, 'ssb': 5}, 'sb': 6}, b=7)
    assert config.has_section('sub')
    assert config['sub'].has_section('subsub')
    assert config['sub']['subsub'].has_section('subsubsub')
    assert not config['sub']['subsub'].has_section('empty1')
    assert not config['sub']['subsub'].has_section('empty2')
Beispiel #13
0
def test_Config_read_merge_default(tmpdir, cfg_source, cfg2):
    config = Config(cfg_source)
    tmpfile = tmpdir.join("read_nomerge.config").strpath
    cfg2.write(tmpfile)
    config.read(tmpfile)
    # config.dump()
    # print("...")
    # cfg2.dump()
    assert compare_dicts(config, cfg2)
    assert len(config) == 2
Beispiel #14
0
def test_Config_defaults_dump(string_io):
    config = Config(defaults=True)
    config['x'] = 1
    config.set_defaults(alpha=10)
    config['sub'] = {'w': 2}
    config['sub'].set_defaults(beta=20, gamma={'x': 11}, delta={})
    assert config['sub']['beta'] == 20
    assert config['sub']['gamma']['x'] == 11
    config.dump(string_io)
    assert string_io.getvalue() == """\
Beispiel #15
0
def test_Config_no_defaults_set_defaults():
    config = Config(defaults=False)
    config.set_defaults(alpha=10, beta={'x': 1})
    assert config.has_option('alpha')
    assert config['alpha'] == 10
    assert len(config) == 2
    assert config.has_section('beta')
    assert config['beta'].has_option('x')
    assert config['beta']['x'] == 1
    assert len(config['beta']) == 1
Beispiel #16
0
def test_main_read_write_2files(tmpdir):
    fn_cfg1 = tmpdir.join("2files_cfg1.config").strpath
    fn_cfg2 = tmpdir.join("2files_cfg2.config").strpath
    fn_sch1 = tmpdir.join("2files_sch1.schema").strpath
    fn_sch2 = tmpdir.join("2files_sch2.schema").strpath

    cfg1 = Config.from_string("""\
a = 10
[c]
    c1 = 20
""")
    cfg2 = Config.from_string("""\
b = 1.5
[d]
    d1 = 2.5
""")

    sch1 = Schema.from_string("""\
a = Int()
[d]
    d1 = Float()
    d2 = Float(default=3.5)
""")
    sch2 = Schema.from_string("""\
b = Float()
[c]
    c1 = Int()
    c2 = Int(default=30)
""")

    cfg1.to_file(fn_cfg1)
    cfg2.to_file(fn_cfg2)
    sch1.to_file(fn_sch1)
    sch2.to_file(fn_sch2)

    args = ["read", "-i", fn_cfg1, "-i", fn_cfg2, "-s", fn_sch1, "-s", fn_sch2, "--defaults"]

    log_stream, out_stream = run(args)
    reference = """\
a = 10
[c]
    c1 = 20
    c2 = 30
b = 1.5
[d]
    d1 = 2.5
    d2 = 3.5

"""
    # print("=== reference:")
    # print(reference)
    # print("=== result:")
    # print(out_stream.getvalue())
    assert out_stream.getvalue() == reference
    assert log_stream.getvalue() == ""
Beispiel #17
0
def test_replace_macros():
    a_value = 10
    c_value = 5
    x_value = 4
    z_value = 7
    config1 = Config(defaults=True)
    config2 = Config(defaults=True)
    for config in config1, config2:
        config['a'] = a_value
        config['b'] = 2 * ROOT['a']
        config['sub'] = {}
        config['sub']['x'] = x_value
        config['sub']['y'] = SECTION['x'] + ROOT['b']
        config.set_defaults(c=c_value, d=3 + ROOT['c'] + SECTION['a'],
                            sub={'z': z_value,
                                 'subsub': {'t': ROOT['a'] - 1},
                                 'w': SECTION['x'] + SECTION['z'] + ROOT['a'] + ROOT['c']})


    def verify(cnfg, a_value, c_value, x_value, z_value, def_a_value=None):
        if def_a_value is None:
            def_a_value = a_value
        assert cnfg['a'] == a_value
        assert cnfg['b'] == 2 * def_a_value
        assert cnfg['sub']['x'] == x_value
        assert cnfg['sub']['y'] == x_value + (2 * def_a_value)
        assert cnfg['sub']['z'] == z_value
        assert cnfg['sub']['w'] == x_value + z_value + def_a_value + c_value
        assert cnfg['c'] == c_value
        assert cnfg['d'] == 3 + c_value + def_a_value

    verify(config1, a_value, c_value, x_value, z_value)
    verify(config2, a_value, c_value, x_value, z_value)

    a_value = 100
    config1['a'] = a_value
    config2['a'] = a_value
    verify(config1, a_value, c_value, x_value, z_value)
    verify(config2, a_value, c_value, x_value, z_value)

    replace_macros(config2)

    new_a_value1 = 67
    config1['a'] = new_a_value1
    config2['a'] = new_a_value1
    verify(config1, new_a_value1, c_value, x_value, z_value)
    verify(config2, new_a_value1, c_value, x_value, z_value, def_a_value=a_value)

    replace_macros(config1)

    new_a_value2 = 67
    config1['a'] = new_a_value2
    config2['a'] = new_a_value2
    verify(config1, new_a_value2, c_value, x_value, z_value, def_a_value=new_a_value1)
    verify(config2, new_a_value2, c_value, x_value, z_value, def_a_value=a_value)
Beispiel #18
0
def test_Config_defaults_eq():
    config = Config(defaults=True)
    config['d'] = 11
    config['e'] = 12
    config.set_defaults(a={}, b=5, c={'x': 7}, d=8)

    config2 = Config(defaults=True)
    config2['d'] = 11
    config2['e'] = 12

    assert config != config2
Beispiel #19
0
def test_Config_defaults_eq():
    config = Config(defaults=True)
    config['d'] = 11
    config['e'] = 12
    config.set_defaults(a={}, b=5, c={'x': 7}, d=8)
    
    config2 = Config(defaults=True)
    config2['d'] = 11
    config2['e'] = 12

    assert config != config2
Beispiel #20
0
def test_Config_defaults_section_add():
    config = Config(defaults=True)
    config.set_defaults(a={'x': 1})
    assert 'a' in config
    config['a']['t'] = 0.1
    assert config['a']['x'] == 1
    assert config['a']['t'] == 0.1
    assert not config.defaults['a'].has_option('t')
    del config['a']
    assert config.has_section('a')
    assert config['a'].has_option('x')
    assert config['a']['x'] == 1
Beispiel #21
0
def test_Config_defaults_section_add():
    config = Config(defaults=True)
    config.set_defaults(a={'x': 1})
    assert 'a' in config
    config['a']['t'] = 0.1
    assert config['a']['x'] == 1
    assert config['a']['t'] == 0.1
    assert not config.defaults['a'].has_option('t')
    del config['a']
    assert config.has_section('a')
    assert config['a'].has_option('x')
    assert config['a']['x'] == 1
Beispiel #22
0
def test_Schema_defaults_defined_in_defaults(use_defaults, has_default):
    config = Config(defaults=True)
    config.defaults["x"] = 10

    schema = Schema(use_defaults=use_defaults)
    int_kwargs = {}
    if has_default:
        int_kwargs["default"] = 100
    schema["x"] = Int(**int_kwargs)

    assert not schema.validate(config)
    assert "x" in config.defaults
    assert config["x"] == 10
Beispiel #23
0
def test_Config_read_merge_True(tmpdir, cfg_source, cfg2):
    cfg1 = Config(cfg_source)
    config = Config(cfg_source)
    tmpfile = tmpdir.join("read_nomerge.config").strpath
    cfg2.write(tmpfile)
    config.read(tmpfile, merge=True)
    assert config != cfg2
    assert len(config) == 4
    assert config["a"] == cfg1["a"]
    assert config["b"]["b0"] == cfg1["b"]["b0"]
    assert config["b"]["b1"] == cfg2["b"]["b1"]
    assert config["c"] == cfg1["c"]
    assert config["d"] == cfg2["d"]
Beispiel #24
0
def test_Config_copy_deep():
    config = Config()
    config['a'] = 10
    config['sub'] = {'x': 10, 'y': 20, 'z': {'w': 30, 't': 40}}
    s = config.to_string(fmt="zirkon")
    # config.dump()
    # print()
    config2 = config.copy()
    del config2["a"]
    del config2["sub"]["y"]
    del config2["sub"]["z"]
    # config.dump()
    s2 = config.to_string(fmt="zirkon")
    assert s == s2
Beispiel #25
0
def test_Config_shared_defaults():
    defaults = DefaultsSection()
    defaults['sub'] = {'x': ROOT['n'] * 3, 'sub': {'x': ROOT['n'] * 4}}
    defaults['x'] = 1000 + ROOT['n']
    config1 = Config(defaults=defaults)
    config1['n'] = 10
    config2 = Config(defaults=defaults)
    config2['n'] = 20
    assert config1['x'] == 1010
    assert config1['sub']['x'] == 30
    assert config1['sub']['sub']['x'] == 40
    assert config2['x'] == 1020
    assert config2['sub']['x'] == 60
    assert config2['sub']['sub']['x'] == 80
Beispiel #26
0
def test_Config_on_shelf(tmpdir):
    with shelve.open(os.path.join(tmpdir.strpath, 'x.shelve')) as shelf:
        flatshelf = FlatMap(dictionary=shelf)
        config = Config(dictionary=flatshelf)
        config['x'] = 10
        config['y'] = {}
        config['y']['a'] = 'aaa'
        config['y']['b'] = 111
        config['y']['c'] = {'v': 10}
        config['y']['c']['w'] = 20
        config['y']['d'] = 888
        config['z'] = 999
        config_stream = string_io()
        config.dump(stream=config_stream)
        config_dump = config_stream.getvalue()
        assert config_dump == """\
z = 999
x = 10
[y]
    b = 111
    [c]
        v = 10
        w = 20
    a = 'aaa'
    d = 888
"""
    with shelve.open(os.path.join(tmpdir.strpath, 'x.shelve')) as shelf2:
        flatshelf2 = FlatMap(dictionary=shelf2)
        config2 = Config(dictionary=flatshelf2)
        config2_stream = string_io()
        config2.dump(stream=config2_stream)
        config2_dump = config2_stream.getvalue()
        assert config2_dump == config_dump
Beispiel #27
0
def test_Config_defaults_copy():
    config = Config(defaults=True)
    config['d'] = 11
    config['e'] = 12
    config.set_defaults(a={}, b=5, c={'x': 7}, d=8)
    config2 = config.copy()
    assert not config2.has_section('a')
    assert config2.has_option('b')
    assert config2['b'] == 5
    assert config2.has_section('c')
    assert len(config2['c']) == 0
    assert config2['c']['x'] == 7
    assert config2.has_option('d')
    assert config2['d'] == 11
    assert config2.has_option('e')
    assert config2['e'] == 12
Beispiel #28
0
def test_Config_macro_error(defaultsvalue):
    config = Config(defaults=defaultsvalue)
    config['a'] = 10
    config['c'] = SECTION['a'] * SECTION['b']
    with pytest.raises(KeyError) as exc_info:
        x = config['c']
    assert str(exc_info.value) == "'b'"
Beispiel #29
0
def test_Config_from_file_configobj(simple_config, tmp_text_file):
    tmp_text_file.write(SIMPLE_CONFIG_CONFIGOBJ_SERIALIZATION)
    tmp_text_file.flush()
    tmp_text_file.seek(0)
    config = Config.from_file(filename=tmp_text_file.name,
                              protocol="configobj")
    assert config == simple_config
Beispiel #30
0
def test_Config_on_shelf_mutable(tmpdir):
    with shelve.open(os.path.join(tmpdir.strpath, 'x_mutable.shelve')) as shelf:
        flatshelf = FlatMap(dictionary=shelf)
        config = Config(dictionary=flatshelf)
        config['a'] = [1, 2]
        config['a'].append(3)
        assert config['a'] == [1, 2]
def config_fmt(fmt, config_de):
    ref_config_de_string = CONFIG_DE_STRING.get(fmt, None)
    if ref_config_de_string:
        config = Config.from_string(ref_config_de_string, fmt=fmt)
    else:
        config = config_de
    return config, fmt
def test_module_config_on_configure_observer_run():
    cb0 = MyCallback(0, 0)
    cb1 = MyCallback(1, 1)
    assert cb0.ab == (0, 0)
    assert cb1.ab == (1, 1)
    module_config = ModuleConfig("""\
a = Int(default=2)
b = Int(default=5)
""",
        on_configure=[cb0, cb1])
    assert cb0.ab == (0, 0)
    assert cb1.ab == (1, 1)
    module_config.configure()
    assert cb0.ab == (2, 5)
    assert cb1.ab == (2, 5)
    cfg = Config.from_string("""\
a = 100
b = 3""")
    module_config.configure(cfg)
    assert cb0.ab == (100, 3)
    assert cb1.ab == (100, 3)
    cfg["a"] = 200
    module_config.configure(cfg)
    assert cb0.ab == (200, 3)
    assert cb1.ab == (200, 3)
    module_config.remove_on_configure_observer(cb1)
    cfg["a"] = 300
    module_config.configure(cfg)
    assert cb0.ab == (300, 3)
    assert cb1.ab == (200, 3)
Beispiel #33
0
def test_Config_defaults_copy():
    config = Config(defaults=True)
    config['d'] = 11
    config['e'] = 12
    config.set_defaults(a={}, b=5, c={'x': 7}, d=8)
    config2 = config.copy()
    assert not config2.has_section('a')
    assert config2.has_option('b')
    assert config2['b'] == 5
    assert config2.has_section('c')
    assert len(config2['c']) == 0
    assert config2['c']['x'] == 7
    assert config2.has_option('d')
    assert config2['d'] == 11
    assert config2.has_option('e')
    assert config2['e'] == 12
def config_protocol(protocol, config_de):
    ref_config_de_string = CONFIG_DE_STRING.get(protocol, None)
    if ref_config_de_string:
        config = Config.from_string(ref_config_de_string, protocol=protocol)
    else:
        config = config_de
    return config, protocol
def mod_config0():
    return Config.from_string("""\
[mod0]
    opt0 = "abc"
[mod1]
    opt0 = 5.2
""", fmt="zirkon")
def config_protocol(protocol, config_de):
    ref_config_de_string = CONFIG_DE_STRING.get(protocol, None)
    if ref_config_de_string:
        config = Config.from_string(ref_config_de_string, protocol=protocol)
    else:
        config = config_de
    return config, protocol
Beispiel #37
0
def test_main_create_template(files, string_io):
    o_name = "x-template.zirkon"
    s_name = "x.zirkon-schema"
    with files.tmp(o_name):
        o_file = files[o_name]
        s_file = files[s_name]

        assert not os.path.exists(files[o_name])

        args = ["create", "-s", s_file, "-o", o_file]
        log_stream, out_stream = run(args)

        config = Config()
        config.read(o_file, protocol="zirkon")
        config.dump(string_io)
        assert string_io.getvalue() == """\
Beispiel #38
0
def test_DefaultsSection1(defaults_section):
    config = Config()
    config['n'] = 5
    config['sub'] = {'y': 100}
    with defaults_section.referencing(config):
        assert defaults_section['x'] == 15
        assert defaults_section['sub']['x'] == 110
        assert defaults_section['sub']['sub']['x'] == 25
def test_module_config_configure(module_config):
    assert not module_config.configured()
    config = Config.from_string("""\
opt0 = 4
""", fmt="zirkon")
    module_config.configure(config)
    assert module_config.configured()
    assert module_config['opt0'] == 4
Beispiel #40
0
def test_main_program_config_ok_no_defaults_options_out(tmpdir):
    ofile = tmpdir.join("pack6.config").strpath
    args = ["program", '-m', 'tests/pack6', '--config', '--defaults', '--evaluate', '--output', ofile, '--', '--pack6-mod1-opt11=True']
    log_stream, out_stream = run(args)
    assert out_stream.getvalue() == """\
"""
    config = Config.from_file(ofile)
    assert config['pack6']['mod1']['opt11'] == True
Beispiel #41
0
def test_Config_sub_setdef():
    config = Config(defaults={'sub': {'a': 100}})
    config['x'] = 10
    config['sub'] = {'b': 3}
    assert config['sub']['a'] == 100
    with pytest.raises(AttributeError):
        config['sub'].defaults = {'a': 200}
    assert config['sub']['a'] == 100
Beispiel #42
0
def test_Config_err_disabled_macros():
    config = Config(macros=False)
    config['x'] = 10
    config['sub'] = {'sub': {}}
    with pytest.raises(ValueError) as exc_info:
        config['y'] = ROOT['x'] + 1
    assert str(exc_info.value) == "cannot set y=ROOT['x'] + 1: macros are not enabled"
    with pytest.raises(ValueError) as exc_info:
        config['sub']['sub']['y'] = ROOT['x'] + 1
    assert str(exc_info.value) == "cannot set y=ROOT['x'] + 1: macros are not enabled"
    config.macros = True
    config['sub']['sub']['y'] = ROOT['x'] + 1
    assert config['sub']['sub']['y'] == 11
    config.macros = False
    with pytest.raises(ValueError) as exc_info:
        c = config['sub']['sub']['y']
    assert str(exc_info.value) == "cannot evaluate ROOT['x'] + 1: macros are not enabled"
Beispiel #43
0
def test_Schema_default_section_subsection_option():
    schema = Schema.from_string("""\
[filesystems]
    [__default_section__]
        path = Str()
        [size]
            min_size_gb = Float(default=0.5)
            max_size_gb = Float(default=1.5)
        [files]
            __default_option__ = Str()
        [__default_section__]
            name = Str()
            __default_option__ = Int()
""")
    config = Config.from_string("""\
[filesystems]
    [home]
        path = "/home/user"
        [files]
            x = "x.txt"
            y = "y.txt"
            z = "y.txt"
        [a]
            name = "alpha"
            v0 = 10
            v1 = 20
            v2 = 30
        [b]
            name = "beta"
            v0 = 5
        [c]
            v0 = 1
            v1 = "g"
            v2 = 2
    [work]
        path = "/tmp/user"
        [size]
            min_size_gb = 0.0
""")
    validation = schema.validate(config)
    assert len(validation) == 1
    assert 'filesystems' in validation
    assert len(validation['filesystems']) == 1
    assert 'home' in validation['filesystems']
    assert len(validation['filesystems']['home']) == 1
    assert 'c' in validation['filesystems']['home']
    assert len(validation['filesystems']['home']['c']) == 2
    assert 'name' in validation['filesystems']['home']['c']
    assert 'v1' in validation['filesystems']['home']['c']

    assert validation
    assert config['filesystems']['home']['size']['min_size_gb'] == 0.5
    assert config['filesystems']['home']['size']['max_size_gb'] == 1.5
    assert 'a' in config['filesystems']['home']
    assert 'b' in config['filesystems']['home']
    assert 'c' in config['filesystems']['home']
    assert config['filesystems']['work']['size']['min_size_gb'] == 0.0
    assert config['filesystems']['work']['size']['max_size_gb'] == 1.5
Beispiel #44
0
def test_Config_defaults_sub():
    config = Config(defaults=True)
    config['a'] = 10
    config['sub'] = {}
    config['sub']['x'] = 1
    config['sub']['subsub'] = {}
    config['sub']['subsub']['xx'] = 11
    config['sub']['subsub']['subsubsub'] = {}
    config['sub']['subsub']['subsubsub']['xxx'] = 111
    config['sub']['subsub']['subsubsub']['yyy'] = 222
    config['sub']['subsub']['yy'] = 22
    config['sub']['y'] = 2
    config['b'] = 20

    config2 = Config(defaults=True)
    config2.set_defaults(**config)
    assert len(config2) == 0

    assert config2['a'] == 10
    assert config2['sub']['x'] == 1
    assert config2['sub']['subsub']['xx'] == 11
    assert config2['sub']['subsub']['subsubsub']['xxx'] == 111
    assert config2['sub']['subsub']['subsubsub']['yyy'] == 222
    assert config2['sub']['subsub']['yy'] == 22
    assert config2['sub']['y'] == 2
    assert config2['b'] == 20
    assert len(config2) == 1
    assert config2.has_option('a')
    assert config2.has_section('sub')
Beispiel #45
0
def defconfig(defaultsvalue):
    config = Config(defaults=defaultsvalue)
    config['n'] = 10
    config['sub'] = {}
    config['sub']['n0'] = 1
    config['sub']['n1'] = ROOT['n'] + SECTION['n0']
    config['sub']['sub'] = {}
    config['sub']['sub']['n2'] = ROOT['n'] * ROOT['sub']['n1']
    return config
Beispiel #46
0
def macro_config(defaultsvalue):
    config = Config(defaults=defaultsvalue)
    config['a'] = 10
    config['b'] = 20
    config['c'] = 10
    config['sub'] = {}
    config['sub']['d'] = 18
    config['sub']['e'] = -100
    return config
def test_Schema_PATH_defaults(macro_schema_PATH):
    filename = "/home/user/PATH.config"
    dirname, basename = os.path.split(filename)
    config = Config.from_string("""\
""", filename=filename, schema=macro_schema_PATH)
    assert config["a"] == os.path.join(dirname, "a_" + basename)
    assert config["b"] == filename + "b"
    assert config["sub"]["c"] == os.path.join(dirname, "c_" + basename)
    assert config["sub"]["d"] == filename + "d"
Beispiel #48
0
def test_Config_defaults_option():
    config = Config(defaults=True)
    config.set_defaults(a=10)
    assert 'a' in config
    assert config.has_key('a')
    assert config.has_option('a')
    assert config['a'] == 10
    assert config.get('a') == 10
    assert config.get_option('a') == 10
    assert len(config) == 0
    assert not 'a' in config.dictionary
def test_Config_use_defaults_False(string_io, use_defaults):
    config = Config()
    schema = Schema(use_defaults=use_defaults)
    schema['x'] = Int(default=10)
    schema['sub'] = {'y': Int(default=20)}
    schema['sub']['sub'] = {'z': Int(default=30)}
    validation = schema.validate(config)
    assert config['x'] == 10
    assert config['sub']['y'] == 20
    assert config['sub']['sub']['z'] == 30
    assert not validation
    config.dump(string_io)
    if use_defaults:
        assert string_io.getvalue() == """\
[sub]
    [sub]
"""
    else:
        assert string_io.getvalue() == """\
def test_Config_use_defaults_False(string_io, use_defaults):
    config = Config()
    schema = Schema(use_defaults=use_defaults)
    schema['x'] = Int(default=10)
    schema['sub'] = {'y': Int(default=20)}
    schema['sub']['sub'] = {'z': Int(default=30)}
    validation = schema.validate(config)
    assert config['x'] == 10
    assert config['sub']['y'] == 20
    assert config['sub']['sub']['z'] == 30
    assert not validation
    config.dump(string_io)
    if use_defaults:
        assert string_io.getvalue() == """\
[sub]
    [sub]
"""
    else:
        assert string_io.getvalue() == """\
Beispiel #51
0
def test_Config_create_dictionary_init_overlap(string_io, defaultsvalue):
    dictionary = collections.OrderedDict()
    dictionary['x'] = 10
    dictionary['y'] = 10
    init = {'a': 20, 'y': 30}
    config = Config(init, dictionary=dictionary, defaults=defaultsvalue)
    assert len(config) == 3
    assert config['x'] == 10
    assert config['y'] == 30
    assert config['a'] == 20
Beispiel #52
0
def simple_validation(simple_schema_content, simple_section_content):
    schema = Schema(simple_schema_content)
    simple_section_content['a'] = -10
    simple_section_content['sub']['sa'] = 100.3
    simple_section_content['sub']['sb'] = "abc"
    simple_section_content['sub']['sc'] = True
    simple_section_content['sub']['subsub']['ssx'] = "omega"
    simple_section_content['sub']['subsub']['ssy'] = []
    config = Config(simple_section_content)
    return schema.validate(config)
Beispiel #53
0
def test_DefaultsSection0(defaults_section):
    config = Config()
    config['n'] = 4
    with defaults_section.referencing(config):
        defaults_section.dump()
        assert defaults_section['x'] == 14
        with pytest.raises(KeyError) as exc_info:
            assert defaults_section['sub']['x'] == 10008
        assert str(exc_info.value) == "'sub'"
        assert defaults_section['sub']['sub']['x'] == 20
Beispiel #54
0
def test_Config_defaults_add_overlap(defaultsvalue):
    config = Config(defaults=defaultsvalue)
    config['a'] = 100
    config['b'] = 101
    config['sub'] = {}
    config['sub']['x'] = 10
    config['sub']['y'] = 11
    config.set_defaults(b=201, c=202, sub={'y': 21, 'z': 22})
    assert 'a' in config
    assert config['a'] == 100
    assert 'b' in config
    assert config['b'] == 101
    assert 'c' in config
    assert config['c'] == 202
    assert 'x' in config['sub']
    assert config['sub']['x'] == 10
    assert 'y' in config['sub']
    assert config['sub']['y'] == 11
    assert 'z' in config['sub']
    assert config['sub']['z'] == 22
Beispiel #55
0
def test_Config_err_disabled_macros():
    config = Config(macros=False)
    config['x'] = 10
    config['sub'] = {'sub': {}}
    with pytest.raises(ValueError) as exc_info:
        config['y'] = ROOT['x'] + 1
    assert str(
        exc_info.value) == "cannot set y=ROOT['x'] + 1: macros are not enabled"
    with pytest.raises(ValueError) as exc_info:
        config['sub']['sub']['y'] = ROOT['x'] + 1
    assert str(
        exc_info.value) == "cannot set y=ROOT['x'] + 1: macros are not enabled"
    config.macros = True
    config['sub']['sub']['y'] = ROOT['x'] + 1
    assert config['sub']['sub']['y'] == 11
    config.macros = False
    with pytest.raises(ValueError) as exc_info:
        c = config['sub']['sub']['y']
    assert str(exc_info.value
               ) == "cannot evaluate ROOT['x'] + 1: macros are not enabled"
Beispiel #56
0
def test_set_key(string_io):
    config = Config()
    with pytest.raises(KeyError) as exc_info:
        set_key(config, (), 3)
    assert str(exc_info.value) == "()"

    with pytest.raises(KeyError) as exc_info:
        set_key(config, "", 3)
    assert str(exc_info.value) == "''"

    set_key(config, "x", 10)
    set_key(config, "sub", {'y': 20})
    set_key(config, "sub.z", 30)
    with pytest.raises(KeyError) as exc_info:
        set_key(config, "sub.sub2.w", 40)
    assert str(exc_info.value) == "'sub2'"
    set_key(config, "sub.sub2.w", 40, parents=True)

    config.dump(string_io)
    assert string_io.getvalue() == """\
Beispiel #57
0
def test_Config_defaults_deloptions():
    config = Config(defaults=True)
    config['d'] = 11
    config['e'] = 12
    config.set_defaults(a={}, b=5, c={'x': 7}, d=8)
    assert config.has_option('d')
    assert config['d'] == 11
    del config['d']
    assert config.has_option('d')
    assert config['d'] == 8
    assert config.has_option('e')
    assert config['e'] == 12
    del config['e']
    assert not config.has_option('e')
    with pytest.raises(KeyError):
        del config['e']
    del config['d']
    assert config.has_option('d')
    del config['c']
    assert config.has_section('c')
    assert config['c']['x'] == 7
def test_Config_err_disabled_macros_on_validation():
    from zirkon.config import ROOT
    config = Config(macros=False)
    schema = Schema()
    schema['x'] = Int()
    schema['y'] = Int(min=ROOT['x'] + 2)
    config['x'] = 3
    config['y'] = 5
    with pytest.raises(ValueError) as exc_info:
        validation = schema.validate(config)
    assert str(exc_info.value
               ) == "cannot evaluate ROOT['x'] + 2: macros are not enabled"
Beispiel #59
0
def test_del_key(string_io):
    config = Config()
    config['x'] = 10
    config['sub'] = {'y': 20}
    config['sub']['sub1'] = {'z': 30, 'w': 40}
    config['sub']['sub2'] = {'z': 30, 'w': 40}

    del_key(config, "x")
    with pytest.raises(KeyError) as exc_info:
        del_key(config, "x")
    assert str(exc_info.value) == "'x'"
    del_key(config, "x", ignore_errors=True)

    with pytest.raises(KeyError) as exc_info:
        del_key(config, "sub.sub3.xx")
    assert str(exc_info.value) == "'sub3'"
    
    del_key(config, "sub.sub1")
    del_key(config, "sub.sub2.z")
    config.dump(string_io)
    assert string_io.getvalue() == """\