Skip to main content

A universal configuration parser for Python.

Project description

configmate: universal, practical configuration parser for Python.

configmate is designed for parsing configuration files in various formats. Key features:

With support for multiple file format backends, this package provides robust validation and allows for configuration overrides. Engineered for straightforward and reliable integration into Python applications.

Usage

Installation

Configmate is available on PyPI:

pip install configmate

To keep it dependency-light, configmate does not include any file format backends. Out of the box, it supports .json and .ini. To get support for yaml and toml you can install extra backends:

pip install configmate[yaml] # reads .yml and .yaml
pip install configmate[toml] # reads .toml

For more flexible validation, you can install validation backends:

pip install configmate[pydantic]

Features:

Configuration layering

configmate supports configuration layering. This is useful for example if you want to have a default configuration file and override it with a user-specific configuration file. For example:

# config.json
{
    "host": "localhost",
    "port": 8080
}
# override.json
{
    "host": "override"
}
import configmate
config = configmate.ConfigMate("config.json", overlays=["override.json"])
print(config['host']) # override
print(config['port']) # 8080

Subsectioning

configmate supports file subsectioning. This is useful if you want to keep your configuration for different parts of the application in a single file. For example:

# config.json
{
    "database": {
        "host": "localhost",
        "port": 8080
    },
    "server": {
        "host": "localhost",
        "port": 9090
    }
}
import configmate

databaseconfig = configmate.ConfigMate("config.json", slices=["database"])
print(databaseconfig['host']) # localhost
print(databaseconfig['port']) # 8080

serverconfig = configmate.ConfigMate("config.json", slices=["server"])
print(serverconfig['host']) # localhost
print(serverconfig['port']) # 9090

Environment variable interpolation

configmate supports environment variable interpolation. This is useful if you want to use environment variables in your configuration files. For example:

# config.json
{
    "host": "${DB_HOST}",
    "port": "${DB_PORT}"
}
export DB_HOST=localhost
export DB_PORT=8080
from configmate import ConfigMate
config = ConfigMate("config.json")
print(config['host']) # localhost
print(config['port']) # 8080

Validation

configmate supports validation of configuration files. This is useful if you want to ensure that your configuration files are valid. For example:

# config.json
{
    "host": "localhost",
    "port": 8080
}
import configmate
import dataclasses

@dataclasses.dataclass
class Config:
    host: str
    port: int

config = configmate.ConfigMate("config.json", validator=Config)
print(config.host) # localhost

Command Line Interface (CLI):

configmate supports overrides and section-specific updates via CLI. You can specify multiple configuration files and also directly update settings via CLI arguments. This enables flexible and robust configuration management for Python applications.

Syntax

python path/to/script.py [Config1] [-override/path ...] [--section.key value]

Examples:

# Single config, multiple overrides
python path/to/script.py -over/ride/1.json -over/ride/2.json

# Single config, multiple section modifications
python path/to/script.py --database.host localhost --server.port 8080

# Multiple configs
python path/to/script.py Config1 -over/ride1.json Config2 -over/ride2.json

# All together
python path/to/script.py Config1 -over/ride1.yaml --database.host localhost Config2 -over/ride2.yaml --server.port 8080

Flags

Use an empty -- to separate CLI arguments from flags. This is useful if you want to pass CLI arguments to your script without them being parsed by configmate. For example: The available options are:

  • -h, --help: Print help message
  • -d, --debug: Print debug information
  • -p, --print: Print configuration files
python path/to/script.py -- --help

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

configmate-0.1.2.tar.gz (50.9 kB view details)

Uploaded Source

Built Distribution

configmate-0.1.2-py3-none-any.whl (27.1 kB view details)

Uploaded Python 3

File details

Details for the file configmate-0.1.2.tar.gz.

File metadata

  • Download URL: configmate-0.1.2.tar.gz
  • Upload date:
  • Size: 50.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.12.0 Darwin/23.1.0

File hashes

Hashes for configmate-0.1.2.tar.gz
Algorithm Hash digest
SHA256 74fe5cd09a656bd9873397449a07d5c5169c317e121662b3eee52282131be231
MD5 d75b59ac97518ea83729ff0b851b2472
BLAKE2b-256 c3daaaa10b142c050b52cf6bc73ce39fb66a9d8207b3b024ee5203027d0c1afb

See more details on using hashes here.

File details

Details for the file configmate-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: configmate-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 27.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.12.0 Darwin/23.1.0

File hashes

Hashes for configmate-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 526961d2d7bbf81fb5696927812e1f470007904e130aef818fa871af806b074a
MD5 d77bb977e7c44e54bb16f8fde528d9a7
BLAKE2b-256 3b3c143c941bd9b1a54f662496cd8d4d1c7bf68f2b9268993f520f03832f5f8a

See more details on using hashes here.

Supported by

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