Configuration with typed env vars
Project description
cabina
Installation
pip3 install cabina
Usage
import cabina
from cabina import computed, env
class Config(cabina.Config):
class Main(cabina.Section):
API_HOST: str = env.str("API_HOST", default="localhost")
API_PORT: int = env.int("API_PORT", default=8080)
@computed
def API_URL(cls) -> str:
return f"http://{cls.API_HOST}:{cls.API_PORT}"
assert Config.Main.API_URL == "http://localhost:8080"
assert Config["Main"]["API_URL"] == "http://localhost:8080"
print(Config)
# class <Config>:
# class <Main>:
# API_HOST = 'localhost'
# API_PORT = 8080
# API_URL = 'http://localhost:8080'
Recipes
- Root Section
- Computed Values
- Default Values
- Raw Values
- Custom Parsers
- JSON Parser
- Lazy Env
- Env Vars Prefix
- Inheritance
Root Section
export API_HOST=localhost;
export API_PORT=8080;
import cabina
from cabina import env
class Config(cabina.Config, cabina.Section): # <- inherited from cabina.Section
API_HOST = env.str("API_HOST")
API_PORT = env.int("API_PORT")
assert Config.API_HOST == "localhost"
assert Config.API_PORT == 8080
Computed Values
export API_HOST=localhost;
export API_PORT=8080;
import cabina
from cabina import computed, env
class Config(cabina.Config, cabina.Section):
API_HOST: str = env.str("API_HOST")
API_PORT: int = env.int("API_PORT")
@computed
def API_URL(cls) -> str:
return f"http://{cls.API_HOST}:{cls.API_PORT}"
assert Config.API_URL == "http://localhost:8080"
Default Values
export API_HOST=127.0.0.1;
import cabina
from cabina import env
class Config(cabina.Config, cabina.Section):
API_HOST = env.str("API_HOST", default="localhost") # <- default arg
API_PORT = env.int("API_PORT", default=8080)
assert Config.API_HOST == "127.0.0.1"
assert Config.API_PORT == 8080
Raw Values
export DEBUG= yes;
# ^ extra space
import cabina
from cabina import env
class Config(cabina.Config, cabina.Section):
DEBUG_RAW = env.raw("DEBUG") # <- alias to env("DEBUG")
DEBUG_STR = env.str("DEBUG")
assert Config.DEBUG_RAW == "" # True
assert Config.DEBUG_STR == "yes" # Error
Custom Parsers
export HTTP_TIMEOUT=10s;
import cabina
from cabina import env
from pytimeparse import parse as parse_duration # <- external package
class Config(cabina.Config, cabina.Section):
HTTP_TIMEOUT: int = env("HTTP_TIMEOUT", parser=parse_duration)
assert Config.HTTP_TIMEOUT == 10
JSON Parser
export IMAGE_SETTINGS='{"AllowedContentTypes": ["image/png", "image/jpeg"]}';
import json
import cabina
from cabina import env
class Config(cabina.Config, cabina.Section):
IMAGE_SETTINGS = env("IMAGE_SETTINGS", parser=json.loads) # <- json.loads
assert Config.IMAGE_SETTINGS == {
'AllowedContentTypes': ['image/png', 'image/jpeg']
}
Lazy Env
export DEBUG=yes;
export API_PORT=80a; # <- extra "a"
import cabina
from cabina import lazy_env
class Config(cabina.Config, cabina.Section):
DEBUG = lazy_env.bool("DEBUG")
API_HOST = lazy_env.str("API_HOST") # the actual value is retrieved on first access
API_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 int
Env Vars Prefix
export APP_HOST=localhost;
export APP_PORT=8080;
import cabina
env = cabina.Environment(prefix="APP_")
class Config(cabina.Config, cabina.Section):
API_HOST = env.str("HOST") # <- No "APP_" prefix
API_PORT = env.int("PORT")
assert Config.API_HOST == "localhost"
assert Config.API_PORT == 8080
Inheritance
import cabina
class Config(cabina.Config, cabina.Section):
DEBUG = False
class Api(cabina.Section):
API_HOST = "app.dev"
API_PORT = 5000
class ConfigLocal(Config):
DEBUG = True
class Api(Config.Api):
API_HOST = "localhost"
assert ConfigLocal.DEBUG is True
assert ConfigLocal.Api.API_HOST == "localhost"
assert ConfigLocal.Api.API_PORT == 5000
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
cabina-1.0.0.tar.gz
(17.1 kB
view details)
Built Distribution
cabina-1.0.0-py3-none-any.whl
(13.6 kB
view details)
File details
Details for the file cabina-1.0.0.tar.gz
.
File metadata
- Download URL: cabina-1.0.0.tar.gz
- Upload date:
- Size: 17.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b5f53b78ef7f4b4844f0f00e38aa46eddeca6ea720867fe41d69555ffc02da3f |
|
MD5 | ed0016d3e8bfe2abac454c65edaf2d6b |
|
BLAKE2b-256 | efcd16fba64483340c625d5094068610d1176d34e5aa98d7abaa4a86e5f1f52b |
File details
Details for the file cabina-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: cabina-1.0.0-py3-none-any.whl
- Upload date:
- Size: 13.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2a9c90e7d0acda72771b7accfe2b91cf4021c1db5bae3c70bace567c36d454e1 |
|
MD5 | 08ba5afb395f6e2246b7dc206c9716d5 |
|
BLAKE2b-256 | 566536d1040d398725a889b5e4a928a5bffa57df8e7ca46d4085b7816dfb1f3c |