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 keys —
cfg.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. Ifcodechu-fsis installed it is used transparently; otherwise the stdlib fallback ships in-package. - Migrations — point the
Configat a list of callables; they run in order whenever the on-disk_versiondiffers from the schema's default. - JSON + TOML — JSON by default (zero deps). TOML reads via stdlib
tomllib(Python 3.11+); TOML writes need the optionaltomli_w. - Dict sugar —
cfg["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 byField,Schema, andConfig.set.MigrationError— raised when a migration callable fails or returns a non-dict.
License
MIT — see LICENSE.
Part of Codechu.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4782d13a27fc526563870b6ded2dea4f62f9bb94c709ef88158df96482aa8ba7
|
|
| MD5 |
94704449aa83021f8b0f070809a82c3f
|
|
| BLAKE2b-256 |
6fc2337ef46d4d810f156d4bfac16b78ddceee6dc4e03be27968e66edacc47c5
|
Provenance
The following attestation bundles were made for codechu_config-0.1.0.tar.gz:
Publisher:
release.yml on codechu/config-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
codechu_config-0.1.0.tar.gz -
Subject digest:
4782d13a27fc526563870b6ded2dea4f62f9bb94c709ef88158df96482aa8ba7 - Sigstore transparency entry: 1582371793
- Sigstore integration time:
-
Permalink:
codechu/config-py@1e35aed4b81536c67ec0c2935fed5e9f5872c410 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/codechu
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@1e35aed4b81536c67ec0c2935fed5e9f5872c410 -
Trigger Event:
workflow_dispatch
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
11ff9177734b53060a60c38d9582dfa4c2f5198dd4452522a051b85dc85d17fd
|
|
| MD5 |
377d4b35da95543693ec504d2437c84f
|
|
| BLAKE2b-256 |
e5021d53725d3904a8adfa50bc105b6e5303894273a67f5547eb5dcaafa04252
|
Provenance
The following attestation bundles were made for codechu_config-0.1.0-py3-none-any.whl:
Publisher:
release.yml on codechu/config-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
codechu_config-0.1.0-py3-none-any.whl -
Subject digest:
11ff9177734b53060a60c38d9582dfa4c2f5198dd4452522a051b85dc85d17fd - Sigstore transparency entry: 1582371898
- Sigstore integration time:
-
Permalink:
codechu/config-py@1e35aed4b81536c67ec0c2935fed5e9f5872c410 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/codechu
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@1e35aed4b81536c67ec0c2935fed5e9f5872c410 -
Trigger Event:
workflow_dispatch
-
Statement type: