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,
        overwrite_conflicts=True,  # create/overwrite missing/non-mapping parents as {}
        save=True,  # persist to disk
        threadsafe=True,  # offload parse/dump
    )
    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,
        data=doc,
        codec=Codec.YAML,
        route=Route("app", "theme"),
        value="dark",
        save=False,  # do NOT persist
    )

    # Optionally persist DATA mode to disk
    doc = await ConfigIO.set(
        loader=Loader.DATA,
        data=doc,
        codec=Codec.YAML,
        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,
        data=doc,
        codec=Codec.YAML,
        route=Route("app", "theme"),
        drop=True,  # prune empty parents bottom-up
        save=False,
    )
    print("after delete:", doc)


if __name__ == "__main__":
    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 🛣️

PathType = 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.2.tar.gz (9.6 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.2-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: cfgio-1.0.2.tar.gz
  • Upload date:
  • Size: 9.6 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.2.tar.gz
Algorithm Hash digest
SHA256 8f3ccc82fb4074d110e724770cf9176c9241f3fc46acfcb80b5d020579cbbaf1
MD5 d57edc92f1005a51b2dd75478e16d88c
BLAKE2b-256 e81058a107a0dfc95bfc497a3bbcd548ab9298d72dc7059fd9116fb9a644436c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cfgio-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 13.1 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e55b32d457e95712489afc439555c2165dfe97ea2a6bf1f8bff7875829355d0a
MD5 6e60e9cd9a8d0e82cd8017d1d606674c
BLAKE2b-256 05f3fe6d18e569b7938a2aacad7c0b79d84bcf1a765da3aa7a0c950b8e910266

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