Exemplo n.º 1
0
def load_config():
    global BENTOML_HOME  # pylint: disable=global-statement

    try:
        Path(BENTOML_HOME).mkdir(exist_ok=True)
    except OSError as err:
        raise BentoMLConfigException(
            "Error creating bentoml home directory '{}': {}".format(
                BENTOML_HOME, err.strerror))

    with open(DEFAULT_CONFIG_FILE, "rb") as f:
        DEFAULT_CONFIG = f.read().decode(CONFIG_FILE_ENCODING)

    loaded_config = BentoMLConfigParser(
        default_config=parameterized_config(DEFAULT_CONFIG))

    local_config_file = get_local_config_file()
    if os.path.isfile(local_config_file):
        logger.info("Loading local BentoML config file: %s", local_config_file)
        with open(local_config_file, "rb") as f:
            loaded_config.read_string(
                parameterized_config(f.read().decode(CONFIG_FILE_ENCODING)))
    else:
        logger.info(
            "No local BentoML config file found, using default configurations")

    return loaded_config
Exemplo n.º 2
0
def test_conf_access_hierachy():
    test_default_config = b"""\
[test]
a = 125
b = true
c = false
d = 1.01
e = value

[test2]
foo = bar
    """.decode("utf-8")
    config = BentoMLConfigParser(default_config=test_default_config)

    assert config["test"].getint("a") == 123
    assert config["test"].getboolean("b")
    assert not config["test"].getboolean("c")
    assert config["test"].getfloat("d") == 1.01
    assert config["test"].get("e") == "value"
    assert config["test2"].get("foo") == "bar"

    config.read_string(b"""\
[test]
c = true
e = value ii
    """.decode("utf-8"))

    assert config["test"].getboolean("c")
    assert config["test"].get("e") == "value ii"

    with env_vars(
            BENTOML__TEST__C="false",
            BENTOML__TEST__E="value iii",
            BENTOML__TEST2__FOO="new bar",
    ):
        assert not config["test"].getboolean("c")
        assert config["test"].get("e") == "value iii"
        assert config["test2"].get("foo") == "new bar"

        config_copy = ConfigParser()
        config_copy.read_dict(config.as_dict())

        assert config_copy["test"].getint("a") == 123
        assert config_copy["test"].getboolean("b")
        assert not config_copy["test"].getboolean("c")
        assert config_copy["test"].getfloat("d") == 1.01
        assert config_copy["test"].get("e") == "value iii"
        assert config_copy["test2"].get("foo") == "new bar"