Skip to main content

Zero-dependency configuration library for the molcrafts ecosystem

Project description

molcfg

CI PyPI Ruff Python Typed License Runtime

molcfg is a zero-dependency Python configuration library for applications that need predictable config loading, merging, validation, interpolation, and safe in-memory mutation.

PyPI distribution name: molcrafts-molcfg Import name: molcfg

Version 1.0.0 stabilizes the public API around four core areas:

  • layered loading from dictionaries, files, environment variables, and CLI arguments
  • nested configuration access with freeze, snapshot, rollback, and change callbacks
  • schema validation with recursive nested schemas, defaults, and unknown-field control
  • source tracking via Config.meta() so callers can explain where a value came from

Why molcfg

  • Runtime dependency free
  • Python 3.12+
  • Small surface area
  • Works well for services, CLIs, and internal tooling

Install

pip install molcrafts-molcfg

Optional local workflows:

pip install "molcrafts-molcfg[dev]"
pip install "molcrafts-molcfg[docs]"
pip install "molcrafts-molcfg[release]"

Quick Start

from molcfg import CliSource, ConfigLoader, DictSource, EnvSource, Range, validate

defaults = DictSource(
    {
        "app": {"name": "molcfg-demo"},
        "db": {"host": "localhost", "port": 5432},
    },
    name="defaults",
)

env = EnvSource(prefix="APP", name="env")
cli = CliSource(["--db.port=6432", "--app.debug=true"], name="cli")

cfg = ConfigLoader([defaults, env, cli]).load()

class DbSchema:
    host: str
    port: int
    __constraints__ = {"port": [Range(1, 65535)]}

validated_db = validate(
    cfg["db"].to_dict(),
    DbSchema,
    allow_extra=False,
    apply_defaults=True,
)

assert cfg["db.port"] == 6432
assert cfg.meta("db.port") == {
    "source": "cli",
    "history": ("defaults", "cli"),
}
assert validated_db == {"host": "localhost", "port": 6432}

Feature Overview

Sources

  • DictSource
  • JsonFileSource
  • TomlFileSource
  • EnvSource
  • CliSource

EnvSource and CliSource coerce values by default:

  • "true" / "false" -> bool
  • "5432" -> int
  • "3.14" -> float
  • "null" / "none" -> None
  • JSON-looking values like ["a", "b"] -> parsed Python objects

Pass coerce=False to keep raw strings.

Config Container

Config supports:

  • attribute access: cfg.db.host
  • dotted access: cfg["db.host"]
  • freezing: cfg.freeze()
  • rollback: cfg.snapshot() and cfg.rollback()
  • serialization: cfg.to_dict() and cfg.to_json()
  • metadata lookup: cfg.meta("db.host")

Validation

validate() supports:

  • primitive types
  • Literal
  • list[T]
  • dict[K, V]
  • nested class-based schemas
  • optional fields
  • defaults via apply_defaults=True
  • extra field rejection via allow_extra=False
  • built-in constraints: Range, Length, Pattern, OneOf

Merging

Strategies:

  • MergeStrategy.DEEP_MERGE
  • MergeStrategy.OVERRIDE
  • MergeStrategy.APPEND

All merge paths return isolated data structures and do not share nested mutable state with the inputs.

Documentation

This repository includes a Zensical documentation site in docs/.

Common commands:

pip install ".[docs]"
zensical serve
zensical build

Deployment is expected to happen from Cloudflare Pages rather than GitHub Actions.

Development

pip install ".[dev]"
pytest -q

See CONTRIBUTING.md for contribution workflow and RELEASING.md for the 1.x release process.

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

molcrafts_molcfg-1.0.0.tar.gz (25.6 kB view details)

Uploaded Source

Built Distribution

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

molcrafts_molcfg-1.0.0-py3-none-any.whl (15.7 kB view details)

Uploaded Python 3

File details

Details for the file molcrafts_molcfg-1.0.0.tar.gz.

File metadata

  • Download URL: molcrafts_molcfg-1.0.0.tar.gz
  • Upload date:
  • Size: 25.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for molcrafts_molcfg-1.0.0.tar.gz
Algorithm Hash digest
SHA256 fc55d89979102a4e5e0b7130aee91e0c4f231c2f34336795080b3d207b03a555
MD5 561f969038f433ccc0af9a7bf71d4617
BLAKE2b-256 3494897866457e6df7d47af0af87419d061f49073bf89c3906cd8495f479c91b

See more details on using hashes here.

Provenance

The following attestation bundles were made for molcrafts_molcfg-1.0.0.tar.gz:

Publisher: release.yml on MolCrafts/molcfg

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file molcrafts_molcfg-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for molcrafts_molcfg-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 096959909fff3d433906e3a15a7bbc22b08382b0e817c149323dd8c78aa1d017
MD5 b31e96bbba004b21842a7da88b7d4c13
BLAKE2b-256 6b5d9331e83978dfefbf9dc23d94bef2c3d195de19560c46bd91783b1352ee50

See more details on using hashes here.

Provenance

The following attestation bundles were made for molcrafts_molcfg-1.0.0-py3-none-any.whl:

Publisher: release.yml on MolCrafts/molcfg

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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