コード例 #1
0
ファイル: test_tool.py プロジェクト: simone-campagna/zirkon
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() == ""
コード例 #2
0
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
コード例 #3
0
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)
コード例 #4
0
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
コード例 #5
0
def mod_config0():
    return Config.from_string("""\
[mod0]
    opt0 = "abc"
[mod1]
    opt0 = 5.2
""", fmt="zirkon")
コード例 #6
0
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
コード例 #7
0
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
コード例 #8
0
ファイル: test_schema.py プロジェクト: simone-campagna/zirkon
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
コード例 #9
0
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"
コード例 #10
0
def test_Schema_PATH_list(tmpdir, defaults):
    schema = Schema.from_string("""\
dirs = StrList(default=[PATH.cwd + PATH.sep + "xdir"])
files = StrList(default=[PATH.cwd + PATH.sep + "x.txt"])
""")
    config = Config.from_string("""\
dirs = ["xdir", PATH.cwd + PATH.sep + "ydir"]
""", schema=schema)
    assert config["dirs"] == ["xdir", os.path.abspath("ydir")]
    assert config["files"] == [os.path.abspath("x.txt")]
コード例 #11
0
def test_Schema_PATH_defaults_no_filename(macro_schema_PATH):
    dirname = os.getcwd()
    filename = ""
    print([dirname, filename])
    config = Config.from_string("""\
""", schema=macro_schema_PATH)
    assert config["a"] == os.path.join(dirname, "a_")
    assert config["b"] == filename + "b"
    assert config["sub"]["c"] == os.path.join(dirname, "c_")
    assert config["sub"]["d"] == filename + "d"
コード例 #12
0
ファイル: test_tool.py プロジェクト: simone-campagna/zirkon
def test_main_read_write_evaluate(tmpdir, evaluate, defaults):
    fn_cfg1 = tmpdir.join("evaluate_cfg1.config").strpath
    fn_sch1 = tmpdir.join("evaluate_sch1.schema").strpath

    cfg1 = Config.from_string("""\
a1 = 10
[b]
    b1 = ROOT['a1'] + 1
    b2 = PATH.filename + PATH.extsep + 'b2'
""", defaults=defaults)
    
    sch1 = Schema.from_string("""\
a1 = Int()
a2 = Int(default=SECTION['a1'] * 2)
[b]
    b1 = Int()
    b2 = Str()
    b3 = Str(default=PATH.dirname + PATH.sep + "b3")
""")

    cfg1.to_file(fn_cfg1)
    sch1.to_file(fn_sch1)

    args = ["read", "-i", fn_cfg1, "-s", fn_sch1, "--defaults"]
    if evaluate:
        args.append("-e")

    log_stream, out_stream = run(args)
    if evaluate:
        reference = """\
a1 = 10
[b]
    b1 = 11
    b2 = {b2!r}
    b3 = {b3!r}
a2 = 20

""".format(b2=fn_cfg1 + os.path.extsep + 'b2', b3=os.path.dirname(fn_cfg1) + os.path.sep + 'b3')
    else:
        reference = """\
a1 = 10
[b]
    b1 = ROOT['a1'] + 1
    b2 = PATH.filename + PATH.extsep + 'b2'
    b3 = PATH.dirname + PATH.sep + 'b3'
a2 = SECTION['a1'] * 2

"""
    print("=== evaluate={}".format(evaluate))
    print("=== reference:")
    print(reference)
    print("=== result:")
    print(out_stream.getvalue())
    assert out_stream.getvalue() == reference
    assert log_stream.getvalue() == ""
コード例 #13
0
def pack1_config0():
    return Config.from_string("""\
[pack1]
    alpha = False
    beta = 100
    [mod0]
        opt0 = "xyz"
        opt1 = 666
    [mod1]
        opt0 = 6.66
""", fmt="zirkon")
コード例 #14
0
ファイル: test_schema.py プロジェクト: simone-campagna/zirkon
def test_Schema_validation_order():
    schema = Schema.from_string("""\
__default_option__ = Str()
homedir = Str(default="/home/user")
""")
    config = Config.from_string("""\
workdir = SECTION["homedir"] + "/work"
""")

    validation = schema.validate(config)
    assert not validation
    assert config["homedir"] == "/home/user"
    assert config["workdir"] == "/home/user/work"
コード例 #15
0
def test_program_config_local_config(pack1_config0, merge):
    pack1 = __import__("pack1")
    reload(pack1)
    program_config = ProgramConfig()
    program_config.parse(pack1)
    assert pack1.get_beta() == 5
    assert pack1.mod0.get_opt0() == "abracadabra"
    assert pack1.mod0.get_opt1() == 10
    assert pack1.mod1.get_opt0() == 3.5
    program_config.configure(pack1_config0)
    pack1_config1 = pack1_config0.copy()
    assert pack1.get_beta() == 100
    assert pack1.mod0.get_opt0() == "xyz"
    assert pack1.mod0.get_opt1() == 666
    assert pack1.mod1.get_opt0() == 6.66
    # [pack1]
    #     alpha = False
    #     beta = 100
    #     [mod0]
    #         opt0 = "xyz"
    #         opt1 = 666
    #     [mod1]
    #         opt0 = 6.66
    pack1_config1 = Config.from_string("""\
[pack1]
    [mod0]
        opt0 = "1xyz"
        opt1 = 1666
    [mod1]
        opt0 = 16.66
""", fmt="zirkon")

    with program_config.local_config(pack1_config1, merge=merge) as (local_config, validation_result):
        assert isinstance(local_config, Config)
        assert isinstance(validation_result, Validation)
        assert not validation_result
        print("=== local_config:")
        local_config.dump()
        if merge:
            beta_value = 100
        else:
            beta_value = 5
        assert pack1.get_beta() == beta_value
        assert pack1.mod0.get_opt0() == "1xyz"
        assert pack1.mod0.get_opt1() == 1666
        assert pack1.mod1.get_opt0() == 16.66

    assert pack1.get_beta() == 100
    assert pack1.mod0.get_opt0() == "xyz"
    assert pack1.mod0.get_opt1() == 666
    assert pack1.mod1.get_opt0() == 6.66
コード例 #16
0
def test_Schema_PATH_overridden(macro_schema_PATH):
    dirname = "/home/user"
    filename = os.path.join(dirname, "PATH.config")
    config = Config.from_string("""\
a = PATH.dirname + PATH.sep + "A"
b = PATH.filename + ".B"
[sub]
    c = PATH.dirname + PATH.sep + "C"
    d = PATH.filename + ".D"
""", filename=filename, schema=macro_schema_PATH)
    assert config["a"] == os.path.join(dirname, "A")
    assert config["b"] == filename + ".B"
    assert config["sub"]["c"] == os.path.join(dirname, "C")
    assert config["sub"]["d"] == filename + ".D"
コード例 #17
0
def test_Schema_PATH_overridden_no_filename(macro_schema_PATH):
    dirname = os.getcwd()
    filename = ""
    config = Config.from_string("""\
a = PATH.dirname + PATH.sep + "A"
b = PATH.filename + ".B"
[sub]
    c = PATH.dirname + PATH.sep + "C"
    d = PATH.filename + ".D"
""", schema=macro_schema_PATH)
    assert config["a"] == os.path.join(dirname, "A")
    assert config["b"] == filename + ".B"
    assert config["sub"]["c"] == os.path.join(dirname, "C")
    assert config["sub"]["d"] == filename + ".D"
コード例 #18
0
ファイル: test_config.py プロジェクト: simone-campagna/zirkon
def test_Config_SECTION_parent():
    config = Config.from_string("""\
[l0]
    l0_0 = 10
    l0_1 = 20
    [l1]
        l1_0 = 5
        l1_1 = SECTION["l1_0"] + SECTION.parent["l0_0"]
        [l2]
            l2_0 = SECTION.parent["l1_1"] * SECTION.parent.parent["l0_1"]
""")
    assert config["l0"]["l0_0"] == 10
    assert config["l0"]["l0_1"] == 20
    assert config["l0"]["l1"]["l1_0"] == 5
    assert config["l0"]["l1"]["l1_1"] == 15
    assert config["l0"]["l1"]["l2"]["l2_0"] == 15 * 20
コード例 #19
0
def test_program_config_load_config_reload():
    pack1 = __import__("pack1")
    reload(pack1)
    program_config = parse_modules(pack1)
    cfg = Config.from_string("""\
[pack1]
    [mod0]
        opt1 = 20
    beta = 55
""")
    program_config.configure(cfg)
    config = program_config.config
    program_config2 = parse_modules(pack1)
    config2 = program_config2.config
    assert config is not config2
    assert config == config2
コード例 #20
0
def test_program_config_load_config_configure():
    pack1 = __import__("pack1")
    reload(pack1)
    program_config = parse_modules(pack1)
    cfg = Config.from_string("""\
[pack1]
    [mod0]
        opt1 = 20
    beta = 55
""")
    program_config.configure(cfg)
    config = program_config.config
    config.dump()
    assert config["pack1"]["alpha"] == True
    assert config["pack1"]["beta"] == 55
    assert config["pack1"]["mod0"]["opt0"] == "abracadabra"
    assert config["pack1"]["mod0"]["opt1"] == 20
コード例 #21
0
ファイル: test_schema.py プロジェクト: simone-campagna/zirkon
def test_Schema_default_option():
    schema = Schema.from_string("""\
[filesystems]
    __default_option__ = Str()
""")
    config = Config.from_string("""\
[filesystems]
    home = "/home/user"
    work = "/tmp/user"
    data = 6.5
""")
    validation = schema.validate(config)
    assert validation
    assert len(validation) == 1
    assert 'filesystems' in validation
    assert len(validation['filesystems']) == 1
    assert 'data' in validation['filesystems']
    assert config['filesystems']['home'] == "/home/user"
    assert config['filesystems']['work'] == "/tmp/user"
コード例 #22
0
ファイル: test_schema.py プロジェクト: simone-campagna/zirkon
def test_Schema_default_section():
    schema = Schema.from_string("""\
[filesystems]
    [__default_section__]
        path = Str()
        max_size_gb = Float(default=1.0)
""")
    config = Config.from_string("""\
[filesystems]
    [home]
        path = "/home/user"
    [work]
        path = "/tmp/user"
        max_size_gb = 6.5
""")
    validation = schema.validate(config)
    assert not validation
    assert config['filesystems']['home']['max_size_gb'] == 1.0
    assert config['filesystems']['work']['max_size_gb'] == 6.5
コード例 #23
0
def test_module_config_nested_default():
    pack7 = __import__("pack7")
    reload(pack7)
    program_config = ProgramConfig()
    program_config.parse(pack7)
    schema = program_config.schema
    conf = pack7.CONF
    assert len(conf['filesystems']) == 0

    config = Config.from_string("""\
[pack7]
    [filesystems]
        [home]
            path = "/home/user"
            max_size_gb = 1.2
            [files]
                source = "x.py"
                data = "x.dat"
        [work]
            path = "/tmp/user"
        [input]
            path = "/db/user"
            [files]
                input1 = "x1.dat"
                input2 = "x2.dat"
                input3 = "x3.dat"
""")
    program_config.configure(config)
    # conf.config.dump()
    assert 'home' in conf['filesystems']
    assert conf['filesystems']['home']['max_size_gb'] == 1.2
    assert 'source' in conf['filesystems']['home']['files']
    assert 'data' in conf['filesystems']['home']['files']
    assert 'work' in conf['filesystems']
    assert conf['filesystems']['work']['max_size_gb'] == 1.0
    assert len(conf['filesystems']['work']['files']) == 0
    assert 'input' in conf['filesystems']
    assert 'input1' in conf['filesystems']['input']['files']
    assert 'input2' in conf['filesystems']['input']['files']
    assert 'input3' in conf['filesystems']['input']['files']
コード例 #24
0
def test_program_config_callback():
    pack8 = __import__("pack8")
    reload(pack8)
    program_config = parse_modules(pack8)
    assert pack8.get_url() == "sqlite://"
    assert pack8.get_db() is not None
    assert pack8.get_db().url == pack8.get_url()
    assert pack8.comp0.get_size() == 0
    assert pack8.comp0.get_lst() is not None
    assert len(pack8.comp0.get_lst()) == pack8.comp0.get_size()
    cfg = Config.from_string("""\
[pack8]
    url = "memory://"
    [comp0]
        size = 10
""")
    program_config.configure(cfg)
    assert pack8.get_url() == "memory://"
    assert pack8.get_db() is not None
    assert pack8.get_db().url == pack8.get_url()
    assert pack8.comp0.get_size() == 10
    assert pack8.comp0.get_lst() is not None
    assert len(pack8.comp0.get_lst()) == pack8.comp0.get_size()
コード例 #25
0
def pack0_config0():
    return Config.from_string("""\
[pack0]
    [comp0]
        opt2 = False
        opt0 = 20
        opt1 = 30
    [comp1]
        [mod0]
            opt0 = 'abc'
        [mod1]
            opt0 = 2003.5
        [mod2]
            opt0 = 3003.5
        opt2 = True
        opt0 = 555555
    [comp2]
        [mod0]
            opt0 = 4003.5
    url = 'sqlite://x.y.z'
    max_workers = 7
    max_delay = 9.78
""", fmt="zirkon")
コード例 #26
0
def test_read_write_config_de(config_fmt):
    config, fmt = config_fmt
    assert config['sub']['z'] == 105
    config_string = config.to_string(fmt=fmt)
    config_reloaded = Config.from_string(config_string, fmt=fmt)
    assert config_reloaded == config
コード例 #27
0
def test_read_write_config_de(config_protocol):
    config, protocol = config_protocol
    assert config['sub']['z'] == 105
    config_string = config.to_string(protocol=protocol)
    config_reloaded = Config.from_string(config_string, protocol=protocol)
    assert config_reloaded == config
コード例 #28
0
ファイル: test_config.py プロジェクト: nazavode/zirkon
def test_Config_macro_protocol(defconfig, protocol):
    s_defconfig = defconfig.to_string(protocol=protocol)
    defconfig_reloaded = Config.from_string(s_defconfig, protocol=protocol)
    assert defconfig_reloaded == defconfig
コード例 #29
0
def test_Config_set_zirkon_times(ztinstance):
    config = Config()
    config['x'] = ztinstance
    assert config['x'] == ztinstance
    print(config.to_string())
    assert config.to_string() == Config.from_string(config.to_string()).to_string()
コード例 #30
0
def test_Config_times_serialization(source):
    config = Config.from_string(source)
    assert source == config.to_string().rstrip('\n')
    config.as_dict(evaluate=True)  # check evaluation
コード例 #31
0
def test_Config_init_error():
    source = "x = TimeDelta('xyz')"
    with pytest.raises(ValueError):
        config = Config.from_string(source)
コード例 #32
0
ファイル: test_tool.py プロジェクト: nazavode/zirkon
def test_main_read_write(files, i_name_protocol, o_name_protocol, s_name_protocol, v_name_protocol, defaults):
    i_name, i_protocol, i_hints = i_name_protocol
    o_name, o_protocol, o_hints = o_name_protocol
    s_name, s_protocol, s_hints = s_name_protocol
    v_name, v_protocol, v_hints = v_name_protocol

    if o_protocol is None:
        o_protocol = i_protocol
    with files.tmp(o_name), files.tmp(v_name):
        i_file_arg = files[i_name] + i_hints
        o_file_arg = files[o_name] + o_hints

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

        args = ["read", "-i", i_file_arg, "-o", o_file_arg]
        if s_name is not None:
            args.extend(["-s", s_name + s_hints])
            if v_name is not None:
                args.extend(["-V", v_name + v_hints])
        if defaults is not None:
            args.append("--defaults={!r}".format(defaults))
        log_stream, out_stream = run(args)

        if o_name:
            assert os.path.exists(files[o_name])

        config_args = {}
        if defaults is not None:
            config_args['defaults'] = defaults

        print("read in {}:{}".format(files[i_name], i_protocol))
        i_config = Config.from_file(files[i_name], protocol=i_protocol, **config_args)
        print("read out {}:{}".format(files[o_name], o_protocol))

        schema = None
        if s_name is not None:
            schema = Schema.from_file(files[s_name], protocol=s_protocol)
            validation = schema.validate(i_config)
            if v_name is not None:
                assert os.path.exists(v_name)
                v_data = Validation.from_file(files[v_name], protocol=v_protocol)
                print("===")
                validation.dump()
                print("===")
                v_data.dump()
                assert validation.to_string(protocol="zirkon") == v_data.to_string(protocol="zirkon")

        if o_name:
            o_config = Config.from_file(files[o_name], protocol=o_protocol, **config_args)
        else:
            o_config = Config.from_string(out_stream.getvalue(), protocol=o_protocol, **config_args)
        if schema:
            o_v_data = schema.validate(o_config)
            
        assert i_config == o_config
        out_x_json_path = files[o_name]
    assert not o_name in files
    assert not os.path.exists(out_x_json_path)
コード例 #33
0
def test_Config_evaluation_error():
    source = "x = MTimeDelta('xyz')"
    config = Config.from_string(source)
    with pytest.raises(EvaluationError):
        config.as_dict(evaluate=True)