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
Dependencies:
-
customlib.filehandlers.FileHandler
python -m pip install [--upgrade] customlib
Usage:
After installation, simply import the method get_config
from cfgpie
module:
from cfgpie import CfgParser, get_config
By passing a name with the instance
param we can have multiple instances:
# mymodule.py
from cfgpie import CfgParser, get_config
cfg: CfgParser = get_config(instance="main")
cfg2: CfgParser = get_config(instance="main")
other: CfgParser = get_config(instance="other")
if __name__ == '__main__':
print("*" * 80)
print("cfg == other:", cfg == other)
print("cfg is other:", cfg is other)
print("*" * 80)
print("cfg == cfg2:", cfg == cfg2)
print("cfg is cfg2:", cfg is cfg2)
********************************************************************************
cfg == other: False
cfg is other: False
********************************************************************************
cfg == cfg2: True
cfg is cfg2: True
Setting up our config environment:
# constants.py
from os.path import join, dirname, realpath
from sys import modules
from types import ModuleType
# main module:
MODULE: ModuleType = modules.get("__main__")
# root directory:
ROOT: str = dirname(realpath(MODULE.__file__))
# default config file path:
CONFIG: str = join(ROOT, "config", "config.ini")
# backup config params:
BACKUP: dict = {
"FOLDERS": {
"logger": r"${DEFAULT:directory}\logs",
},
"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],
},
}
For interpolation, refer to interpolation-of-values
documentation.
# mymodule.py
from cfgpie import CfgParser, get_config
from .constants import ROOT, CONFIG, BACKUP
cfg: CfgParser = get_config(instance="main")
# we can set default section options:
cfg.set_defaults(directory=ROOT)
# also possible at instance creation:
# cfg: CfgParser = get_config(defaults=some_dict)
# 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.parse()
print(cfg.get("TESTS", "option_1"))
print(cfg.getint("TESTS", "option_2"))
This is also possible given that cmd-args are fetched as a list of strings:
if __name__ == '__main__':
cfg.parse(["--tests-option_1", "another_value", "--tests-option_2", "6543"])
print(cfg.get("TESTS", "option_1"))
print(cfg.getint("TESTS", "option_2"))
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. -
instance
parameter to the namedefault
; -
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.