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 🛣️

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.1.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.1-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: cfgio-1.0.1.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.1.tar.gz
Algorithm Hash digest
SHA256 9a1bec6308f0a18a1b6372b115e5a8334cb1db0347b875c2aab69604129dee08
MD5 b47ef4ce84d2a97344dee5ccca6d353f
BLAKE2b-256 fab999980b4a7598355f330ecd76e551dd151044be45ebf4a1209aec09e7fb11

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cfgio-1.0.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 08828e544b58d67087fce1300aee1c7cbe2a347ccb16c2ccb4c4a41ba36d5ba0
MD5 4796ad4e191acabec39b99dd6dd13e21
BLAKE2b-256 a91cbbb9b5e635de35bcf35ed7ade4e358a116eb2b6d3cd6aaf2360dfdc454b1

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