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.ConfigParser, section: str, option: str, default: bool = 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- parser – instance of
-
cardinal_pythonlib.configfiles.
get_config_multiline_option
(parser: configparser.ConfigParser, section: str, option: str, default: List[str] = 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- parser – instance of
-
cardinal_pythonlib.configfiles.
get_config_parameter
(config: configparser.ConfigParser, section: str, param: str, fn: Callable[[Any], Any], default: Any = None, loglevel: int = 10) → Any[source]¶ Fetch parameter from
configparser
.INI
file.Parameters: - config –
ConfigParser
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
ifdefault is None
, orfn(default)
- config –
-
cardinal_pythonlib.configfiles.
get_config_parameter_boolean
(config: configparser.ConfigParser, section: str, param: str, default: bool, loglevel: int = 10) → bool[source]¶ Get Boolean parameter from
configparser
.INI
file.Parameters: - config –
ConfigParser
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
- config –
-
cardinal_pythonlib.configfiles.
get_config_parameter_loglevel
(config: configparser.ConfigParser, section: str, param: str, default: int, loglevel: int = 10) → int[source]¶ Get
loglevel
parameter fromconfigparser
.INI
file, e.g. mapping'debug'
tologging.DEBUG
.Parameters: - config –
ConfigParser
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
- config –
-
cardinal_pythonlib.configfiles.
get_config_parameter_multiline
(config: configparser.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: - config –
ConfigParser
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
- config –
-
cardinal_pythonlib.configfiles.
get_config_string_option
(parser: configparser.ConfigParser, section: str, option: str, default: str = 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- parser – instance of
-
cardinal_pythonlib.configfiles.
read_config_multiline_options
(obj: Any, parser: configparser.ConfigParser, section: str, options: Iterable[str]) → None[source]¶ This is to
read_config_string_options()
asget_config_multiline_option()
is toget_config_string_option()
.
-
cardinal_pythonlib.configfiles.
read_config_string_options
(obj: Any, parser: configparser.ConfigParser, section: str, options: Iterable[str], default: str = None) → None[source]¶ Reads config options and writes them as attributes of
obj
, with attribute names as peroptions
.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: