def test_get_dict(): # type: ignore cfg = ConfigurationSet(config_from_dict(DICT2_1), config_from_dict(DICT2_2), config_from_env(prefix=PREFIX)) assert cfg.get_dict("a2") == { 'b1.c1': 'f', 'b1.c2': False, 'b1.c3': None, 'b2.c1': 10, 'b2.c2': 'YWJjZGVmZ2g=', 'b2.c3': 'abcdefgh' } assert cfg.a2.as_dict() == { 'b1.c1': 'f', 'b1.c2': False, 'b1.c3': None, 'b2.c1': 10, 'b2.c2': 'YWJjZGVmZ2g=', 'b2.c3': 'abcdefgh' } assert dict(cfg.a2) == { 'b1.c1': 'f', 'b1.c2': False, 'b1.c3': None, 'b2.c1': 10, 'b2.c2': 'YWJjZGVmZ2g=', 'b2.c3': 'abcdefgh' } with raises(KeyError): assert cfg.get_dict("a3") is Exception assert set( cfg.a2.values()) == {'f', False, None, 10, 'YWJjZGVmZ2g=', 'abcdefgh'} assert dict(cfg.a2) == dict(cfg.a2.items())
def test_repr_and_str(): # type: ignore import sys path = os.path.join(os.path.dirname(__file__), "python_config.py") cfg = ConfigurationSet( config_from_dict(DICT2_1, lowercase_keys=True), config_from_dict(DICT2_2, lowercase_keys=True), config_from_env(prefix=PREFIX, lowercase_keys=True), config_from_python(path, prefix="CONFIG", lowercase_keys=True), ) joined_dicts = dict((k, str(v)) for k, v in DICT1.items()) joined_dicts.update(DICT2_1) joined_dicts.update(DICT2_2) joined_dicts["sys.version"] = sys.hexversion assert hex(id(cfg)) in repr(cfg) assert ( str(cfg) == "{'a1.b1.c1': '1', 'a1.b1.c2': '2', 'a1.b1.c3': '3', 'a1.b2.c1': 'a', 'a1.b2.c2': 'True', " "'a1.b2.c3': '1.1', 'a2.b1.c1': 'f', 'a2.b1.c2': False, 'a2.b1.c3': None, 'a2.b2.c1': 10, " "'a2.b2.c2': 'YWJjZGVmZ2g=', 'a2.b2.c3': 'abcdefgh', 'sys.version': " + str(sys.hexversion) + "}" )
def test_load_env(): # type: ignore os.environ.update((PREFIX + "__" + k.replace(".", "__").upper(), str(v)) for k, v in DICT.items()) cfg = config_from_env(PREFIX, lowercase_keys=True) assert cfg["a1.b1.c1"] == "1" assert cfg["a1.b1"].get_int("c1") == 1 assert cfg["a1.b1"].as_dict() == {"c1": "1", "c2": "2", "c3": "3"} assert cfg["a1.b2"].as_dict() == {"c1": "a", "c2": "True", "c3": "1.1"}
def test_get(): # type: ignore cfg = ConfigurationSet( config_from_dict(DICT2_1, lowercase_keys=True), config_from_dict(DICT2_2, lowercase_keys=True), config_from_env(prefix=PREFIX, lowercase_keys=True), ) assert cfg.get("a2.b2") == config_from_dict( {"c1": 10, "c2": "YWJjZGVmZ2g=", "c3": "abcdefgh"} ) assert cfg.get("a2.b5", "1") == "1"
def test_get(): # type: ignore cfg = ConfigurationSet(config_from_dict(DICT2_1), config_from_dict(DICT2_2), config_from_env(prefix=PREFIX)) assert cfg.get("a2.b2") == config_from_dict({ 'c1': 10, 'c2': 'YWJjZGVmZ2g=', 'c3': 'abcdefgh' }) assert cfg.get("a2.b5", "1") == "1"
def test_reload(): # type: ignore os.environ.update((PREFIX + "__" + k.replace(".", "__").upper(), str(v)) for k, v in DICT.items()) cfg = config_from_env(PREFIX, lowercase_keys=True) assert cfg == config_from_dict(dict((k, str(v)) for k, v in DICT.items())) os.environ[PREFIX + "__" + "A2__B2__C3"] = "updated" assert cfg == config_from_dict(dict((k, str(v)) for k, v in DICT.items())) cfg.reload() d = DICT.copy() d["a2.b2.c3"] = "updated" assert cfg == config_from_dict(dict((k, str(v)) for k, v in d.items()))
def test_repr(): # type: ignore import sys path = os.path.join(os.path.dirname(__file__), 'python_config.py') cfg = ConfigurationSet(config_from_dict(DICT2_1), config_from_dict(DICT2_2), config_from_env(prefix=PREFIX), config_from_python(path, prefix='CONFIG')) joined_dicts = dict((k, str(v)) for k, v in DICT1.items()) joined_dicts.update(DICT2_1) joined_dicts.update(DICT2_2) joined_dicts['sys.version'] = sys.hexversion assert str(dict( (k.lower(), v) for k, v in joined_dicts.items())) in repr(cfg)
def test_fails(): # type: ignore cfg = ConfigurationSet(config_from_dict(DICT2_1), config_from_dict(DICT2_2), config_from_env(prefix=PREFIX)) with raises(KeyError, message="a1.b2.c3.d4"): assert cfg["a1.b2.c3.d4"] is Exception with raises(KeyError, message="'c4'"): assert cfg.a1.b2.c4 is Exception with raises(ValueError, message="Expected a valid True or False expression."): assert cfg["a1.b2"].get_bool("c3") is Exception
def test_configs(): # type: ignore # readable configs cfg = ConfigurationSet( config_from_dict(DICT2_1, lowercase_keys=True), config_from_dict(DICT2_2, lowercase_keys=True), config_from_env(prefix=PREFIX, lowercase_keys=True), ) assert cfg.configs[0] == config_from_dict(DICT2_1, lowercase_keys=True) cfg.configs = cfg.configs[1:] assert cfg.configs[0] == config_from_dict(DICT2_2, lowercase_keys=True) # writable configs cfg = ConfigurationSet( config_from_dict(DICT2_1, lowercase_keys=True), config_from_dict(DICT2_2, lowercase_keys=True), config_from_env(prefix=PREFIX, lowercase_keys=True), ) cfg.update({"abc": "xyz"}) assert cfg.configs[0] == config_from_dict(DICT2_1, lowercase_keys=True) cfg.configs = cfg.configs[1:] assert cfg.configs[0] == config_from_dict(DICT2_2, lowercase_keys=True)
def test_load_env(): # type: ignore cfg = ConfigurationSet( config_from_dict(DICT2_1, lowercase_keys=True), config_from_dict(DICT2_2, lowercase_keys=True), config_from_env(prefix=PREFIX, lowercase_keys=True), ) # from env assert cfg["a1.b1.c1"] == "1" assert cfg["a1.b1"].get_int("c1") == 1 assert cfg["a1.b1"].as_dict() == {"c1": "1", "c2": "2", "c3": "3"} assert cfg["a1.b2"].as_dict() == {"c1": "a", "c2": "True", "c3": "1.1"} # from dict assert cfg["a2.b1.c1"] == "f" assert cfg["a2.b2"].as_dict() == {"c1": 10, "c2": "YWJjZGVmZ2g=", "c3": "abcdefgh"}
def test_fails(): # type: ignore cfg = ConfigurationSet( config_from_dict(DICT2_1, lowercase_keys=True), config_from_dict(DICT2_2, lowercase_keys=True), config_from_env(prefix=PREFIX, lowercase_keys=True), ) with pytest.raises(KeyError, match="a1.b2.c3.d4"): assert cfg["a1.b2.c3.d4"] is Exception with pytest.raises(KeyError, match="c4"): assert cfg.a1.b2.c4 is Exception with pytest.raises(ValueError, match="Expected a valid True or False expression."): assert cfg["a1.b2"].get_bool("c3") is Exception
def test_dict_methods_keys_values(): # type: ignore cfg = ConfigurationSet( config_from_dict(DICT2_1, lowercase_keys=True), config_from_dict(DICT2_2, lowercase_keys=True), config_from_env(prefix=PREFIX, lowercase_keys=True), ) assert sorted(cfg.keys()) == [ "a1", "a2", ] assert dict(zip(cfg.keys(), cfg.values())) == { "a1": { "b1.c1": "1", "b1.c2": "2", "b1.c3": "3", "b2.c1": "a", "b2.c2": "True", "b2.c3": "1.1", }, "a2": { "b1.c1": "f", "b1.c2": False, "b1.c3": None, "b2.c1": 10, "b2.c2": "YWJjZGVmZ2g=", "b2.c3": "abcdefgh", }, } with cfg.dotted_iter(): assert sorted(cfg.keys()) == [ "a1.b1.c1", "a1.b1.c2", "a1.b1.c3", "a1.b2.c1", "a1.b2.c2", "a1.b2.c3", "a2.b1.c1", "a2.b1.c2", "a2.b1.c3", "a2.b2.c1", "a2.b2.c2", "a2.b2.c3", ] assert dict(zip(cfg.keys(), cfg.values())) == cfg.as_dict()
def test_load_env(): # type: ignore cfg = ConfigurationSet(config_from_dict(DICT2_1), config_from_dict(DICT2_2), config_from_env(prefix=PREFIX)) # from env assert cfg["a1.b1.c1"] == '1' assert cfg["a1.b1"].get_int('c1') == 1 assert cfg["a1.b1"].as_dict() == {"c1": '1', "c2": '2', "c3": '3'} assert cfg["a1.b2"].as_dict() == {"c1": "a", "c2": 'True', "c3": '1.1'} # from dict assert cfg["a2.b1.c1"] == 'f' assert cfg["a2.b2"].as_dict() == { 'c1': 10, 'c2': 'YWJjZGVmZ2g=', 'c3': 'abcdefgh' }
def test_repr(): # type: ignore import sys path = os.path.join(os.path.dirname(__file__), "python_config.py") cfg = ConfigurationSet( config_from_dict(DICT2_1, lowercase_keys=True), config_from_dict(DICT2_2, lowercase_keys=True), config_from_env(prefix=PREFIX, lowercase_keys=True), config_from_python(path, prefix="CONFIG", lowercase_keys=True), ) joined_dicts = dict((k, str(v)) for k, v in DICT1.items()) joined_dicts.update(DICT2_1) joined_dicts.update(DICT2_2) joined_dicts["sys.version"] = sys.hexversion assert str(dict( (k.lower(), v) for k, v in joined_dicts.items())) in repr(cfg)
def test_get_dict(): # type: ignore cfg = ConfigurationSet( config_from_dict(DICT2_1, lowercase_keys=True), config_from_dict(DICT2_2, lowercase_keys=True), config_from_env(prefix=PREFIX, lowercase_keys=True), ) a2 = { "b2.c1": 10, "b1.c1": "f", "b1.c2": False, "b1.c3": None, "b2.c1": 10, "b2.c2": "YWJjZGVmZ2g=", "b2.c3": "abcdefgh", } a2nested = { "b1": { "c1": "f", "c2": False, "c3": None }, "b2": { "c1": 10, "c2": "YWJjZGVmZ2g=", "c3": "abcdefgh" }, } assert cfg.get_dict("a2") == a2 assert cfg.a2.as_dict() == a2 assert dict(cfg.a2) == a2nested with cfg.dotted_iter(): assert cfg.get_dict("a2") == a2 assert cfg.a2.as_dict() == a2 # note that this still returns he nested dict since the dotted iteration # impacts only the parent cfg, not cfg.a assert dict(cfg.a2) == a2nested # to use dotted iteration for children, we need to explicitly set it with cfg.a2.dotted_iter() as cfg_a2: assert dict(cfg_a2) == a2 with pytest.raises(KeyError): assert cfg.get_dict("a3") is Exception assert dict(cfg.a2) == dict(cfg.a2.items())
def load_config(): global _conf home = expanduser('~') config_folder = f'{home}/.config/holmes' config_file = f'{config_folder}/config.yaml' if exists(config_file): with open(config_file) as input: _conf = ConfigurationSet(config_from_env('HOLMES'), config_from_yaml(input)) else: DEFAULT_CONFIG = { 'storage': 'file', 'files': { 'frames': f'{home}/Library/ApplicationSupport/watson/frames', 'state': f'{home}/Library/ApplicationSupport/watson/state', } } _conf = ConfigurationSet(config_from_dict(DEFAULT_CONFIG))
def test_dict_methods_items(): # type: ignore cfg = ConfigurationSet( config_from_dict(DICT2_1, lowercase_keys=True), config_from_dict(DICT2_2, lowercase_keys=True), config_from_env(prefix=PREFIX, lowercase_keys=True), ) assert dict(cfg.items()) == { "a1": { "b1.c1": "1", "b1.c2": "2", "b1.c3": "3", "b2.c1": "a", "b2.c2": "True", "b2.c3": "1.1", }, "a2": { "b1.c1": "f", "b1.c2": False, "b1.c3": None, "b2.c1": 10, "b2.c2": "YWJjZGVmZ2g=", "b2.c3": "abcdefgh", }, } with cfg.dotted_iter(): assert dict(cfg.items()) == dict( [ ("a2.b2.c2", "YWJjZGVmZ2g="), ("a1.b2.c2", "True"), ("a1.b2.c1", "a"), ("a1.b1.c2", "2"), ("a2.b2.c3", "abcdefgh"), ("a2.b1.c1", "f"), ("a1.b1.c3", "3"), ("a2.b1.c2", False), ("a2.b1.c3", None), ("a1.b1.c1", "1"), ("a2.b2.c1", 10), ("a1.b2.c3", "1.1"), ] )
def test_dict_methods_items(): # type: ignore cfg = ConfigurationSet( config_from_dict(DICT2_1, lowercase_keys=True), config_from_dict(DICT2_2, lowercase_keys=True), config_from_env(prefix=PREFIX, lowercase_keys=True), ) assert dict(cfg.items()) == dict( [ ("a2.b2.c2", "YWJjZGVmZ2g="), ("a1.b2.c2", "True"), ("a1.b2.c1", "a"), ("a1.b1.c2", "2"), ("a2.b2.c3", "abcdefgh"), ("a2.b1.c1", "f"), ("a1.b1.c3", "3"), ("a2.b1.c2", False), ("a2.b1.c3", None), ("a1.b1.c1", "1"), ("a2.b2.c1", 10), ("a1.b2.c3", "1.1"), ] )
def get_settings( config_file: Path = None, config: Dict[str, Any] = None, ) -> SimpleNamespace: if config_file is None: config_dir = Path( os.environ.get("PT__config_file", Path.home() / ".papertext")).resolve() config_file = config_dir / "config.toml" if not config_file.exists() and not config_file.is_dir(): config_file.touch(exist_ok=True) if config is None: config = {} cfg = ConfigurationSet( config_from_env(prefix="PT", separator="__"), config_from_toml(str(config_file), read_from_file=True), config_from_dict(config), ) cfg = cast(SimpleNamespace, cfg) return cfg
def test_dict_methods_keys_values(): # type: ignore cfg = ConfigurationSet( config_from_dict(DICT2_1, lowercase_keys=True), config_from_dict(DICT2_2, lowercase_keys=True), config_from_env(prefix=PREFIX, lowercase_keys=True), ) assert sorted(cfg.keys()) == [ "a1.b1.c1", "a1.b1.c2", "a1.b1.c3", "a1.b2.c1", "a1.b2.c2", "a1.b2.c3", "a2.b1.c1", "a2.b1.c2", "a2.b1.c3", "a2.b2.c1", "a2.b2.c2", "a2.b2.c3", ] assert dict(zip(cfg.keys(), cfg.values())) == cfg.as_dict()
def test_get_dict(): # type: ignore cfg = ConfigurationSet( config_from_dict(DICT2_1, lowercase_keys=True), config_from_dict(DICT2_2, lowercase_keys=True), config_from_env(prefix=PREFIX, lowercase_keys=True), ) assert cfg.get_dict("a2") == { "b1.c1": "f", "b1.c2": False, "b1.c3": None, "b2.c1": 10, "b2.c2": "YWJjZGVmZ2g=", "b2.c3": "abcdefgh", } assert cfg.a2.as_dict() == { "b1.c1": "f", "b1.c2": False, "b1.c3": None, "b2.c1": 10, "b2.c2": "YWJjZGVmZ2g=", "b2.c3": "abcdefgh", } assert dict(cfg.a2) == { "b1.c1": "f", "b1.c2": False, "b1.c3": None, "b2.c1": 10, "b2.c2": "YWJjZGVmZ2g=", "b2.c3": "abcdefgh", } with raises(KeyError): assert cfg.get_dict("a3") is Exception assert set( cfg.a2.values()) == {"f", False, None, 10, "YWJjZGVmZ2g=", "abcdefgh"} assert dict(cfg.a2) == dict(cfg.a2.items())
def __load_from_env(self, prefix: str = "FATE", separator: str = "_") -> config.Configuration: cfg = config.config_from_env(prefix, separator, lowercase_keys=True) return cfg
def test_equality(): # type: ignore cfg = config_from_env(PREFIX) assert cfg == config_from_dict(dict((k, str(v)) for k, v in DICT.items()))
def test_equality(): # type: ignore os.environ.update((PREFIX + "__" + k.replace(".", "__").upper(), str(v)) for k, v in DICT.items()) cfg = config_from_env(PREFIX, lowercase_keys=True) assert cfg == config_from_dict(dict((k, str(v)) for k, v in DICT.items()))
def test_load_env(): # type: ignore cfg = config_from_env(PREFIX, lowercase_keys=True) assert cfg["a1.b1.c1"] == "1" assert cfg["a1.b1"].get_int("c1") == 1 assert cfg["a1.b1"].as_dict() == {"c1": "1", "c2": "2", "c3": "3"} assert cfg["a1.b2"].as_dict() == {"c1": "a", "c2": "True", "c3": "1.1"}
def setup_config(dictionary): return config.ConfigurationSet( config.config_from_env(prefix="MLCOMMONS"), config.config_from_yaml(config_path(), read_from_file=True), config.config_from_dict(dictionary), )
def test_load_env(): # type: ignore cfg = config_from_env(PREFIX) assert cfg["a1.b1.c1"] == '1' assert cfg["a1.b1"].get_int('c1') == 1 assert cfg["a1.b1"].as_dict() == {"c1": '1', "c2": '2', "c3": '3'} assert cfg["a1.b2"].as_dict() == {"c1": "a", "c2": 'True', "c3": '1.1'}