Skip to main content

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)

Metadata

Release files for config-man 0.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for config-man 0.0.4
File Size Uploaded Provenance Actions
config-man-0.0.4.tar.gz 6.7 kB Without provenance Details for config-man-0.0.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for config-man 0.0.4
File Interpreter ABI Platform Actions
config_man-0.0.4-py3-none-any.whl Python 3 none any Details for config_man-0.0.4-py3-none-any.whl

Total release size: 13.0 kB

Release files / config-man-0.0.4.tar.gz

Download URL config-man-0.0.4.tar.gz
Size 6.7 kB
Tags Source
SHA-256 checksum
How to use checksums
8965452785b2d0ce81fc45952143c7b12cc067c7d8add24ab121bcb581631cef
BLAKE2b-256 checksum
How to use checksums
fbbbca8b932e0b81c13372058c35c0d8309625ba08c5e5722be4663682d5d7a7
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / config_man-0.0.4-py3-none-any.whl

Download URL config_man-0.0.4-py3-none-any.whl
Size 6.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fc56fef26f4427eb8907407a433d0092f1afd783335b1f2b19352c9f5252b347
BLAKE2b-256 checksum
How to use checksums
a41a8e847cf2365ef90897bf53291d41eb2c7b92f4cc28e1a67209d331beccf2
Upload date
Uploaded using Trusted Publishing?
What is 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

Security for config-man 0.0.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page