From 9c8c6c1fc2ccf31bc8c709ab052c34a45e207b36 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 19 Jan 2020 14:40:13 -0600 Subject: Add support for retrieving entire sections with get() --- config_file/parsers/ini_parser.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'config_file') diff --git a/config_file/parsers/ini_parser.py b/config_file/parsers/ini_parser.py index 3e93e4c..549ed37 100644 --- a/config_file/parsers/ini_parser.py +++ b/config_file/parsers/ini_parser.py @@ -33,15 +33,27 @@ class IniParser(BaseParser): :raises ParsingError: if the specified `section.key` is not found, in an invalid format, or if we are unable to coerce the return value to value_type. """ - # TODO: Support for retrieving entire sections instead of just key/values - section, key = split_on_dot(section_key, only_last_dot=True) + if "." not in section_key: + return self.__retrieve_section(section_key, parse_type) try: + section, key = split_on_dot(section_key, only_last_dot=True) value = self.parsed_content.get(section, key) return parse_value(value) if parse_type else value except configparser.Error as error: raise ParsingError(error.message) + def __retrieve_section(self, section, parse_type): + items = dict(self.parsed_content.items(section)) + + if not parse_type: + return items + + for item in items: + items[item] = parse_value(items[item]) + + return items + def set(self, section_key, value): """ Sets the value of 'section.key' of the config file. If the specified section -- cgit v1.2.3