━━━━━━━━━━━━ c o d e c h u · c o n f i g ━━━━━━━━━━━━
Schema({ Config(schema, path)
"language": Field(str, │
default="en", ├─ .load() ← run migrations
choices=["en","tr"]), ├─ .set(k, v) ← validate on set
"watchdog.threshold": ├─ .save() ← atomic fsync+rename
Field(int, └─ cfg["k"] ← dict sugar
range=(0, 100)),
}) JSON / TOML, dotted keys, migrations
━━━━ validates on set. saves atomically. migrates on load. ━━━━
Schema-first config. Validates on set. Saves atomically. Migrates on load.
Schema-driven configuration for Python. JSON + TOML backends,
dotted-key access, atomic save, forward migrations. Pure stdlib;
tomli_w is an optional extra for writing TOML.
pip install codechu-config # JSON-only, zero deps
pip install "codechu-config[toml]" # add TOML write supportPython 3.10+. TOML reading via stdlib tomllib (Python 3.11+).
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: fsync + os.replace- Schema validation — types, choices, ranges, required
fields, enforced on every
.set()and.update(). - Dotted-key access —
cfg.set("watchdog.threshold", 90)writes nested structure; reads with the same path. - Atomic save — tempfile in the same dir,
fsync, thenos.replace. A mid-write crash leaves the previous content intact. Usescodechu-fsif installed, stdlib fallback otherwise. - Migrations — list of callables run in order when on-disk
_versiondiffers from the schema default. Idempotent upgrades for users moving across releases. - JSON + TOML — JSON by default, zero deps. TOML reads via
stdlib
tomllib; TOML writes need the[toml]extra. - All-or-nothing
.update()— bulk write is validated as a unit; a single invalid field rolls the whole batch back. - Dict sugar —
cfg["language"] = "tr","language" in cfg, iteration, length.
- API reference — every public symbol with full signatures, edge cases, and validation rules.
- Changelog
| Library | Purpose |
|---|---|
| codechu-xdg | XDG Base Directory helpers, vendor-namespaced |
| codechu-fs | Filesystem primitives — atomic write, XDG trash |
| codechu-log | Structured logging — context, JSON, rotation |
| codechu-i18n | Internationalization — locale, plural rules, RTL |
| codechu-events | Thread-safe multi-channel pub/sub bus |
Full ecosystem: github.com/codechu.
- Atomic write pattern follows POSIX
rename(2)guarantees. - Dotted-key conventions per common configuration tooling (Ansible, dynaconf, Hydra).
MIT — see LICENSE.
Part of Codechu.