def test_program_config_unique_module_config():
    pack2 = __import__("pack2")
    reload(pack2)
    program_config = parse_modules(pack2.mod1, fqname="packX", override_function=override_merge)
    schema = program_config.schema
    assert 'opt10' in schema['packX']['mod1']
    assert 'opt11' in schema['packX']['mod1']
def test_module_config_local_config_None(pack1_config0, merge):
    pack1 = __import__("pack1")
    reload(pack1)
    program_config = ProgramConfig()
    program_config.parse(pack1)
    assert pack1.get_alpha() == True
    assert pack1.mod0.get_opt1() == 10
    program_config.configure(pack1_config0)
    assert pack1.get_alpha() == False
    assert pack1.mod0.get_opt0() == "xyz"
    assert pack1.mod0.get_opt1() == 666
    with pack1.mod0.CONF.local_config(merge=merge) as (local_config, validation_result):
        assert isinstance(local_config, ConfigSection)
        assert isinstance(validation_result, ValidationSection)
        assert not validation_result
        if merge:
            opt0_value = "xyz"
            opt1_value = 666
        else:
            opt0_value = "abracadabra"
            opt1_value = 10
        assert local_config['opt0'] == opt0_value
        assert local_config['opt1'] == opt1_value
        assert pack1.mod0.get_opt0() == opt0_value
        assert pack1.mod0.get_opt1() == opt1_value
    assert pack1.get_alpha() == False
    assert pack1.mod0.get_opt0() == "xyz"
    assert pack1.mod0.get_opt1() == 666
def test_program_config_local_config_raise(pack1_config0, merge, raise_on_error):
    pack1 = __import__("pack1")
    reload(pack1)
    program_config = ProgramConfig()
    program_config.parse(pack1)
    assert pack1.get_beta() == 5
    assert pack1.mod0.get_opt1() == 10
    program_config.configure(pack1_config0)
    assert pack1.get_beta() == 100
    assert pack1.mod0.get_opt1() == 666
    pack1_config1 = pack1_config0.copy()
    pack1_config1['pack1']['mod0']['opt0'] = 123
    if raise_on_error:
        with pytest.raises(InvalidTypeError) as exc_info:
            with program_config.local_config(pack1_config1, merge=merge, raise_on_error=raise_on_error) as (local_config, validation_result):
                pass
    else:
        with program_config.local_config(pack1_config1, merge=merge, raise_on_error=raise_on_error) as (local_config, validation_result):
            assert isinstance(local_config, Config)
            assert isinstance(validation_result, Validation)
            assert validation_result
            assert 'pack1' in validation_result
            assert 'mod0' in validation_result['pack1']
            assert 'opt0' in validation_result['pack1']['mod0']

    assert pack1.get_beta() == 100
    assert pack1.mod0.get_opt1() == 666
def test_program_config_existing_parser(tmpdir, mod_config0, mod_config1):
    pack0 = __import__("pack0")
    reload(pack0)
    assert not pack0.CONF.configured()
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--max", "-m",
        type=int,
        default=12,
        help="max value")
    program_config = ProgramConfig(pack0.comp1.mod0, pack0.comp1.mod1)
    tmpfile0 = tmpdir.join("pexparser_0.zirkon").strpath
    mod_config0.write(tmpfile0, fmt="zirkon")
    tmpfile1 = tmpdir.join("pexparser_1.zirkon").strpath
    mod_config1.write(tmpfile1, fmt="zirkon")
    config, validation_result, namespace = program_config.parse_args(
        args=["--config", tmpfile1, "--config", tmpfile0, "--mod0-opt0=wow", "--mod1-opt0", "9.97", "--max=18"],
        argparser=parser)

    print("--- config:")
    config.dump()
    print("--- validation_result:")
    validation_result.dump()
    print("--- namespace:", namespace)

    assert namespace_len(namespace) == 1
    assert namespace.max == 18
    assert pack0.comp1.mod0.get_opt0() == "wow"
    assert pack0.comp1.mod0.get_opt1() == 80
    assert pack0.comp1.mod1.get_opt0() == 9.97
def test_program_config_package_pack0_fqname0(pack0_config0, pack0_fqname0):
    pack0 = __import__("pack0")
    reload(pack0)
    program_config = ProgramConfig()
    print("comp0:", pack0_fqname0['comp0'])
    print("comp1:", pack0_fqname0['comp1'])
    program_config.parse(pack0.comp0, fqname=pack0_fqname0['comp0'])
    program_config.parse(pack0.comp1, fqname=pack0_fqname0['comp1'])
    print("schema:")
    program_config.schema.dump()
    print("config:")
    pack0_config0.dump()
    program_config.configure(pack0_config0)
    
    assert not pack0.CONF.configured()
    assert pack0.comp0.CONF.configured()
    assert pack0.comp1.CONF.configured()
    assert not pack0.comp2.mod0.CONF.configured()
    assert pack0.comp0.get_opt0() == 20
    assert pack0.comp0.get_opt1() == 30
    assert pack0.comp0.get_opt2() == False
    assert pack0.comp1.get_opt0() == 555555
    assert pack0.comp1.get_opt2() == True
    assert pack0.comp1.mod0.get_opt0() == "abc"
    assert pack0.comp1.mod1.get_opt0() == 2003.5
    assert pack0.comp1.mod2.get_opt0() == 3003.5
def test_module_config_unregister():
    pack5 = __import__("pack5")
    reload(pack5)
    program_config = ProgramConfig()
    program_config.parse(pack5.comp_unregister)
    schema = program_config.schema
    assert not 'comp_unregister' in schema
    assert not schema
def test_program_config_root():
    pack10_comp1 = __import__("pack10.comp1", fromlist=('pack10', ))
    reload(pack10_comp1)
    print(pack10_comp1)
    program_config = parse_modules(pack10_comp1)
    assert 'comp1' in program_config.schema
    assert 'comp0' not in program_config.schema
    program_config.schema.dump()
def test_module_config_relocate_itself():
    pack5 = __import__("pack5")
    reload(pack5)
    program_config = ProgramConfig()
    program_config.parse(pack5.comp_relocate_itself)
    schema = program_config.schema
    assert 'comp_relocate_itself' in schema
    assert 'opt0' in schema['comp_relocate_itself']
def test_module_config_relocate_outside():
    pack5 = __import__("pack5")
    reload(pack5)
    program_config = ProgramConfig()
    program_config.parse(pack5.comp_relocate_outside)
    schema = program_config.schema
    assert not 'comp_relocate_outside' in schema
    assert not schema
def test_program_config_PATH_noconfig():
    pack9 = __import__("pack9")
    reload(pack9)
    program_config = parse_modules(pack9)
    dirname = os.getcwd()
    basename = ""
    assert pack9.get_url() == "sqlite://{}/.{}.db".format(dirname, basename)
    assert pack9.comp0.get_fn0() == "{}/fn0.config".format(dirname)
    assert pack9.comp0.get_fn1() == "_fn1.config"
def test_program_config_duplication_merge():
    pack2 = __import__("pack2")
    reload(pack2)
    program_config = parse_modules(pack2.mod1, override_function=override_merge)
    assert pack2.mod1.CONF_A.schema == pack2.mod1.CONF_B.schema
    assert 'opt10' in pack2.mod1.CONF_A.schema
    assert 'opt11' in pack2.mod1.CONF_A.schema
    assert pack2.mod1.get_opt10() == 3
    assert pack2.mod1.get_opt11() == 1.5
def test_program_config_package_isolation_3(pack1_config0):
    pack1 = __import__("pack1")
    reload(pack1)
    assert not pack1.mod0.CONF.configured()
    assert not pack1.mod1.CONF.configured()
    program_config = ProgramConfig(pack1)
    program_config.configure(pack1_config0)
    assert pack1.CONF.configured()
    assert pack1.mod0.CONF.configured()
    assert pack1.mod1.CONF.configured()
def test_program_config_package_depth(depth):
    pack0 = __import__("pack0")
    reload(pack0)
    assert not pack0.CONF.configured()
    program_config = ProgramConfig()
    program_config.parse(pack0, depth=depth)
    print("depth:", depth)
    print("schema:")
    program_config.schema.dump()
    assert maxdepth(program_config.schema) == depth
def test_module_config_noconfigure():
    pack0 = __import__("pack0")
    reload(pack0)
    assert not pack0.CONF.configured()
    assert not pack0.comp1.mod0.CONF.configured()
    assert pack0.comp1.mod0.get_opt0() == "abracadabra"
    assert not pack0.CONF.configured()
    assert pack0.comp1.mod0.CONF.configured()
    assert pack0.comp1.mod0.get_opt1() == 10
    assert pack0.comp1.mod1.get_opt0() == 3.5
def test_program_config_package_CONF(pack0_config0):
    pack0 = __import__("pack0")
    reload(pack0)
    assert not pack0.CONF.configured()
    program_config = ProgramConfig(pack0)
    program_config.configure(pack0_config0)
    conf = pack0.CONF
    for key, val in conf.items():
        print(key, val)
    print(len(conf))
    conf.config.dump()
def test_program_config_manual_configure_default():
    pack0 = __import__("pack0")
    reload(pack0)
    assert not pack0.CONF.configured()
    program_config = ProgramConfig(pack0.comp1.mod0, pack0.comp1.mod1)
    # print("=== schema:")
    # program_config.schema.dump()
    program_config.configure()
    assert pack0.comp1.mod0.get_opt0() == "abracadabra"
    assert pack0.comp1.mod0.get_opt1() == 10
    assert pack0.comp1.mod1.get_opt0() == 3.5
def test_program_config_module_isolation():
    pack4 = __import__("pack4")
    reload(pack4)
    program_config = parse_modules(pack4)
    schema = program_config.schema
    schema.dump()
    assert 'pack4' in schema
    assert 'comp1' in schema["pack4"]
    assert 'opt10' in schema["pack4"]["comp1"]
    assert 'comp2' in schema["pack4"]
    assert 'opt20' in schema["pack4"]["comp2"]
def test_module_config_relocate_inside():
    pack5 = __import__("pack5")
    reload(pack5)
    program_config = ProgramConfig()
    program_config.parse(pack5)
    schema = program_config.schema
    print("=== schema:")
    schema.dump()
    assert 'pack5' in schema
    assert 'comp_relocate_outside' in schema['pack5']
    assert 'optX' in schema['pack5']['comp_relocate_outside']
def test_program_config_load_config_noconfigure():
    pack1 = __import__("pack1")
    reload(pack1)
    program_config = parse_modules(pack1)
    config = program_config.config
    config.dump()
    config.defaults.dump()
    assert config["pack1"]["alpha"] == True
    assert config["pack1"]["beta"] == 5
    assert config["pack1"]["mod0"]["opt0"] == "abracadabra"
    assert config["pack1"]["mod0"]["opt1"] == 10
    assert config["pack1"]["mod1"]["opt0"] == 3.5
def test_module_config_relocate():
    pack5 = __import__("pack5")
    reload(pack5)
    program_config = ProgramConfig()
    program_config.parse(pack5.comp_relocate)
    assert pack5.comp_relocate.get_opt0() == 10
    assert pack5.comp_relocate.get_opt1() == 1.0
    schema = program_config.schema
    schema.dump()
    assert 'comp_relocate' in schema
    assert 'opt0' in schema['comp_relocate']
    assert 'opt1' in schema['comp_relocate']
def test_program_config_package_isolation_2(pack1_config0):
    pack1 = __import__("pack1")
    reload(pack1)
    assert not pack1.mod0.CONF.configured()
    assert not pack1.mod1.CONF.configured()
    program_config = ProgramConfig(pack1.mod0, pack1.mod1)
    config = pack1_config0["pack1"]["mod0"].copy()
    config.update(pack1_config0["pack1"]["mod1"])
    config.dump()
    program_config.configure(config)
    assert not pack1.CONF.configured()
    assert pack1.mod0.CONF.configured()
    assert pack1.mod1.CONF.configured()
def test_program_config_default_option_ignored(tmpdir, mod_config0):
    pack0 = __import__("pack0")
    reload(pack0)
    assert not pack0.CONF.configured()
    program_config = ProgramConfig(pack0.comp1.mod0, pack0.comp1.mod1, default_option=Any())
    tmpfile0 = tmpdir.join("pverr_0.zirkon").strpath
    mod_config0 = mod_config0.copy()
    mod_config0["unexpected_opt"] = 100
    mod_config0.write(tmpfile0, fmt="zirkon")
    program_config.parse_args(["--config", tmpfile0], raise_on_error=True)
    assert pack0.comp1.mod0.get_opt0() == "abc"
    assert pack0.comp1.mod0.get_opt1() == 10
    assert pack0.comp1.mod1.get_opt0() == 5.2
def test_program_config_manual_configure(mod_config0):
    pack0 = __import__("pack0")
    reload(pack0)
    assert not pack0.CONF.configured()
    program_config = ProgramConfig(pack0.comp1.mod0, pack0.comp1.mod1)
    print("=== schema:")
    program_config.schema.dump()
    print("=== config:")
    mod_config0.dump()
    program_config.configure(mod_config0)
    assert pack0.comp1.mod0.get_opt0() == "abc"
    assert pack0.comp1.mod0.get_opt1() == 10
    assert pack0.comp1.mod1.get_opt0() == 5.2
def test_program_config_PATH_config_argparser(tmpdir):
    pack9 = __import__("pack9")
    reload(pack9)
    program_config = parse_modules(pack9)
    filename = tmpdir.join("PATH.config").strpath
    with open(filename, "w") as f_out:
        f_out.write("""\
""")
    config, validation_result, namespace = program_config.parse_args(["--config", filename])
    dirname, basename = os.path.split(filename)
    assert pack9.get_url() == "sqlite://{}/.{}.db".format(dirname, basename)
    assert pack9.comp0.get_fn0() == "{}/fn0.config".format(dirname)
    assert pack9.comp0.get_fn1() == "{}_fn1.config".format(filename)
def test_program_config_unique_module_config():
    pack2 = __import__("pack2")
    reload(pack2)
    program_config = parse_modules(pack2, override_function=override_merge)
    program_config.schema.dump()
    schema = program_config.schema
    assert 'opt0' in schema['pack2']
    assert 'opt00' in schema['pack2']['mod0']
    assert 'opt00' not in schema['pack2']
    assert 'opt10' in schema['pack2']['mod1']
    assert 'opt10' not in schema['pack2']
    assert 'opt11' in schema['pack2']['mod1']
    assert 'opt11' not in schema['pack2']
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
def test_program_config_load_schema():
    pack1 = __import__("pack1")
    reload(pack1)
    program_config = parse_modules(pack1)
    schema = program_config.schema
    schema.dump()
    assert 'pack1' in schema
    assert 'alpha' in schema["pack1"]
    assert 'beta' in schema["pack1"]
    assert 'mod0' in schema["pack1"]
    assert 'opt0' in schema["pack1"]["mod0"]
    assert 'opt1' in schema["pack1"]["mod0"]
    assert 'mod1' in schema["pack1"]
    assert 'opt0' in schema["pack1"]["mod1"]
def test_module_config_relocate_above():
    pack5 = __import__("pack5")
    reload(pack5)
    reload(pack5.comp_relocate_above.api)  # force reloading as last
    program_config = ProgramConfig()
    program_config.parse(pack5.comp_relocate_above, pack5.comp_relocate_above.api)
    assert pack5.comp_relocate.get_opt0() == 10
    assert pack5.comp_relocate.get_opt1() == 1.0
    schema = program_config.schema
    schema.dump()
    assert 'comp_relocate_above' in schema
    assert 'api' in schema['comp_relocate_above']
    assert 'opt0' in schema['comp_relocate_above']['api']
    assert 'opt1' in schema['comp_relocate_above']['api']
def test_module_config_nested():
    pack6 = __import__("pack6")
    reload(pack6)
    program_config = ProgramConfig()
    program_config.parse(pack6)
    schema = program_config.schema
    assert 'pack6' in schema
    assert 'alpha' in schema['pack6']
    assert 'mod1' in schema['pack6']
    assert 'beta' in schema['pack6']['mod1']
    assert 'opt10' in schema['pack6']['mod1']
    assert 'opt11' in schema['pack6']['mod1']
    assert isinstance(schema['pack6']['mod1']['opt11'], Bool)
    assert 'mod2' in schema['pack6']
    assert 'opt20' in schema['pack6']['mod2']
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