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}"
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):  # <- 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']
}

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()  # <- prefetch method

# 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

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.5.1.tar.gz (13.5 kB view details)

Uploaded Source

Built Distribution

cabina-0.5.1-py3-none-any.whl (15.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: cabina-0.5.1.tar.gz
  • Upload date:
  • Size: 13.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12

File hashes

Hashes for cabina-0.5.1.tar.gz
Algorithm Hash digest
SHA256 9a5eda0617f86772517047366a901abe617596367a93fb4a7b78c8a84846c2e7
MD5 984af25f0b98b44e4027d7a7051e9bde
BLAKE2b-256 12efce21af6a565501017063879fb9587af196dfcbd7022af111e4ace533978f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cabina-0.5.1-py3-none-any.whl
  • Upload date:
  • Size: 15.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12

File hashes

Hashes for cabina-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 09a2af27c352969a0bf278e30955813e2a3691ba26c0b61ab31a7a278dc56514
MD5 b38c9fdb5c9ba5b3eada6a899132cd10
BLAKE2b-256 0eef7bb6ee71a0f1aaef6a33909104732f512e332aec4991451523b77099f678

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