Reading and writing INI format files

This module provides an enhanced INI format reader and writer based on the standard library’s configparser .

class clldutils.inifile.INI(defaults=None, dict_type=<class 'dict'>, allow_no_value=False, *, delimiters=('=', ':'), comment_prefixes=('#', ';'), inline_comment_prefixes=None, strict=True, empty_lines_in_values=True, default_section='DEFAULT', interpolation=<object object>, converters=<object object>)[source]

An enhanced ConfigParser with better support for list-valued options and multiline text.

static format_list(items)[source]

Concatenate items as INI style list.

Parameters:

items (collections.abc.Iterable[str]) –

Return type:

str

classmethod from_file(fname, encoding='utf-8', **kw)[source]

kw are passed through to ConfigParser.__init__.

Parameters:

fname (typing.Union[str, pathlib.Path]) –

Return type:

clldutils.inifile.INI

write_string(**kw)[source]

Write the INI prefixed with an encoding comment suitable for emacs.

Return type:

str

set(section, option, value=None)[source]

Enhances ConfigParser.set by

  • ignoring None values

  • creating missing sections

  • accepting list instances as value

Parameters:
  • section (str) –

  • option (str) –

  • value (typing.Union[None, list, tuple, typing.Any]) –

getlist(section, option)[source]

Get section content as list.

Parameters:
  • section (str) –

  • option (str) –

Return type:

list

gettext(section, option, whitespace_preserving_prefix='.')[source]

While configparser supports multiline values, it does this at the expense of stripping leading whitespace for each line in such a value. Sometimes we want to preserve such whitespace, e.g. to be able to put markdown with nested lists into INI files. We support this be introducing a special prefix, which is prepended to lines starting with whitespace in INI.settext() and stripped in INI.gettext() .

Return type:

str

settext(section, option, value, whitespace_preserving_prefix='.')[source]

Set a text option, preserving newlines.

Parameters:
  • section (str) –

  • option (str) –

  • value (str) –

write(fname, **kw)[source]

Write an INI file.