Skip to main content

Avatar

Build Status Code Scanning Status Dependencies Status Security Status Published Version

CFG-RW

CFG-RW is a Python library for reading and writing properties in configuration files.

Installation

pip3 install cfgrw

Usage

Configuration file

CFG-RW can read configuration properties from YAML, JSON, INI, and XML files.

Create a configuration file, e.g. cfgrw.yaml:

---
handlers: "stream,file"
datefmt: "%Y-%m-%d %H:%M:%S"
filename: "stage/test-integration/test-yaml-conf.log"
filemode: "w"
format: "%(levelname)s %(message)s"
level: "info"

Create CFGRW object with specific conf_file, and read the values of the configuration properties:

from cfgrw import CFGRW

cfgrw = CFGRW(conf_file='path/to/cfgrw.yaml')
values = cfgrw.read(['handlers', 'filemode', 'level'])
print(values['handlers']) # will print stream,file
print(values['filemode']) # will print w
print(values['level']) # will print info

Environment variables

CFG-RW can also read configuration properties from environment variables with a given prefix.

For example, here are the environment variables with prefix CFGRW_ :

export CFGRW_HANDLERS="stream,file"
export CFGRW_DATEFMT="%Y-%m-%d %H:%M:%S"
export CFGRW_FILENAME="stage/test-integration/test-yaml-conf.log"
export CFGRW_FILEMODE="w"
export CFGRW_FORMAT="%(levelname)s %(message)s"
export CFGRW_LEVEL="info"

Create CFGRW object without conf_file, and read the value of the configuration properties with specified prefix:

cfgrw = CFGRW()
values = cfgrw.read(['handlers', 'filemode', 'level'], { 'prefix': 'CFGRW_' })
print(values['handlers']) # will print stream,file
print(values['filemode']) # will print w
print(values['level']) # will print info

Configuration file with environment variable fallback

If a property is not found in the configuration file, CFG-RW will fall back to reading it from an environment variable. This requires a prefix to be passed in the options.

For example, given a configuration file cfgrw.yaml that only contains handlers and level:

---
handlers: "stream,file"
level: "info"

and the following environment variable for the missing filemode property:

export CFGRW_FILEMODE="w"

CFG-RW will read handlers and level from the file, then fall back to the environment variable for filemode:

from cfgrw import CFGRW

cfgrw = CFGRW(conf_file='path/to/cfgrw.yaml')
values = cfgrw.read(['handlers', 'filemode', 'level'], { 'prefix': 'CFGRW_' })
print(values['handlers']) # will print stream,file
print(values['filemode']) # will print w (from environment variable)
print(values['level'])    # will print info

If a property is missing from both the configuration file and the environment variables, it will simply be absent from the returned values dict — no error is raised.

Configuration file with Jinja template

CFG-RW can read configuration properties with YAML, JSON, INI, and XML within a Jinja template. You just need to add a .j2 to the configuration file name.

Create a configuration Jinja template, e.g. cfgrw.yaml.j2:

---
handlers: "{{ env.FOOBAR_HANDLERS }}"
datefmt: "%Y-%m-%d %H:%M:%S"
filename: "stage/test-integration/test-yaml-conf.log"
filemode: "{{ env.FOOBAR_FILEMODE }}"
format: "%(levelname)s %(message)s"
level: "{{ env.FOOBAR_LEVEL }}"

and the following environment variables:

export FOOBAR_HANDLERS="stream,file"
export FOOBAR_FILEMODE="w"
export FOOBAR_LEVEL="info"

Create CFGRW object with specific conf_file, and read the values of the configuration properties:

from cfgrw import CFGRW

cfgrw = CFGRW(conf_file='path/to/cfgrw.yaml.j2')
values = cfgrw.read(['handlers', 'level', 'level'])
print(values['handlers']) # will print stream,file
print(values['filemode']) # will print w
print(values['level']) # will print info

Configuration

CFG-RW automatically loads the configuration file based on the extension.

Format Extension
INI .ini
JSON .json
XML .xml
YAML .yaml or .yml
Jinja .ini.j2 or .json.j2 or .xml.j2 or .yaml.j2 or .yml.j2

Colophon

Developer's Guide

Build reports:

Metadata

Release files for cfgrw 1.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cfgrw 1.2.0
File Size Uploaded Provenance
cfgrw-1.2.0.tar.gz 9.0 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for cfgrw 1.2.0
File Interpreter ABI Platform
cfgrw-1.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 20.8 kB

Release files / cfgrw-1.2.0.tar.gz

Download URL cfgrw-1.2.0.tar.gz
Size 9.0 kB
Tags Source
SHA-256 checksum
How to use checksums
5baf1c4f1f92be1a3ffb256a859cfd236a4e4c8a86619fdb908344c12c5011f5
BLAKE2b-256 checksum
How to use checksums
41a32e6adbadfc94822d311f0d44190859639b5dfcacc6e4f04d60e1e43cf608
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.3.2 CPython/3.14.6 Linux/6.17.0-1018-azure

Release files / cfgrw-1.2.0-py3-none-any.whl

Download URL cfgrw-1.2.0-py3-none-any.whl
Size 11.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
15d9af216e2c0cc0b5c245880609cd1410665f50ba8eeee490a8f8fb543fb18e
BLAKE2b-256 checksum
How to use checksums
a74a676315022769c2131da3e38c26ec1720d5f4640f751f732b802259904714
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.3.2 CPython/3.14.6 Linux/6.17.0-1018-azure

Security for cfgrw 1.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page