Skip to main content

Unified async JSON/YAML config I/O with routed access for files and in-memory data, featuring atomic saves and thread-offloaded parsing.

Project description

ConfigIO ⚙️

PyPI version Python Versions License: MIT

configio provides a single, loader‑driven API that can either work on files (FILE mode) or on in‑memory Python documents (DATA mode). Nested access and updates are addressed using pyroute.Route. Parsing and persistence are delegated to lightweight backends (configio.jsonio, configio.yamlio) that perform best‑effort atomic writes.


Features ✨

  • Two loaders, one API: operate on disk (loader=FILE) or on in‑memory objects (loader=DATA) with the same methods.
  • Routed access: immutable, hashable paths via Route("a", "b", "c").
  • Async I/O: all entry points are async.
  • Thread offload: threadsafe=True offloads heavy parse/dump to a worker thread (primarily useful in FILE mode).
  • Atomic saves: temp‑file + os.replace(...) pattern under the hood.
  • Strict codec: codec is explicit (Codec.JSON or Codec.YAML)—no extension inference.

Installation 📦

pip install cfgio -U

Quick Start 🚀

import asyncio
from configio import ConfigIO, Loader, Codec, Route
# Also you can import Route from pyroute package


async def main():
    # --- FILE mode (read from / write to disk) ---
    value = await ConfigIO.get(
        loader=Loader.FILE,
        codec=Codec.YAML,
        path="config.yml",
        route=Route("server", "port"),
    )
    print("server.port:", value)

    updated = await ConfigIO.set(
        loader=Loader.FILE,
        codec=Codec.JSON,
        path="config.json",
        route=Route("features", "beta"),
        value=True,
        save=True,               # persist to disk
        threadsafe=True,         # offload parse/dump
        overwrite_conflicts=True # create/overwrite missing/non-mapping parents as {}
    )
    print("updated FILE doc:", updated)

    # --- DATA mode (operate on an in-memory document) ---
    doc = {"app": {"theme": "light", "lang": "en"}}

    # Update in memory only
    doc = await ConfigIO.set(
        loader=Loader.DATA,
        codec=Codec.YAML,
        data=doc,
        route=Route("app", "theme"),
        value="dark",
        save=False,              # do NOT persist
    )

    # Optionally persist DATA mode to disk
    doc = await ConfigIO.set(
        loader=Loader.DATA,
        codec=Codec.YAML,
        data=doc,
        path="app.yml",
        route=Route("app", "lang"),
        value="fa-IR",
        save=True                # requires path when loader=DATA
    )

    # Delete with drop semantics
    doc = await ConfigIO.delete(
        loader=Loader.DATA,
        codec=Codec.YAML,
        data=doc,
        route=Route("app", "theme"),
        drop=True,               # prune empty parents bottom-up
        save=False
    )
    print("after delete:", doc)


asyncio.run(main())

Routing Cheatsheet 🧭

from pyroute import Route

Route("a")                # ["a"]
Route("a", "b", "c")      # ["a"]["b"]["c"]
# Use hashable keys (strings, ints...) to traverse mapping-only structures.

Path Types 🛣️

PathLike = Union[str, os.PathLike[str]]
At runtime, both plain strings and os.PathLike instances are accepted and validated.


Example Configs 📄

YAML

server:
  host: 127.0.0.1
  port: 8080
features:
  beta: false

JSON

{
  "server": { "host": "127.0.0.1", "port": 8080 },
  "features": { "beta": false }
}

License 📝

This project is licensed under the MIT License – see the LICENSE file for details.


Acknowledgements 🙏

  • pyroute for clean route semantics.

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

cfgio-1.0.0.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

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

cfgio-1.0.0-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: cfgio-1.0.0.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for cfgio-1.0.0.tar.gz
Algorithm Hash digest
SHA256 71f31e7fb22dadd3b21c0d75992d09b8220c0cfca05da23c870ff7306e15ac75
MD5 3885396f1a63c3e885cc3fe9f07f3b9a
BLAKE2b-256 b2c54e959219520aeba79089b3f8972ee9a5b6bf6b1b6b9d3c001c1d35c164af

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cfgio-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 13.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for cfgio-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 23b8eb0b3de0c999d4c5e6c1a42548f2f6e3fd47d28533327943d1b5b4b4eeba
MD5 5f88cef79988a63f17d3af7b8e68d28c
BLAKE2b-256 8601b5a9d66f4a4f1a96f2f0c64a58f410c11a9433f9018596f10044ebc5a9db

See more details on using hashes here.

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