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
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)
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)
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}!")
def load_config() -> Config: konfi.set_sources(konfi.FileLoader("repo.yaml"), konfi.Env(decoder="yaml")) return konfi.load(Config)