Skip to content

Repository files navigation

━━━━━━━━━━━━ 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. ━━━━

PyPI Python CI License: MIT

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, forward migrations. Pure stdlib; tomli_w is an optional extra for writing TOML.

Install

pip install codechu-config           # JSON-only, zero deps
pip install "codechu-config[toml]"   # add TOML write support

Python 3.10+. TOML reading via stdlib tomllib (Python 3.11+).

Quick example

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

What you get

  • Schema validation — types, choices, ranges, required fields, enforced on every .set() and .update().
  • Dotted-key accesscfg.set("watchdog.threshold", 90) writes nested structure; reads with the same path.
  • Atomic save — tempfile in the same dir, fsync, then os.replace. A mid-write crash leaves the previous content intact. Uses codechu-fs if installed, stdlib fallback otherwise.
  • Migrations — list of callables run in order when on-disk _version differs 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 sugarcfg["language"] = "tr", "language" in cfg, iteration, length.

Read more

Family

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.

Credits

  • Atomic write pattern follows POSIX rename(2) guarantees.
  • Dotted-key conventions per common configuration tooling (Ansible, dynaconf, Hydra).

License

MIT — see LICENSE.

Part of Codechu.

About

Schema-driven config — dotted keys, atomic save, migrations, JSON+TOML

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages