Skip to main content

The Strangely Familiar Config Parser

Project description

konfik-logo

>> The Strangely Familiar Config Parser <<

Codecov Code style: black MIT license

Konfik is a simple configuration parser that helps you access your config variables using dot (.) notation. This lets you to do this —

foo_bar_bazz = config.FOO.BAR.BAZZ

— instead of this —

foo_bar_bazz = config["FOO"]["BAR"]["BAZZ"]

Konfik currently supports TOML, YAML, DOTENV and JSON configuration formats.

⚙️ Installation

Install Konfik via pip:

pip install konfik

💡 Usage

Let's see how you can parse a TOML config file and access the variables there. For demonstration, we'll be using the following config.toml file:

# Contents of `config.toml`

title = "TOML Example"

[owner]
name = "Tom Preston-Werner"
dob = 1979-05-27T07:32:00-08:00 # First class dates

[servers]
  [servers.alpha]
  ip = "10.0.0.1"
  dc = "eqdc10"

  [servers.beta]
  ip = "10.0.0.2"
  dc = "eqdc10"

[clients]
data = [ ["gamma", "delta"], [1, 2] ]

To parse this in Python:

from pathlib import Path
from konfik import Konfik

# Define the config path
BASE_DIR = Path(__file__).parent
CONFIG_PATH_TOML = BASE_DIR / "config.toml"

# Initialize the konfik class
konfik = Konfik(config_path=CONFIG_PATH_TOML)

# Print the config file as a Python dict
konfik.show_config()

# Get the config dict from the konfik class
config = konfik.config

# Access and print the variables
print(config.title)
print(config.owner)
print(config.owner.dob)
print(config.database.ports)
print(config.servers.alpha.ip)
print(config.clients)

The .show_config() method will print your entire config file as a colorized Python dictionary object like this:

{
    'title': 'TOML Example',
    'owner': {
        'name': 'Tom Preston-Werner',
        'dob': datetime.datetime(1979, 5, 27, 7, 32, tzinfo=<toml.tz.TomlTz object at
0x7f2dfca308b0>)
    },
    'database': {
        'server': '192.168.1.1',
        'ports': [8001, 8001, 8002],
        'connection_max': 5000,
        'enabled': True
    },
    'servers': {
        'alpha': {'ip': '10.0.0.1', 'dc': 'eqdc10'},
        'beta': {'ip': '10.0.0.2', 'dc': 'eqdc10'}
    },
    'clients': {'data': [['gamma', 'delta'], [1, 2]]}
}

Konfik also exposes a few command-line options for you to introspect your config file and variables. Run:

konfik --help

This will reveal the options associated with the CLI tool:

Konfik -- The strangely familiar config parser ⚙️

usage: konfik [-h] [--path PATH] [--show] [--show-literal] [--var VAR] [--version]

optional arguments:
  -h, --help      show this help message and exit
  --path PATH     add config file path
  --show          print config as a dict
  --show-literal  print config file content literally
  --var VAR       print config variable
  --version       print konfik-cli version number

To inspect the value of a specific variable in a config.toml file you can run:

konfik --path=examples/config.env --var servers.alpha.ip

🎉 Contribution

  • Clone the repo

  • Spin up and activate your virtual environment. You can use anything between Python 3.6 to Python 3.9.

  • Install poetry

  • Install the dependencies via:

    poetry install
    
  • Make your changes to the konfik/__init__.py file

  • Run the tests via the following command. Make sure you've Python 3.6 - Python 3.9 installed, otherwise Tox would throw an error.

    make test
    
  • Write a simple unit test for your change

  • Run the linter via:

    make lint
    
  • Send a pull request against the master branch

🍰

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

konfik-2.0.1.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

konfik-2.0.1-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file konfik-2.0.1.tar.gz.

File metadata

  • Download URL: konfik-2.0.1.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.8.5 Linux/5.4.0-7642-generic

File hashes

Hashes for konfik-2.0.1.tar.gz
Algorithm Hash digest
SHA256 aab64ba3555fc716a27da202f404737d52bc845ec6cb254bc2c0b7c93ec8862d
MD5 9891d61cc6d88959f062be22c3680802
BLAKE2b-256 f077e4a82d0043f7a404a17176beb1fd58525004c0ac89d9a2bfcf0e53ba98c1

See more details on using hashes here.

File details

Details for the file konfik-2.0.1-py3-none-any.whl.

File metadata

  • Download URL: konfik-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.8.5 Linux/5.4.0-7642-generic

File hashes

Hashes for konfik-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1e683c74207f8f390703d3fcb5aa10eb3f5b38be71bd6fdc2a87271118e5396b
MD5 e2c6e3c5e62205b8619b3eb33b1f8e52
BLAKE2b-256 89618caddc8b6966f20dac217158a0d47b2a12997731ee1f1166431a257eb6e8

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