Пример #1
0
def test_load_toml_filename():  # type: ignore
    with tempfile.NamedTemporaryFile() as f:
        f.file.write(TOML.encode())
        f.file.flush()
        cfg = config_from_toml(f.name, read_from_file=True)
    assert cfg["a1.b1.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}
    assert cfg == config_from_dict(DICT)
Пример #2
0
def test_load_toml_2():  # type: ignore
    cfg = config_from_toml(TOML2)
    assert cfg["owner.name"] == 'ABC'
    assert cfg["servers"].as_dict() == {
        'alpha.dc': 'eqdc10',
        'alpha.ip': '10.0.0.1',
        'beta.dc': 'eqdc10',
        'beta.ip': '10.0.0.2'
    }
    assert cfg["clients.data"] == [['gamma', 'delta'], [1, 2]]
Пример #3
0
def test_load_toml_2():  # type: ignore
    cfg = config_from_toml(TOML2)
    assert cfg["owner.name"] == "ABC"
    assert cfg["servers"].as_dict() == {
        "alpha.dc": "eqdc10",
        "alpha.ip": "10.0.0.1",
        "beta.dc": "eqdc10",
        "beta.ip": "10.0.0.2",
    }
    assert cfg["clients.data"] == [["gamma", "delta"], [1, 2]]
 def load_complete_cfg(self):
     """Loads the complete configuration"""
     if os.path.isfile(self._filename):
         path, filename = os.path.split(self._filename)
         if self.num_parent_directories == 0:
             self._complete_cfg = config.ConfigurationSet(
                 config.config_from_toml(self._filename,
                                         read_from_file=True), )
         elif self.num_parent_directories == 1:
             self._complete_cfg = config.ConfigurationSet(
                 config.config_from_toml(self._filename,
                                         read_from_file=True),
                 config.config_from_toml(os.path.join(path, '..', filename),
                                         read_from_file=True),
             )
         elif self.num_parent_directories == 2:
             self._complete_cfg = config.ConfigurationSet(
                 config.config_from_toml(self._filename,
                                         read_from_file=True),
                 config.config_from_toml(os.path.join(path, '..', filename),
                                         read_from_file=True),
                 config.config_from_toml(os.path.join(
                     path, '..', '..', filename),
                                         read_from_file=True))
         else:
             raise ValueError(
                 'Unsupported value for "num_parent_directories"')
     else:
         self._complete_cfg = config.ConfigurationSet(
             config.config_from_dict(dict()))
     self.add_ephemeral_attributes()
Пример #5
0
def test_reload_toml():  # type: ignore
    with tempfile.NamedTemporaryFile() as f:
        f.file.write(TOML.encode())
        f.file.flush()
        cfg = config_from_toml(f.name, read_from_file=True)
        assert cfg == config_from_dict(DICT)

        f.file.seek(0)
        f.file.truncate(0)
        f.file.write(b'[owner]\nname = "ABC"\n')
        f.file.flush()
        cfg.reload()
        assert cfg == config_from_dict({"owner.name": "ABC"})
Пример #6
0
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
Пример #7
0
def test_equality():  # type: ignore
    cfg = config_from_toml(TOML)
    assert cfg == config_from_dict(DICT)
Пример #8
0
def test_load_toml():  # type: ignore
    cfg = config_from_toml(TOML)
    assert cfg["a1.b1.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}