Exemplo n.º 1
0
def test_env_source():
    @konfi.template()
    class SubConf:
        b: int
        d_e: float

    @konfi.template()
    class Conf:
        a: str
        with_underscore: str
        sub: SubConf

    with mock.patch("konfi.sources.env.os") as mocked_os:
        mocked_os.environ = {
            "P_A": "\"test\"",
            "P_WITHUNDERSCORE": "\"value\"",
            "P_SUB_B": "5",
            "P_SUB_DE": "3.5",
        }
        konfi.set_sources(konfi.Env("P_"))
        c: Conf = konfi.load(Conf)

    assert c.a == "test"
    assert c.with_underscore == "value"
    assert c.sub.b == 5
    assert c.sub.d_e == 3.5
Exemplo n.º 2
0
def load_config(config_file: str) -> Config:
    """Load the ari config."""
    konfi.set_sources(
        konfi.FileLoader(config_file, ignore_not_found=True),
        konfi.Env("ARI_"),
    )

    return konfi.load(Config)
Exemplo n.º 3
0
def load_config(path: str) -> Config:
    """Load the configuration from the given path.

    The file configs are then overwritten by the environment variables
    with the "BOT_" prefix.
    """
    konfi.set_sources(
        konfi.FileLoader(path, ignore_not_found=True),
        konfi.Env("BOT_"),
    )

    return konfi.load(Config)
Exemplo n.º 4
0
# let's tell konfi how to convert to a Location


@konfi.register_converter(Location)
def location_converter(value: Any) -> Location:
    # this works! If you're not amazed by this then I don't know what's wrong
    # with you
    lat, long = konfi.convert_value(value, Tuple[float, float])
    return Location(lat, long)


@konfi.template()
class Config:
    loc: Location
    # you can also pass a one-time converter instead of registering a new one.
    temp: Temperature = konfi.field(converter=Temperature.create)


if __name__ == "__main__":
    konfi.set_sources(
        # file loader is a higher-order source which determines what
        # kind of file it's dealing with by looking at the file extension.
        # It then delegates the actual work to another source. (in this case
        # konfi.TOML)
        konfi.FileLoader("config.toml", ignore_not_found=True), )

    config = konfi.load(Config)

    print(f"it's {config.temp} in {config.loc}")
Exemplo n.º 5
0
import konfi


@konfi.template()
class UserInfo:
    name: str
    country: str


@konfi.template()
class AppConfig:
    name: str = "konfi"
    user: UserInfo


if __name__ == "__main__":
    konfi.set_sources(
        konfi.YAML("config.yml"),
        konfi.Env(prefix="app_"),
    )

    # the return type is usually inferred, but some editors *cough* PyCharm
    # can't deal with the function alias
    config: AppConfig = konfi.load(AppConfig)

    print(f"Hello {config.user.name} from {config.user.country}")
    print(f"Welcome to {config.name}!")
Exemplo n.º 6
0
def load_config() -> Config:
    konfi.set_sources(konfi.FileLoader("repo.yaml"), konfi.Env(decoder="yaml"))
    return konfi.load(Config)