cardinal_pythonlib.configfiles


Original code copyright (C) 2009-2022 Rudolf Cardinal (rudolf@pobox.com).

This file is part of cardinal_pythonlib.

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.


Support functions for config (.INI) file reading.

cardinal_pythonlib.configfiles.get_config_bool_option(parser: ConfigParser, section: str, option: str, default: bool | None = None) bool[source]

Retrieves a boolean value from a parser.

Parameters:
  • parser – instance of ConfigParser

  • section – section name within config file

  • option – option (variable) name within that section

  • default – value to return if option is absent

Returns:

Boolean value

Raises:

ValueError – if the section is absent

cardinal_pythonlib.configfiles.get_config_multiline_option(parser: ConfigParser, section: str, option: str, default: List[str] | None = None) List[str][source]

Retrieves a multi-line string value from a parser as a list of strings (one per line, ignoring blank lines).

Parameters:
  • parser – instance of ConfigParser

  • section – section name within config file

  • option – option (variable) name within that section

  • default – value to return if option is absent (None is mapped to [])

Returns:

list of strings

Raises:

ValueError – if the section is absent

cardinal_pythonlib.configfiles.get_config_parameter(config: ConfigParser, section: str, param: str, fn: Callable[[Any], Any], default: Any | None = None, loglevel: int = 10) Any[source]

Fetch parameter from configparser .INI file.

Parameters:
  • configConfigParser object

  • section – section name within config file

  • param – name of parameter within section

  • fn – function to apply to string parameter (e.g. int)

  • default – default value

  • loglevel – log level if default is needed

Returns:

parameter value, or None if default is None, or fn(default)

cardinal_pythonlib.configfiles.get_config_parameter_boolean(config: ConfigParser, section: str, param: str, default: bool, loglevel: int = 10) bool[source]

Get Boolean parameter from configparser .INI file.

Parameters:
  • configConfigParser object

  • section – section name within config file

  • param – name of parameter within section

  • default – default value

  • loglevel – log level if default is needed

Returns:

parameter value, or default

cardinal_pythonlib.configfiles.get_config_parameter_loglevel(config: ConfigParser, section: str, param: str, default: int, loglevel: int = 10) int[source]

Get loglevel parameter from configparser .INI file, e.g. mapping 'debug' to logging.DEBUG.

Parameters:
  • configConfigParser object

  • section – section name within config file

  • param – name of parameter within section

  • default – default value

  • loglevel – log level if default is needed

Returns:

parameter value, or default

cardinal_pythonlib.configfiles.get_config_parameter_multiline(config: ConfigParser, section: str, param: str, default: List[str], loglevel: int = 10) List[str][source]

Get multi-line string parameter from configparser .INI file, as a list of strings (one per line, ignoring blank lines).

Parameters:
  • configConfigParser object

  • section – section name within config file

  • param – name of parameter within section

  • default – default value

  • loglevel – log level if default is needed

Returns:

parameter value, or default

cardinal_pythonlib.configfiles.get_config_string_option(parser: ConfigParser, section: str, option: str, default: str | None = None) str[source]

Retrieves a string value from a parser.

Parameters:
  • parser – instance of ConfigParser

  • section – section name within config file

  • option – option (variable) name within that section

  • default – value to return if option is absent

Returns:

string value

Raises:

ValueError – if the section is absent

cardinal_pythonlib.configfiles.read_config_multiline_options(obj: Any, parser: ConfigParser, section: str, options: Iterable[str]) None[source]

This is to read_config_string_options() as get_config_multiline_option() is to get_config_string_option().

cardinal_pythonlib.configfiles.read_config_string_options(obj: Any, parser: ConfigParser, section: str, options: Iterable[str], default: str | None = None) None[source]

Reads config options and writes them as attributes of obj, with attribute names as per options.

Parameters:
  • obj – the object to modify

  • parser – instance of ConfigParser

  • section – section name within config file

  • options – option (variable) names within that section

  • default – value to use for any missing options

Returns: