Skip to main content

Easily parse configurations across various formats (YAML, JSON, etc.) with recursive class binding, environment variables resolution and type safety.

Project description

config-binder

downloads PyPI version License versions

Simple configuration parsing with recursive class binding, environment variables resolution and types safety.

Installation

pip install config-binder

Simple example

input.yaml

host: ${MYAPP_HOST:localhost}
port: 5432
username: admin
password: ${MYAPP_PASSWORD}

ENV exposure

export MYAPP_PASSWORD=123

With binding:

class MySettings:
    host: str
    port: int
    username: str
    password: str
    source_urls: List[str]


config = ConfigBinder.load('input.yaml')
print(f"type: {type(config).__name__}, config: host:{config.host} port:{config.port} source_urls:{config.source_urls}")

# Output:
# type: MySettings, config: host:localhost port:5432 source_urls:['some-url.com', 'another-url.com']

Without binding

in this case data will be returned in dict with just all the environment variables resolved

config = ConfigBinder.load('input.yaml')
print(f"type: {type(config).__name__}, config: {config}")

# Output:
# type: dict, config: {'host': 'localhost', 'port': 5432, 'username': 'admin', 'password': '123', 'source_urls': ['some-url.com', 'another-url.com']}

More complex example

input.yaml

name: MyApplication
logging_level: INFO
redis_config:
  host: ${MYAPP_REDIS_HOST:127.0.0.1}
  post: ${MYAPP_REDIS_PORT:6379}
  password: ${MYAPP_REDIS_PASS}
  encryption_Key: ${MYAPP_REDIS_ENCRYPTION_KEY}
sources_configs:
  orders:
    url: some-url.com/orders
    token: ${MYAPP_ORDERS_SOURCE_TOKEN}
    retry_policy:
      max_attempts: 5
      backoff_seconds: 10
  products:
    url: another-url.com/products
    token: ${MYAPP_ORDERS_products_TOKEN}
    retry_policy:
      max_attempts: 3
      backoff_seconds: 5

ENV exposure

export MYAPP_REDIS_PASS=redis_pass
export MYAPP_REDIS_ENCRYPTION_KEY=very_strong_key
export MYAPP_ORDERS_SOURCE_TOKEN=orders_token
export MYAPP_ORDERS_PRODUCTS_TOKEN=products_token

Config classes definition

class RedisConfig:
    host: str
    port: int
    password: str
    encryption_key: str

class RetryPolicy:
    max_attempts: int
    backoff_seconds: int

class SourceConfig:
    url: str
    token: str
    retry_policy: RetryPolicy

class AppConfig:
    name: str
    logging_level: Literal['DEBUG', 'INFO', 'ERROR']
    redis_config: RedisConfig
    sources_configs: Dict[str, SourceConfig]

Binding from file

config = ConfigBinder.load('input.yaml', AppConfig)

Binding from str variable

input_yaml is variable with the same yaml specified above

config = ConfigBinder.read(ConfigType.yaml, input_yaml, AppConfig)

Results

input yaml is now bound to AppConfig class with env variables resolution and types validation

print(config.name)
print(config.logging_level)
print(f"type: {type(config.redis_config).__name__}, config: {str(vars(config.redis_config))}")
print(f"type: {type(config.sources_configs['orders']).__name__}, config: {str(vars(config.sources_configs['orders']))}")
print(f"type: {type(config.sources_configs['products']).__name__}, config: {str(vars(config.sources_configs['products']))}")
# Output:
# MyApplication
# INFO
# type: RedisConfig, config: {'host': '127.0.0.1', 'port': 6379, 'password': 'redis_pass', 'encryption_key': 'None'}
# type: SourceConfig, config: {'url': 'some-url.com/orders', 'token': 'orders_token', 'retry_policy': <__main__.RetryPolicy object at 0x7f268fe2fe00>}
# type: SourceConfig, config: {'url': 'another-url.com/products', 'token': 'products_token', 'retry_policy': <__main__.RetryPolicy object at 0x7f268fe2fe30>}

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

config_binder-0.2.1.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

config_binder-0.2.1-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file config_binder-0.2.1.tar.gz.

File metadata

  • Download URL: config_binder-0.2.1.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.4 Linux/6.5.0-1025-azure

File hashes

Hashes for config_binder-0.2.1.tar.gz
Algorithm Hash digest
SHA256 1036f1c65f25fe401a75a850e89f3f9fb5b2b98549f8c194df9f46cbbfd7fa24
MD5 7800da7393782cb6f1abd16b654107be
BLAKE2b-256 414ba6a4d86bf88a21f7d011a1f5b3d9de92ad04d0a444f22f16ecab107c39d2

See more details on using hashes here.

File details

Details for the file config_binder-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: config_binder-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.4 Linux/6.5.0-1025-azure

File hashes

Hashes for config_binder-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1a47aad155daf665ac03117a418799bfa7d77a05549f4c174743ed51d6595944
MD5 2aeb69178eaade528d61a26554f1240e
BLAKE2b-256 165383dc8b1d6818825d0fbbbbd5d926511b794532a49d95594729060052e0ba

See more details on using hashes here.

Supported by

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