Skip to main content

PyConfita: Confita-like library for Python

Project description

PyConfita: Confita-like for Python

Library that ease reading a value from multiple key-value stores/backends with ordered evaluation.

Disclaimer

Free implementation of the GO library Confita.

Features

  • Backends/stores supported:
    • Environment variables (EnvBackend);
    • File (YAML format) (FileBackend);
    • Python dictionary object (DictBackend);
    • Vault key-value store (VaultBackend);
  • Backends evaluation order: directly set by the order of backends in Confita.backends list. The last not None evaluated value is returned;
  • Explicit type conversion supported for str, bool, int, float;
  • Case sensitivity option: option to read key with casing variations (uppercased, lowercased).

Quickstart

from pyconfita import (
    LoggingInterface,
    Confita,
    EnvBackend,
    DictBackend,
    VaultBackend,
    FileBackend
)
dumb_logger = LoggingInterface()
c = Confita(
    logger=dumb_logger,
    backends=[
        FileBackend("/abs/path/vars.yaml"),
        DictBackend({"FOO": "bar"}),
        VaultBackend(dumb_logger, default_key_path=f"path1"),
        EnvBackend(),
    ],
)

v = c.get("FOO") 
# At least DictBackend should evaluate it as not None
assert v is not None
# Type should be `str` 
assert isinstance(v, str)

Evaluation order

Evaluation is performed in order of the list of backends. Next backend has priority over previous backend to set the value.

import os
from pyconfita import (
    LoggingInterface,
    Confita,
    EnvBackend,
    DictBackend
)
dumb_logger = LoggingInterface()

os.environ.setdefault("KEY", "VALUE_FROM_ENV")

c = Confita(
    logger=dumb_logger,
    backends=[
        DictBackend({
            "KEY": "VALUE",
            "BOOL_1": "false",
            "BOOL_2": "true"
        }),
        EnvBackend(),
    ],
)

assert c.get("KEY") == "VALUE_FROM_ENV" # Environment backend overrides previous backends' values
assert c.get("BOOL_1") == "false" # No implicit type conversion 
assert c.get("BOOL_2", **{"type": bool}) # Explicit type conversion requested

# Reverse evaluation order by reversing list of backends
c = Confita(
    logger=dumb_logger,
    backends=[
        EnvBackend(),
        DictBackend({"KEY": "VALUE"}),
    ],
)

assert c.get("KEY") == "VALUE" # Dict backend overrides previous backends' values

Explicit type conversion

Type conversion must be explicit. Only str, bool, int, float types are supported. Default type is str.

from pyconfita import (
    LoggingInterface,
    Confita,
    DictBackend
)
dumb_logger = LoggingInterface()

c = Confita(
    logger=dumb_logger,
    backends=[
        DictBackend({
            "BOOL_1": "false",
            "BOOL_2": "true"
        }),
    ],
)

assert c.get("BOOL_1") == "false" # No implicit type conversion 
assert c.get("BOOL_2", **{"type": bool}) # Explicit type conversion requested

Case sensitivity

from pyconfita import (
    LoggingInterface,
    Confita,
    DictBackend
)
dumb_logger = LoggingInterface()

# Case sensitivity is enabled by default
c = Confita(
    logger=dumb_logger,
    backends=[DictBackend({"KEY": "VALUE"})],
)

assert c.get("KEY") == "VALUE" 
assert c.get("key") == None 

# Disable case sensitivity
c = Confita(
    logger=dumb_logger,
    backends=[DictBackend({"KEY": "VALUE"})],
    case_sensitive=False
)

assert c.get("KEY") == "VALUE" 
assert c.get("key") == "VALUE" 

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

pyconfita-1.0.3.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyconfita-1.0.3-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file pyconfita-1.0.3.tar.gz.

File metadata

  • Download URL: pyconfita-1.0.3.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.12

File hashes

Hashes for pyconfita-1.0.3.tar.gz
Algorithm Hash digest
SHA256 66a9bf2fecee99e731b46a6544fad5585191c2dbf8fa24625530c8d5a5de9c5d
MD5 eea87e1fbfff377c004c006f9864a1b6
BLAKE2b-256 bf98fd5889d55794efaae2715aee3e44afb71a74a5cbc17fff775076435337fe

See more details on using hashes here.

File details

Details for the file pyconfita-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: pyconfita-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 9.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.12

File hashes

Hashes for pyconfita-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 97c8c2dcc01ec8ca388b45e61a4e621cb0f9a26544456b411995f8c7210c6e1a
MD5 6ed1148bcd04184c733d5b5f462351aa
BLAKE2b-256 047baa13cf3d0f915219c3e33a11718aefaff59ccb7751faf345e705415f2bb8

See more details on using hashes here.

Supported by

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