Simplified `ConfigParser` setup.
Project description
cfgpie
Simplified ConfigParser
setup.
This module automates, to some extent, the setup of ConfigParser with cmd-line args parsing ability.
Installation:
python -m pip install [--upgrade] cfgpie
Usage:
After installation, simply import the class CfgParser
from cfgpie
module:
from cfgpie import CfgParser
By passing a name with the name
param we can have multiple named instances:
# mymodule.py
from cfgpie import CfgParser
cfg1: CfgParser = CfgParser(name="root")
cfg2: CfgParser = CfgParser(name="root")
cfg3: CfgParser = CfgParser(name="other")
if __name__ == '__main__':
print("*" * 80)
print("cfg1:", cfg1.name)
print("cfg2:", cfg2.name)
print("cfg3:", cfg3.name)
print("*" * 80)
print("cfg1 == cfg3:", cfg1 == cfg3)
print("cfg1 is cfg3:", cfg1 is cfg3)
print("*" * 80)
print("cfg1 == cfg2:", cfg1 == cfg2)
print("cfg1 is cfg2:", cfg1 is cfg2)
********************************************************************************
cfg1: root
cfg2: root
cfg3: other
********************************************************************************
cfg1 == cfg3: False
cfg1 is cfg3: False
********************************************************************************
cfg1 == cfg2: True
cfg1 is cfg2: True
Setting up our configuration:
# -*- coding: UTF-8 -*-
from os.path import dirname, realpath, join
from sys import modules
from types import ModuleType
from cfgpie import CfgParser
# main python module:
MODULE: ModuleType = modules.get("__main__")
# root directory:
ROOT: str = dirname(realpath(MODULE.__file__))
# config default file path:
CONFIG: str = join(ROOT, "config", "config.ini")
BACKUP: dict = {
"FOLDERS": {
"logger": r"${DEFAULT:directory}\logs", # extended interpolation
},
"TESTS": {
"option_1": "some_value",
"option_2": 23453,
"option_3": True,
"option_4": r"${DEFAULT:directory}\value", # extended interpolation
"option_5": ["abc", 345, 232.545, "3534.5435", True, {"key_": "value_"}, False],
}
}
cfg: CfgParser = CfgParser(
"root",
defaults={"directory": ROOT}
)
# we can update `DEFAULT` section:
# cfg.set_defaults(directory=ROOT)
# we can provide a backup dictionary
# in case our config file does not exist
# and by default a new file will be created
cfg.open(
file_path=CONFIG,
encoding="UTF-8",
fallback=BACKUP,
)
if __name__ == '__main__':
# we're parsing cmd-line arguments
cfg.read_argv()
# cmd-args are fetched as a list of strings:
# cfg.read_argv(["--tests-option_1", "another_value", "--tests-option_2", "6543"])
print(cfg.get("TESTS", "option_1"))
print(cfg.getint("TESTS", "option_2"))
For interpolation, refer to interpolation-of-values
documentation.
To pass cmd-line arguments:
python -O main.py --section-option value --section-option value
cmd-line args have priority over config file and will override the cfg params.
Defaults:
If not provided, by default, CfgParser
will set:
-
defaults
parameter as dict with sectionDEFAULT
and optiondirectory
to the root folder of the__main__
module. -
name
parameter to:cfgpie
; -
interpolation
parameter to ExtendedInterpolation; -
converters
parameter to evaluate:-
list
,tuple
,set
anddict
objects using ast.literal_eval() function; -
decimal
objects using decimal.Decimal(); -
path
strings using os.path.realpath(); -
folder
andfile
paths which:-
return a path-like formatted string depending on the operating system;
-
will recursively create the folder structure if missing (see
folder()
&file()
methods in utils.py).
-
All of which can be accessed by prefixing them with
get
:getlist("SECTION", "option")
gettuple("SECTION", "option")
getset("SECTION", "option")
getdict("SECTION", "option")
getdecimal("SECTION", "option")
getpath("SECTION", "option")
getfolder("SECTION", "option")
getfile("SECTION", "option")
-
All other parameters are passed directly to ConfigParser.
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 cfgpie-3.0.3.tar.gz
.
File metadata
- Download URL: cfgpie-3.0.3.tar.gz
- Upload date:
- Size: 8.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.10.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 96cdeacb7696eb124887d4eea96acb2bd8a04b49ab24ecd4311c9aefb5327d21 |
|
MD5 | 3d8de16cb5d0ab3003d741fe6dafd93b |
|
BLAKE2b-256 | 3da8e68094ac9bf23a7a2685a82163571d1c5ea4d3b7235d662e6a12acfdcc4a |
File details
Details for the file cfgpie-3.0.3-py3-none-any.whl
.
File metadata
- Download URL: cfgpie-3.0.3-py3-none-any.whl
- Upload date:
- Size: 7.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.10.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ff7157d0f14b03fb8c6f4b8922dc7d16469ca5d0933f32827571feac3047b468 |
|
MD5 | 42552652025e00cfa1891d67d970f027 |
|
BLAKE2b-256 | f5d278f013a536ba3b74774d932b8fb8a0e1e35694433e1e82870d5ba5e34c1d |