Skip to main content

Configuration with typed env vars

Project description

cabina

Codecov PyPI PyPI - Downloads Python Version

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}"

    class Kafka(cabina.Section):
        BOOTSTRAP_SERVERS: tuple = env.tuple("KAFKA_BOOTSTRAP_SERVERS")
        AUTO_COMMIT: bool = env.bool("KAFKA_AUTO_COMMIT", True)
assert Config.Main.API_URL == "http://localhost:8080"
assert Config["Main"]["API_URL"] == "http://localhost:8080"

Recipes

Root Section

export API_HOST=localhost;
export API_PORT=8080;
import cabina
from cabina import env


class Config(cabina.Config, 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")
    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")
    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


class Config(cabina.Config, cabina.Section):
    HTTP_TIMEOUT: int = env("HTTP_TIMEOUT", parser=parse_duration)


assert Config.HTTP_TIMEOUT == 10

Prefetch Env Vars

export DEBUG=yes;
export API_PORT=80a;  # <- extra "a"
import cabina
from cabina import env


class Config(cabina.Config, cabina.Section):
    DEBUG = env.bool("DEBUG")
    API_HOST = env.str("API_HOST")
    API_PORT = env.int("API_PORT")


Config.prefetch()

# 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")
    API_PORT = env.int("PORT")


assert Config.API_HOST == "localhost"
assert Config.API_PORT == 8080

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cabina-0.3.1.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

cabina-0.3.1-py3-none-any.whl (15.3 kB view details)

Uploaded Python 3

File details

Details for the file cabina-0.3.1.tar.gz.

File metadata

  • Download URL: cabina-0.3.1.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.3.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3

File hashes

Hashes for cabina-0.3.1.tar.gz
Algorithm Hash digest
SHA256 7bdd26e2121b1816ee9515673e8a5e3bc8455d5c80785ec33f4ae27386e551ea
MD5 179bf33c1eeb1b7ebfcb053e8fbde0f9
BLAKE2b-256 5c69857f0dbe9fc3d76e6da3535ffac3db6bc43a561c1bd0cdaea8a7339f4ce6

See more details on using hashes here.

File details

Details for the file cabina-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: cabina-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 15.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.3.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3

File hashes

Hashes for cabina-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 14fa697655aff4a5003e009508e7d7666a3e12fd7bf0b06dada4c2b8dc25158c
MD5 464e9388607d71ea1c2cde2de97e6c92
BLAKE2b-256 d66e5a0a5dc3f638bdf90f4977939c7b04a9bd49d26b763696a424ab91b03d90

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page