gws.lib.inifile

Read and write ini files.

Reads one or more ini files with configparser into a nested dict (section, then option) or into a flat dict with section.option keys, and writes a flat dict back as ini text. When reading, option names keep their case, files that do not exist are skipped, and values from later files override earlier ones.

Example:

d = gws.lib.inifile.from_paths_flat('/data/a.ini', '/data/b.ini')
# {'section.option': 'value', ...}
text = gws.lib.inifile.to_string({'section.option': 'value'})

Source code: gws.lib.inifile

Package Contents

gws.lib.inifile.from_paths(*paths: str) → dict

Read ini files into a nested dict.

Parameters:

*paths – Paths to ini files. Missing files are skipped, later files override earlier ones.

Returns:

A dict with section names as keys and dicts of options as values.

gws.lib.inifile.from_paths_flat(*paths: str) → dict

Read ini files into a flat dict.

Parameters:

*paths – Paths to ini files. Missing files are skipped, later files override earlier ones.

Returns:

A dict with section.option keys.

gws.lib.inifile.to_string(d: dict) → str

Convert a flat dict to ini text.

Keys are split at the first dot into a section name and an option name. Option names are lowercased.

Parameters:

d – A dict with section.option keys and string values.

Returns:

Ini file content.