Skip to main content

Schema-driven config with JSON+TOML backends, dotted-key access, atomic save, and migrations.

Project description

              .    .  c o d e c h u  .    .
           .   \  |  /  c o n f i g  \  |   .
        ((((( ── (( ⚙ )) ──────────── (( ⚙ )) ── )))))
           '   /  |  \                /  |   '
              '    '  schema · dotted · atomic

Schema-first config. Validates on set. Saves atomically. Migrates on load.

codechu-config

Schema-driven configuration for Python. JSON + TOML backends, dotted-key access, atomic save, and forward migrations. Pure stdlib (tomli_w is the only optional dependency, needed only when writing TOML).

pip install codechu-config           # JSON-only
pip install "codechu-config[toml]"   # adds TOML write support

What it gives you

  • Schema validation — types, choices, ranges, required fields, all enforced on every set.
  • Dotted keyscfg.set("watchdog.threshold", 90) writes nested structure; cfg.get("watchdog.threshold") reads it back.
  • Atomic save — tempfile-in-same-dir + fsync + os.replace. A crash mid-write leaves the previous content intact, never garbage. If codechu-fs is installed it is used transparently; otherwise the stdlib fallback ships in-package.
  • Migrations — point the Config at a list of callables; they run in order whenever the on-disk _version differs from the schema's default.
  • JSON + TOML — JSON by default (zero deps). TOML reads via stdlib tomllib (Python 3.11+); TOML writes need the optional tomli_w.
  • Dict sugarcfg["language"] = "tr", "language" in cfg.

Quick start

from codechu_config import Config, Field, Schema

schema = Schema({
    "_version": Field(int, default=1),
    "language": Field(str, default="en", choices=["en", "tr"]),
    "watchdog.threshold": Field(int, default=90, range=(0, 100)),
    "watchdog.enabled": Field(bool, default=True),
})

cfg = Config(schema, "~/.config/myapp/config.json").load()
cfg.set("language", "tr")
cfg.set("watchdog.threshold", 75)
cfg.save()                       # atomic

Bulk update with all-or-nothing semantics

try:
    cfg.update({"language": "tr", "watchdog.threshold": 200})
except ValidationError:
    pass                         # nothing changed; original values intact

Migrations

def v1_to_v2(data):
    data["language"] = data.pop("lang", "en")
    data["_version"] = 2
    return data

cfg = Config(schema, path, migrations=[v1_to_v2]).load()

Migrations run only when the on-disk _version does not match the schema default. Each migration is responsible for bumping _version.

TOML backend

cfg = Config(schema, "~/.config/myapp/config.toml", format="toml").load()

Reading TOML uses the stdlib tomllib (Python 3.11+). Writing TOML requires pip install codechu-config[toml].

API reference

Field

Parameter Purpose
type Coerced + checked type: str, int, float, bool, list, dict, or None for any.
default Used when the key is absent on load.
choices Iterable of allowed values; checked after coercion.
range (low, high) inclusive numeric bounds.
required If True, missing keys raise on load/validate.
doc Human description for tooling.

Schema

Method Purpose
Schema({name: Field, ...}) Build a schema. Names may be dotted ("watchdog.threshold").
.defaults() Fresh nested dict of all defaults.
.validate(data) Return a coerced + validated copy; raise ValidationError on failure.
.field(name) Look up a single field.

Config

Method Purpose
Config(schema, path, format="json", migrations=None) Construct.
.load() Read file (or use defaults), migrate, validate.
.save() Atomically write current state.
.get(key, default=None) Dotted-key read.
.set(key, value) Validated dotted-key write.
.update({key: value, ...}) All-or-nothing bulk update.
cfg[key] / cfg[key] = value Dict sugar over get / set.
.as_dict() Deep-ish copy of current data.

Exceptions

  • ConfigError — base.
  • ValidationError — raised by Field, Schema, and Config.set.
  • MigrationError — raised when a migration callable fails or returns a non-dict.

License

MIT — see LICENSE.

Part of Codechu.

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

codechu_config-0.1.0.tar.gz (16.5 kB view details)

Uploaded Source

Built Distribution

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

codechu_config-0.1.0-py3-none-any.whl (13.3 kB view details)

Uploaded Python 3

File details

Details for the file codechu_config-0.1.0.tar.gz.

File metadata

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

File hashes

Hashes for codechu_config-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4782d13a27fc526563870b6ded2dea4f62f9bb94c709ef88158df96482aa8ba7
MD5 94704449aa83021f8b0f070809a82c3f
BLAKE2b-256 6fc2337ef46d4d810f156d4bfac16b78ddceee6dc4e03be27968e66edacc47c5

See more details on using hashes here.

Provenance

The following attestation bundles were made for codechu_config-0.1.0.tar.gz:

Publisher: release.yml on codechu/config-py

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

File details

Details for the file codechu_config-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: codechu_config-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 13.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for codechu_config-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 11ff9177734b53060a60c38d9582dfa4c2f5198dd4452522a051b85dc85d17fd
MD5 377d4b35da95543693ec504d2437c84f
BLAKE2b-256 e5021d53725d3904a8adfa50bc105b6e5303894273a67f5547eb5dcaafa04252

See more details on using hashes here.

Provenance

The following attestation bundles were made for codechu_config-0.1.0-py3-none-any.whl:

Publisher: release.yml on codechu/config-py

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