Python library for reading and writing properties in configuration files
Project description
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(value['handlers']) # will print stream,file
print(value['filemode']) # will print w
print(value['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(value['handlers']) # will print stream,file
print(value['filemode']) # will print w
print(value['level']) # will print info
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(value['handlers']) # will print stream,file
print(value['filemode']) # will print w
print(value['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
Build reports:
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
File details
Details for the file cfgrw-0.10.1.tar.gz
.
File metadata
- Download URL: cfgrw-0.10.1.tar.gz
- Upload date:
- Size: 8.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.12.7 Linux/6.5.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 60c3da6a50971961f957d6465b1b7bad4d7b04e2ce60e6c986327b1d8f3881c8 |
|
MD5 | bf5266cf0f53e415b506df49b1c9786a |
|
BLAKE2b-256 | ff220ee2d3bd6053e0982d728d23480c780e638850c86ac4743fa3d2d4375d76 |
File details
Details for the file cfgrw-0.10.1-py3-none-any.whl
.
File metadata
- Download URL: cfgrw-0.10.1-py3-none-any.whl
- Upload date:
- Size: 10.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.12.7 Linux/6.5.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 99dc0f01b394db35edd4457be05cb4c98393a414217b814340379314657cd183 |
|
MD5 | 8141588b3188869bcdf41a05318bb0b9 |
|
BLAKE2b-256 | 050a74b323caa099810934cd5c3294b9facb42ecaeb54db8e266cd75581174fd |