Skip to main content

An easy Python configuration manager with Typing support.

Project description

Config-Man

Config-Man is a configuration manager for python projects. It helps you handle your project's runtime configurations in an easy and clean way. It also supports multiple config sources like json file, environment variables, hard coded defaults, etc.

Installation

Simply install using pip:

pip install config-man

Keep in mind that Config-Man uses Python type annotations (PEP 484), and f-Strings (PEP 498) so you'll need Python 3.6 or higher.

Usage

Defining config

In Config-Man, configuration is defined using a class subclassed from configman.ConfigMan. In this class, configurations are static members with type hints.

Currently Config-Man only supports primitives (bool, int, float, str) and subtypes of configman.ConfigMan:

from configman import ConfigMan


class ServerConfig(ConfigMan):
    port: int
    log_level: str

For creation of nested configs (for more organized code) simply do the following:

from configman import ConfigMan


class LoggingConfig(ConfigMan):
    log_level: str


class Config(ConfigMan):
    port: int
    logging: LoggingConfig

You can add default values during definition simply by assigning a value to it:

from configman import ConfigMan


class ServerConfig(ConfigMan):
    port: int = 80
    log_level: str = "error"

Loading config

First of all, you need to create an instance of your main config:

config = Config()

Config Sources

Then you need to tell it where to look for configurations. Config-Man supports multiple config sources. Currently it supports hard-coded, environment variables, json config files and arguments. If there is a config source that you need and Config-Man does not support feel free to open an issue.

1. hard-coded

Apart from the default value you set during config definition, you can add an other default value during config load process simply by assigning the default value to it:

config.port = 443
2. Environment Variables

Config-Man can read configurations from environment variables.

One way to use env as a source is to assign a specific env to a config:

config.set_env("logging.log_level")

By default all dots "." in a variable path will be replaced by double under scores "__", So logging.log_level will be filled by the value of logging__log_level.

You can also set a specific name for the env:

config.set_env("logging.log_level", "LOG_LEVEL")

Another way is to tell Config-Man to load all possible configs from env

config.set_auto_env()

In order to avoid collisions between different programs, you can add a prefix to all envs (in auto_env):

config.set_auto_env("MY_PROGRAM")

Now when you load the config, Config-Man tries to read MY_PROGRAM__PORT and MY_PROGRAM__logging__log_level and put their values into the corresponding variables.

3. Config File

Currently Config-Man only supports json config files. You can set config file using:

config.set_config_file("config.json")
4. Arguments

You can tell Config-Man to read a specific config from arguments using:

import argparse

parser = argparse.ArgumentParser()
config.set_arg("logging.log_level", "log_level", parser)

Config-Man automatically adds needed argument to parser. If necessary, you can also define action, help, and required.

Loading Configs

Finally you can load the config itself by calling:

config.load()

By default configs from file overrides config from env and config from args overrides everything else.

If you like to do things in a different way, you can run load_from_env, load_from_file and load_from_args by yourself in any order to desire.

Creating an empty config file

If you wish to create an empty config file, you can do so using to_dict:

import json

config = Config()

with open("config.json", "w") as f:
    json.dump(f, config.to_dict(), indent=2)

This way config.json will contain an empty config ready for you to fill.

Full example

import argparse

from configman import ConfigMan


class LoggingConfig(ConfigMan):
    log_level: str = "error"


class Config(ConfigMan):
    port: int
    logging: LoggingConfig


config = Config()
parser = argparse.ArgumentParser()

config.port = 443
config.set_auto_env("MY_PROGRAM")
config.set_env("logging.log_level", "LOG_LEVEL")
config.set_config_file("config.json")
config.set_arg("logging.log_level", "--log_level", "-l", parser)

args = parser.parse_args()

config.load(args)

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-man-0.0.4.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

config_man-0.0.4-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file config-man-0.0.4.tar.gz.

File metadata

  • Download URL: config-man-0.0.4.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.0.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.5

File hashes

Hashes for config-man-0.0.4.tar.gz
Algorithm Hash digest
SHA256 8965452785b2d0ce81fc45952143c7b12cc067c7d8add24ab121bcb581631cef
MD5 198a8c78b30541ec336b7fd52100804f
BLAKE2b-256 fbbbca8b932e0b81c13372058c35c0d8309625ba08c5e5722be4663682d5d7a7

See more details on using hashes here.

File details

Details for the file config_man-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: config_man-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 6.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/45.0.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.5

File hashes

Hashes for config_man-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 fc56fef26f4427eb8907407a433d0092f1afd783335b1f2b19352c9f5252b347
MD5 584337a2981bc3d843f9ef168690ec25
BLAKE2b-256 a41a8e847cf2365ef90897bf53291d41eb2c7b92f4cc28e1a67209d331beccf2

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