Package: cabina
cabinaInstallationpip3installcabinaUsageimportcabinafromcabinaimportcomputed,envclassConfig(cabina.Config):classMain(cabina.Section):API_HOST:str=env.str("API_HOST",default="localhost")API_PORT:int=env.int("API_PORT",default=8080)@computeddefAPI_URL(cls)->str:returnf"http://{cls.API_HOST}:{cls.API_PORT}"assertConfig.Main.API_URL=="http://localhost:8080"assertConfig["Main"]["API_URL"]=="http://localhost:8080"printprint(Config)# class <Config>:#     class <Main>:#         API_HOST = 'localhost'#         API_PORT = 8080#         API_URL = 'http://localhost:8080'RecipesRoot SectionComputed ValuesDefault ValuesRaw ValuesCustom ParsersJSON ParserLazy EnvEnv Vars PrefixInheritanceRoot SectionexportAPI_HOST=localhost;exportAPI_PORT=8080;importcabinafromcabinaimportenvclassConfig(cabina.Config,cabina.Section):# <- inherited from cabina.SectionAPI_HOST=env.str("API_HOST")API_PORT=env.int("API_PORT")assertConfig.API_HOST=="localhost"assertConfig.API_PORT==8080Computed ValuesexportAPI_HOST=localhost;exportAPI_PORT=8080;importcabinafromcabinaimportcomputed,envclassConfig(cabina.Config,cabina.Section):API_HOST:str=env.str("API_HOST")API_PORT:int=env.int("API_PORT")@computeddefAPI_URL(cls)->str:returnf"http://{cls.API_HOST}:{cls.API_PORT}"assertConfig.API_URL=="http://localhost:8080"Default ValuesexportAPI_HOST=127.0.0.1;importcabinafromcabinaimportenvclassConfig(cabina.Config,cabina.Section):API_HOST=env.str("API_HOST",default="localhost")# <- default argAPI_PORT=env.int("API_PORT",default=8080)assertConfig.API_HOST=="127.0.0.1"assertConfig.API_PORT==8080Raw ValuesexportDEBUG=yes;#            ^ extra spaceimportcabinafromcabinaimportenvclassConfig(cabina.Config,cabina.Section):DEBUG_RAW=env.raw("DEBUG")# <- alias to env("DEBUG")DEBUG_STR=env.str("DEBUG")assertConfig.DEBUG_RAW==""# TrueassertConfig.DEBUG_STR=="yes"# ErrorCustom ParsersexportHTTP_TIMEOUT=10s;importcabinafromcabinaimportenvfrompytimeparseimportparseasparse_duration# <- external packageclassConfig(cabina.Config,cabina.Section):HTTP_TIMEOUT:int=env("HTTP_TIMEOUT",parser=parse_duration)assertConfig.HTTP_TIMEOUT==10JSON ParserexportIMAGE_SETTINGS='{"AllowedContentTypes": ["image/png", "image/jpeg"]}';importjsonimportcabinafromcabinaimportenvclassConfig(cabina.Config,cabina.Section):IMAGE_SETTINGS=env("IMAGE_SETTINGS",parser=json.loads)# <- json.loadsassertConfig.IMAGE_SETTINGS=={'AllowedContentTypes':['image/png','image/jpeg']}Lazy EnvexportDEBUG=yes;exportAPI_PORT=80a;# <- extra "a"importcabinafromcabinaimportlazy_envclassConfig(cabina.Config,cabina.Section):DEBUG=lazy_env.bool("DEBUG")API_HOST=lazy_env.str("API_HOST")# the actual value is retrieved on first accessAPI_PORT=lazy_env.int("API_PORT")Config.prefetch()# <- prefetch() accesses all variables# ConfigEnvError: Failed to prefetch:# - Config.API_HOST: 'API_HOST' does not exist# - Config.API_PORT: Failed to parse '80a' as intEnv Vars PrefixexportAPP_HOST=localhost;exportAPP_PORT=8080;importcabinaenv=cabina.Environment(prefix="APP_")classConfig(cabina.Config,cabina.Section):API_HOST=env.str("HOST")# <- No "APP_" prefixAPI_PORT=env.int("PORT")assertConfig.API_HOST=="localhost"assertConfig.API_PORT==8080InheritanceimportcabinaclassConfig(cabina.Config,cabina.Section):DEBUG=FalseclassApi(cabina.Section):API_HOST="app.dev"API_PORT=5000classConfigLocal(Config):DEBUG=TrueclassApi(Config.Api):API_HOST="localhost"assertConfigLocal.DEBUGisTrueassertConfigLocal.Api.API_HOST=="localhost"assertConfigLocal.Api.API_PORT==5000